Update app.py
Browse files
app.py
CHANGED
|
@@ -162,20 +162,26 @@ def run_search(country_name, state_name, categories):
|
|
| 162 |
country_code = COUNTRY_CODES[idx]
|
| 163 |
except ValueError:
|
| 164 |
return "Invalid country", None, None, None
|
|
|
|
| 165 |
# geocode state
|
| 166 |
bbox = geocode_region(state_name if state_name else country_name, country_code)
|
| 167 |
if bbox is None:
|
| 168 |
return f"Could not geocode region '{state_name}'. Try a different subdivision or broaden to the country.", None, None, None
|
|
|
|
| 169 |
if not categories:
|
| 170 |
return "Please select at least one category.", None, None, None
|
|
|
|
| 171 |
df = fetch_places(categories, bbox)
|
| 172 |
map_html = make_map(df, center_bbox=bbox)
|
| 173 |
csv_bytes = df_to_csv_bytes(df)
|
|
|
|
| 174 |
elapsed = time.time() - start
|
| 175 |
msg = f"Found {len(df)} places for {', '.join(categories)} in {state_name}, {country_name} (took {elapsed:.1f}s)."
|
| 176 |
-
|
|
|
|
| 177 |
csv_b64 = base64.b64encode(csv_bytes).decode("utf-8")
|
| 178 |
-
csv_href = f"data:text/csv;base64,{csv_b64}"
|
|
|
|
| 179 |
return msg, df, map_html, csv_href
|
| 180 |
|
| 181 |
# --- Build Gradio UI -----------------------------------------------------
|
|
@@ -197,7 +203,7 @@ with gr.Blocks() as demo:
|
|
| 197 |
categories = gr.CheckboxGroup(place_options, label="Categories to search", value=["cafe","restaurant"])
|
| 198 |
search_btn = gr.Button("Search places")
|
| 199 |
info = gr.Textbox(label="Status", interactive=False)
|
| 200 |
-
download = gr.
|
| 201 |
with gr.Column(scale=2):
|
| 202 |
map_html_out = gr.HTML(label="Map")
|
| 203 |
table_out = gr.Dataframe(headers=["name","amenity","lat","lon","address","osm_id"], label="Results Table")
|
|
|
|
| 162 |
country_code = COUNTRY_CODES[idx]
|
| 163 |
except ValueError:
|
| 164 |
return "Invalid country", None, None, None
|
| 165 |
+
|
| 166 |
# geocode state
|
| 167 |
bbox = geocode_region(state_name if state_name else country_name, country_code)
|
| 168 |
if bbox is None:
|
| 169 |
return f"Could not geocode region '{state_name}'. Try a different subdivision or broaden to the country.", None, None, None
|
| 170 |
+
|
| 171 |
if not categories:
|
| 172 |
return "Please select at least one category.", None, None, None
|
| 173 |
+
|
| 174 |
df = fetch_places(categories, bbox)
|
| 175 |
map_html = make_map(df, center_bbox=bbox)
|
| 176 |
csv_bytes = df_to_csv_bytes(df)
|
| 177 |
+
|
| 178 |
elapsed = time.time() - start
|
| 179 |
msg = f"Found {len(df)} places for {', '.join(categories)} in {state_name}, {country_name} (took {elapsed:.1f}s)."
|
| 180 |
+
|
| 181 |
+
# 🔑 Create download link as HTML
|
| 182 |
csv_b64 = base64.b64encode(csv_bytes).decode("utf-8")
|
| 183 |
+
csv_href = f'<a href="data:text/csv;base64,{csv_b64}" download="results.csv">📥 Download CSV</a>'
|
| 184 |
+
|
| 185 |
return msg, df, map_html, csv_href
|
| 186 |
|
| 187 |
# --- Build Gradio UI -----------------------------------------------------
|
|
|
|
| 203 |
categories = gr.CheckboxGroup(place_options, label="Categories to search", value=["cafe","restaurant"])
|
| 204 |
search_btn = gr.Button("Search places")
|
| 205 |
info = gr.Textbox(label="Status", interactive=False)
|
| 206 |
+
download = gr.HTML(label="Download results (CSV)")
|
| 207 |
with gr.Column(scale=2):
|
| 208 |
map_html_out = gr.HTML(label="Map")
|
| 209 |
table_out = gr.Dataframe(headers=["name","amenity","lat","lon","address","osm_id"], label="Results Table")
|