Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,9 +5,10 @@
|
|
| 5 |
# (7) Add Leg + Reset buttons
|
| 6 |
# (8) Stats panel
|
| 7 |
# Leaflet-style interactive map like your screenshot (pan/zoom/tiles/popups)
|
|
|
|
| 8 |
|
| 9 |
import re
|
| 10 |
-
import
|
| 11 |
import xml.etree.ElementTree as ET
|
| 12 |
|
| 13 |
import gradio as gr
|
|
@@ -178,10 +179,6 @@ def compute_stats(path_pts, seg_dists_m, total_time_min, legs):
|
|
| 178 |
|
| 179 |
# ---------------- Leaflet map (Folium) ----------------
|
| 180 |
def _all_bounds(points_list):
|
| 181 |
-
"""
|
| 182 |
-
points_list: iterable of lists of (lat, lon)
|
| 183 |
-
returns [[min_lat, min_lon], [max_lat, max_lon]] or None
|
| 184 |
-
"""
|
| 185 |
lats, lons = [], []
|
| 186 |
for pts in points_list:
|
| 187 |
for (la, lo) in pts:
|
|
@@ -195,7 +192,6 @@ def _all_bounds(points_list):
|
|
| 195 |
|
| 196 |
|
| 197 |
def build_leaflet_map(path_pts, legs, seg_dists_m, selected_rings):
|
| 198 |
-
# Center initial map near start; we'll fit_bounds after drawing
|
| 199 |
start_lat, start_lon = path_pts[0]
|
| 200 |
m = folium.Map(
|
| 201 |
location=[start_lat, start_lon],
|
|
@@ -204,8 +200,7 @@ def build_leaflet_map(path_pts, legs, seg_dists_m, selected_rings):
|
|
| 204 |
control_scale=True,
|
| 205 |
)
|
| 206 |
|
| 207 |
-
# ---- ping rings (
|
| 208 |
-
# draw selected rings first so path sits on top
|
| 209 |
for ring_name in selected_rings or []:
|
| 210 |
ring_pts = PING_RINGS.get(ring_name)
|
| 211 |
if not ring_pts:
|
|
@@ -213,15 +208,17 @@ def build_leaflet_map(path_pts, legs, seg_dists_m, selected_rings):
|
|
| 213 |
folium.PolyLine(
|
| 214 |
ring_pts,
|
| 215 |
weight=3,
|
| 216 |
-
opacity=0.
|
|
|
|
| 217 |
tooltip=ring_name,
|
| 218 |
).add_to(m)
|
| 219 |
|
| 220 |
-
# ---- flight path
|
| 221 |
folium.PolyLine(
|
| 222 |
path_pts,
|
| 223 |
weight=4,
|
| 224 |
opacity=1.0,
|
|
|
|
| 225 |
tooltip="Flight path",
|
| 226 |
).add_to(m)
|
| 227 |
|
|
@@ -230,18 +227,27 @@ def build_leaflet_map(path_pts, legs, seg_dists_m, selected_rings):
|
|
| 230 |
location=path_pts[0],
|
| 231 |
radius=6,
|
| 232 |
fill=True,
|
| 233 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
).add_to(m)
|
| 235 |
|
| 236 |
folium.CircleMarker(
|
| 237 |
location=path_pts[-1],
|
| 238 |
radius=6,
|
| 239 |
fill=True,
|
| 240 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
).add_to(m)
|
| 242 |
|
| 243 |
-
# ---- waypoint markers with popups
|
| 244 |
-
# Waypoint i corresponds to end of leg i (i>=1). Waypoint 0 is start.
|
| 245 |
total_dist_km = 0.0
|
| 246 |
elapsed_min = 0.0
|
| 247 |
|
|
@@ -269,13 +275,14 @@ def build_leaflet_map(path_pts, legs, seg_dists_m, selected_rings):
|
|
| 269 |
f"Distance flown: {total_dist_km:.2f} km",
|
| 270 |
]
|
| 271 |
|
| 272 |
-
popup_html = "<br>".join(popup_lines)
|
| 273 |
-
|
| 274 |
folium.CircleMarker(
|
| 275 |
location=(lat, lon),
|
| 276 |
radius=5,
|
| 277 |
fill=True,
|
| 278 |
-
|
|
|
|
|
|
|
|
|
|
| 279 |
).add_to(m)
|
| 280 |
|
| 281 |
# ---- fit map to all plotted data ----
|
|
@@ -286,8 +293,18 @@ def build_leaflet_map(path_pts, legs, seg_dists_m, selected_rings):
|
|
| 286 |
if bounds:
|
| 287 |
m.fit_bounds(bounds, padding=(20, 20))
|
| 288 |
|
| 289 |
-
#
|
| 290 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
|
| 292 |
|
| 293 |
def run_sim(start_lat, start_lon, legs_df, method, rings):
|
|
@@ -329,7 +346,7 @@ with gr.Blocks() as demo:
|
|
| 329 |
|
| 330 |
btn = gr.Button("Simulate on Map")
|
| 331 |
|
| 332 |
-
out_map = gr.HTML(
|
| 333 |
stats = gr.Markdown()
|
| 334 |
|
| 335 |
btn.click(
|
|
@@ -342,4 +359,3 @@ with gr.Blocks() as demo:
|
|
| 342 |
reset_btn.click(reset_legs, outputs=[legs_df])
|
| 343 |
|
| 344 |
demo.launch()
|
| 345 |
-
|
|
|
|
| 5 |
# (7) Add Leg + Reset buttons
|
| 6 |
# (8) Stats panel
|
| 7 |
# Leaflet-style interactive map like your screenshot (pan/zoom/tiles/popups)
|
| 8 |
+
# FIX: Folium map is embedded via <iframe srcdoc=...> so it shows correctly in Gradio
|
| 9 |
|
| 10 |
import re
|
| 11 |
+
import html
|
| 12 |
import xml.etree.ElementTree as ET
|
| 13 |
|
| 14 |
import gradio as gr
|
|
|
|
| 179 |
|
| 180 |
# ---------------- Leaflet map (Folium) ----------------
|
| 181 |
def _all_bounds(points_list):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
lats, lons = [], []
|
| 183 |
for pts in points_list:
|
| 184 |
for (la, lo) in pts:
|
|
|
|
| 192 |
|
| 193 |
|
| 194 |
def build_leaflet_map(path_pts, legs, seg_dists_m, selected_rings):
|
|
|
|
| 195 |
start_lat, start_lon = path_pts[0]
|
| 196 |
m = folium.Map(
|
| 197 |
location=[start_lat, start_lon],
|
|
|
|
| 200 |
control_scale=True,
|
| 201 |
)
|
| 202 |
|
| 203 |
+
# ---- ping rings (blue like your screenshot) ----
|
|
|
|
| 204 |
for ring_name in selected_rings or []:
|
| 205 |
ring_pts = PING_RINGS.get(ring_name)
|
| 206 |
if not ring_pts:
|
|
|
|
| 208 |
folium.PolyLine(
|
| 209 |
ring_pts,
|
| 210 |
weight=3,
|
| 211 |
+
opacity=0.95,
|
| 212 |
+
color="blue",
|
| 213 |
tooltip=ring_name,
|
| 214 |
).add_to(m)
|
| 215 |
|
| 216 |
+
# ---- flight path (red like your screenshot) ----
|
| 217 |
folium.PolyLine(
|
| 218 |
path_pts,
|
| 219 |
weight=4,
|
| 220 |
opacity=1.0,
|
| 221 |
+
color="red",
|
| 222 |
tooltip="Flight path",
|
| 223 |
).add_to(m)
|
| 224 |
|
|
|
|
| 227 |
location=path_pts[0],
|
| 228 |
radius=6,
|
| 229 |
fill=True,
|
| 230 |
+
color="green",
|
| 231 |
+
fill_opacity=1.0,
|
| 232 |
+
popup=folium.Popup(
|
| 233 |
+
f"<b>Start</b><br>Lat: {path_pts[0][0]:.5f}<br>Lon: {path_pts[0][1]:.5f}",
|
| 234 |
+
max_width=300,
|
| 235 |
+
),
|
| 236 |
).add_to(m)
|
| 237 |
|
| 238 |
folium.CircleMarker(
|
| 239 |
location=path_pts[-1],
|
| 240 |
radius=6,
|
| 241 |
fill=True,
|
| 242 |
+
color="purple",
|
| 243 |
+
fill_opacity=1.0,
|
| 244 |
+
popup=folium.Popup(
|
| 245 |
+
f"<b>End</b><br>Lat: {path_pts[-1][0]:.5f}<br>Lon: {path_pts[-1][1]:.5f}",
|
| 246 |
+
max_width=300,
|
| 247 |
+
),
|
| 248 |
).add_to(m)
|
| 249 |
|
| 250 |
+
# ---- waypoint markers with popups ----
|
|
|
|
| 251 |
total_dist_km = 0.0
|
| 252 |
elapsed_min = 0.0
|
| 253 |
|
|
|
|
| 275 |
f"Distance flown: {total_dist_km:.2f} km",
|
| 276 |
]
|
| 277 |
|
|
|
|
|
|
|
| 278 |
folium.CircleMarker(
|
| 279 |
location=(lat, lon),
|
| 280 |
radius=5,
|
| 281 |
fill=True,
|
| 282 |
+
color="white",
|
| 283 |
+
fill_color="white",
|
| 284 |
+
fill_opacity=1.0,
|
| 285 |
+
popup=folium.Popup("<br>".join(popup_lines), max_width=340),
|
| 286 |
).add_to(m)
|
| 287 |
|
| 288 |
# ---- fit map to all plotted data ----
|
|
|
|
| 293 |
if bounds:
|
| 294 |
m.fit_bounds(bounds, padding=(20, 20))
|
| 295 |
|
| 296 |
+
# ---- IMPORTANT FIX: embed as iframe with fixed height ----
|
| 297 |
+
map_html = m.get_root().render()
|
| 298 |
+
escaped = html.escape(map_html, quote=True)
|
| 299 |
+
|
| 300 |
+
return f"""
|
| 301 |
+
<iframe
|
| 302 |
+
srcdoc="{escaped}"
|
| 303 |
+
style="width: 100%; height: 650px; border: 0; border-radius: 12px;"
|
| 304 |
+
loading="lazy"
|
| 305 |
+
referrerpolicy="no-referrer"
|
| 306 |
+
></iframe>
|
| 307 |
+
"""
|
| 308 |
|
| 309 |
|
| 310 |
def run_sim(start_lat, start_lon, legs_df, method, rings):
|
|
|
|
| 346 |
|
| 347 |
btn = gr.Button("Simulate on Map")
|
| 348 |
|
| 349 |
+
out_map = gr.HTML()
|
| 350 |
stats = gr.Markdown()
|
| 351 |
|
| 352 |
btn.click(
|
|
|
|
| 359 |
reset_btn.click(reset_legs, outputs=[legs_df])
|
| 360 |
|
| 361 |
demo.launch()
|
|
|