Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,6 @@ DEFAULT_CENTER = [38.1157, 13.3615] # Palermo
|
|
| 6 |
DEFAULT_ZOOM = 12
|
| 7 |
|
| 8 |
def render_map(lat, lon, zoom, geojson_file, show_zoom_ctrl, show_scale, tileset):
|
| 9 |
-
# Build a Folium map (OSM by default)
|
| 10 |
tiles = "OpenStreetMap" if tileset == "OpenStreetMap" else tileset
|
| 11 |
m = folium.Map(
|
| 12 |
location=[lat, lon],
|
|
@@ -15,30 +14,23 @@ def render_map(lat, lon, zoom, geojson_file, show_zoom_ctrl, show_scale, tileset
|
|
| 15 |
control_scale=bool(show_scale),
|
| 16 |
zoom_control=bool(show_zoom_ctrl),
|
| 17 |
prefer_canvas=True,
|
|
|
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
-
# Optional: add a GeoJSON overlay if provided
|
| 21 |
if geojson_file is not None:
|
| 22 |
try:
|
| 23 |
with open(geojson_file.name, "r", encoding="utf-8") as f:
|
| 24 |
gj = json.load(f)
|
| 25 |
-
folium.GeoJson(
|
| 26 |
-
gj,
|
| 27 |
-
name="Overlay",
|
| 28 |
-
tooltip=folium.GeoJsonTooltip(fields=[]),
|
| 29 |
-
).add_to(m)
|
| 30 |
folium.LayerControl().add_to(m)
|
| 31 |
except Exception as e:
|
| 32 |
-
folium.Marker(
|
| 33 |
-
location=[lat, lon],
|
| 34 |
-
tooltip=f"Failed to load GeoJSON: {e}"
|
| 35 |
-
).add_to(m)
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
return m.get_root().render()
|
| 39 |
|
| 40 |
with gr.Blocks(title="OpenStreetMap Viewer (Gradio)") as demo:
|
| 41 |
-
gr.Markdown("## OpenStreetMap Viewer
|
| 42 |
|
| 43 |
with gr.Row():
|
| 44 |
lat = gr.Number(value=DEFAULT_CENTER[0], label="Latitude", precision=6)
|
|
@@ -47,15 +39,8 @@ with gr.Blocks(title="OpenStreetMap Viewer (Gradio)") as demo:
|
|
| 47 |
|
| 48 |
with gr.Row():
|
| 49 |
tileset = gr.Dropdown(
|
| 50 |
-
choices=[
|
| 51 |
-
|
| 52 |
-
"Stamen Terrain",
|
| 53 |
-
"Stamen Toner",
|
| 54 |
-
"CartoDB positron",
|
| 55 |
-
"CartoDB dark_matter"
|
| 56 |
-
],
|
| 57 |
-
value="OpenStreetMap",
|
| 58 |
-
label="Tiles"
|
| 59 |
)
|
| 60 |
show_zoom_ctrl = gr.Checkbox(True, label="Show zoom control")
|
| 61 |
show_scale = gr.Checkbox(True, label="Show scale bar")
|
|
@@ -63,20 +48,11 @@ with gr.Blocks(title="OpenStreetMap Viewer (Gradio)") as demo:
|
|
| 63 |
geojson_file = gr.File(label="Optional GeoJSON upload", file_types=[".geojson", ".json"], interactive=True)
|
| 64 |
|
| 65 |
btn = gr.Button("Render map")
|
| 66 |
-
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
render_map,
|
| 71 |
-
inputs=[lat, lon, zoom, geojson_file, show_zoom_ctrl, show_scale, tileset],
|
| 72 |
-
outputs=out
|
| 73 |
-
)
|
| 74 |
-
# Re-render on click
|
| 75 |
-
btn.click(
|
| 76 |
-
render_map,
|
| 77 |
-
inputs=[lat, lon, zoom, geojson_file, show_zoom_ctrl, show_scale, tileset],
|
| 78 |
-
outputs=out
|
| 79 |
-
)
|
| 80 |
|
| 81 |
if __name__ == "__main__":
|
| 82 |
demo.launch()
|
|
|
|
| 6 |
DEFAULT_ZOOM = 12
|
| 7 |
|
| 8 |
def render_map(lat, lon, zoom, geojson_file, show_zoom_ctrl, show_scale, tileset):
|
|
|
|
| 9 |
tiles = "OpenStreetMap" if tileset == "OpenStreetMap" else tileset
|
| 10 |
m = folium.Map(
|
| 11 |
location=[lat, lon],
|
|
|
|
| 14 |
control_scale=bool(show_scale),
|
| 15 |
zoom_control=bool(show_zoom_ctrl),
|
| 16 |
prefer_canvas=True,
|
| 17 |
+
width="100%", # ensure it fills the container
|
| 18 |
+
height=600 # give it a visible height
|
| 19 |
)
|
| 20 |
|
|
|
|
| 21 |
if geojson_file is not None:
|
| 22 |
try:
|
| 23 |
with open(geojson_file.name, "r", encoding="utf-8") as f:
|
| 24 |
gj = json.load(f)
|
| 25 |
+
folium.GeoJson(gj, name="Overlay").add_to(m)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
folium.LayerControl().add_to(m)
|
| 27 |
except Exception as e:
|
| 28 |
+
folium.Marker([lat, lon], tooltip=f"Failed to load GeoJSON: {e}").add_to(m)
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
return m.get_root().render() # includes the required <script> tags
|
|
|
|
| 31 |
|
| 32 |
with gr.Blocks(title="OpenStreetMap Viewer (Gradio)") as demo:
|
| 33 |
+
gr.Markdown("## OpenStreetMap Viewer")
|
| 34 |
|
| 35 |
with gr.Row():
|
| 36 |
lat = gr.Number(value=DEFAULT_CENTER[0], label="Latitude", precision=6)
|
|
|
|
| 39 |
|
| 40 |
with gr.Row():
|
| 41 |
tileset = gr.Dropdown(
|
| 42 |
+
choices=["OpenStreetMap","Stamen Terrain","Stamen Toner","CartoDB positron","CartoDB dark_matter"],
|
| 43 |
+
value="OpenStreetMap", label="Tiles"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
)
|
| 45 |
show_zoom_ctrl = gr.Checkbox(True, label="Show zoom control")
|
| 46 |
show_scale = gr.Checkbox(True, label="Show scale bar")
|
|
|
|
| 48 |
geojson_file = gr.File(label="Optional GeoJSON upload", file_types=[".geojson", ".json"], interactive=True)
|
| 49 |
|
| 50 |
btn = gr.Button("Render map")
|
| 51 |
+
# IMPORTANT: allow scripts so Leaflet can run
|
| 52 |
+
out = gr.HTML(label="Map", sanitize=False)
|
| 53 |
|
| 54 |
+
demo.load(render_map, [lat, lon, zoom, geojson_file, show_zoom_ctrl, show_scale, tileset], out)
|
| 55 |
+
btn.click(render_map, [lat, lon, zoom, geojson_file, show_zoom_ctrl, show_scale, tileset], out)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
if __name__ == "__main__":
|
| 58 |
demo.launch()
|