Nusri7 commited on
Commit
d8e43d0
Β·
verified Β·
1 Parent(s): 3bfd49f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -19
app.py CHANGED
@@ -14,6 +14,8 @@ gmaps = googlemaps.Client(key=API_KEY)
14
 
15
  def get_business_suggestions(query, location):
16
  try:
 
 
17
  response = gmaps.places_autocomplete(query, location=location)
18
  return [{"name": item["description"], "place_id": item["place_id"]} for item in response]
19
  except Exception as e:
@@ -115,6 +117,12 @@ st.set_page_config(page_title="Business Rank Checker", layout="wide")
115
  st.title("πŸ“Š Business Rank Checker")
116
  st.markdown("Check your business ranking on Google Maps and search results")
117
 
 
 
 
 
 
 
118
  # User Inputs
119
  with st.form("search_form"):
120
  col1, col2 = st.columns(2)
@@ -123,26 +131,28 @@ with st.form("search_form"):
123
  with col2:
124
  search_radius_km = st.slider("πŸ” Search Radius (km)", 1, 20, 2)
125
 
126
- # Combined business name input with autocomplete dropdown
127
  business_query = st.text_input("🏒 Your Business Name", "", key="business_query")
128
- business_place_id = None
129
- business_selection = None
130
 
131
- if business_query:
132
- suggestions = get_business_suggestions(business_query, location)
 
133
  if suggestions:
134
- # Create a selectbox that appears below the input field
135
- selected_index = 0
136
- options = [s["name"] for s in suggestions]
137
- business_selection = st.selectbox(
138
- "πŸ”Ž Select from suggestions",
139
- options=options,
140
- index=selected_index,
141
- key="business_selection",
142
- label_visibility="collapsed" # Hide the label
143
  )
144
- business_place_id = next(s["place_id"] for s in suggestions if s["name"] == business_selection)
145
- else:
 
 
 
 
 
 
146
  st.warning("No matching businesses found")
147
 
148
  website_url = st.text_input("🌐 Your Website URL", "", help="Enter your website to check if it appears in search results")
@@ -150,7 +160,7 @@ with st.form("search_form"):
150
 
151
  submitted = st.form_submit_button("πŸš€ Check Rankings")
152
 
153
- if submitted and business_query and business_place_id:
154
  keywords = [kw.strip() for kw in keywords_input.split(",") if kw.strip()]
155
 
156
  if not keywords:
@@ -167,7 +177,7 @@ if submitted and business_query and business_place_id:
167
  st.markdown("### πŸ“Œ Google Maps Rankings")
168
  maps_rankings = get_google_maps_rank(
169
  keyword, location,
170
- business_selection, business_place_id,
171
  search_radius_km
172
  )
173
 
@@ -184,7 +194,7 @@ if submitted and business_query and business_place_id:
184
  })
185
 
186
  # Display in app
187
- st.markdown(f"#### Your Business: **{business_selection}**")
188
  st.write(f"Rank: {maps_rankings['rank']}")
189
  st.write(f"Address: {maps_rankings['address']}")
190
 
 
14
 
15
  def get_business_suggestions(query, location):
16
  try:
17
+ if not query or len(query) < 3: # Don't search for very short queries
18
+ return []
19
  response = gmaps.places_autocomplete(query, location=location)
20
  return [{"name": item["description"], "place_id": item["place_id"]} for item in response]
21
  except Exception as e:
 
117
  st.title("πŸ“Š Business Rank Checker")
118
  st.markdown("Check your business ranking on Google Maps and search results")
119
 
120
+ # Initialize session state variables
121
+ if 'business_selection' not in st.session_state:
122
+ st.session_state.business_selection = None
123
+ if 'business_place_id' not in st.session_state:
124
+ st.session_state.business_place_id = None
125
+
126
  # User Inputs
127
  with st.form("search_form"):
128
  col1, col2 = st.columns(2)
 
131
  with col2:
132
  search_radius_km = st.slider("πŸ” Search Radius (km)", 1, 20, 2)
133
 
134
+ # Business name input with real-time suggestions
135
  business_query = st.text_input("🏒 Your Business Name", "", key="business_query")
 
 
136
 
137
+ # Show suggestions as you type (appears automatically)
138
+ if st.session_state.business_query and len(st.session_state.business_query) >= 3:
139
+ suggestions = get_business_suggestions(st.session_state.business_query, location)
140
  if suggestions:
141
+ selected_option = st.selectbox(
142
+ "πŸ”Ž Select your business from suggestions",
143
+ options=[s["name"] for s in suggestions],
144
+ index=0,
145
+ key="business_suggestion_select",
146
+ label_visibility="collapsed"
 
 
 
147
  )
148
+ # Update the selected business in session state
149
+ if selected_option:
150
+ st.session_state.business_selection = selected_option
151
+ st.session_state.business_place_id = next(
152
+ s["place_id"] for s in suggestions
153
+ if s["name"] == selected_option
154
+ )
155
+ elif len(st.session_state.business_query) >= 3:
156
  st.warning("No matching businesses found")
157
 
158
  website_url = st.text_input("🌐 Your Website URL", "", help="Enter your website to check if it appears in search results")
 
160
 
161
  submitted = st.form_submit_button("πŸš€ Check Rankings")
162
 
163
+ if submitted and st.session_state.business_selection and st.session_state.business_place_id:
164
  keywords = [kw.strip() for kw in keywords_input.split(",") if kw.strip()]
165
 
166
  if not keywords:
 
177
  st.markdown("### πŸ“Œ Google Maps Rankings")
178
  maps_rankings = get_google_maps_rank(
179
  keyword, location,
180
+ st.session_state.business_selection, st.session_state.business_place_id,
181
  search_radius_km
182
  )
183
 
 
194
  })
195
 
196
  # Display in app
197
+ st.markdown(f"#### Your Business: **{st.session_state.business_selection}**")
198
  st.write(f"Rank: {maps_rankings['rank']}")
199
  st.write(f"Address: {maps_rankings['address']}")
200