Fix Gradio map initialization
Browse files- app.py +11 -1
- assets/map.js +13 -7
app.py
CHANGED
|
@@ -31,6 +31,10 @@ def read_asset(name: str) -> str:
|
|
| 31 |
return path.read_text(encoding="utf-8") if path.exists() else ""
|
| 32 |
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
MAP_HTML = """
|
| 35 |
<div id="sis-map-shell">
|
| 36 |
<div class="map-controls">
|
|
@@ -343,12 +347,15 @@ def _file_path(file_obj: Any) -> str:
|
|
| 343 |
|
| 344 |
def build_app() -> gr.Blocks:
|
| 345 |
map_js = read_asset("map.js")
|
|
|
|
| 346 |
block_kwargs: dict[str, Any] = {"title": "Site Intelligence Studio"}
|
| 347 |
block_parameters = inspect.signature(gr.Blocks).parameters
|
| 348 |
if "css" in block_parameters:
|
| 349 |
block_kwargs["css"] = read_asset("style.css")
|
| 350 |
if "head" in block_parameters:
|
| 351 |
block_kwargs["head"] = HEAD_HTML
|
|
|
|
|
|
|
| 352 |
with gr.Blocks(**block_kwargs) as demo:
|
| 353 |
gr.HTML(
|
| 354 |
"""
|
|
@@ -424,7 +431,8 @@ def build_app() -> gr.Blocks:
|
|
| 424 |
warnings_output,
|
| 425 |
],
|
| 426 |
)
|
| 427 |
-
|
|
|
|
| 428 |
return demo
|
| 429 |
|
| 430 |
|
|
@@ -440,6 +448,8 @@ if __name__ == "__main__":
|
|
| 440 |
launch_kwargs["css"] = read_asset("style.css")
|
| 441 |
if "head" in launch_parameters:
|
| 442 |
launch_kwargs["head"] = HEAD_HTML
|
|
|
|
|
|
|
| 443 |
if "ssr_mode" in launch_parameters:
|
| 444 |
launch_kwargs["ssr_mode"] = False
|
| 445 |
demo.launch(**launch_kwargs)
|
|
|
|
| 31 |
return path.read_text(encoding="utf-8") if path.exists() else ""
|
| 32 |
|
| 33 |
|
| 34 |
+
def gradio_js_callback(script: str) -> str:
|
| 35 |
+
return "() => {\n" + script + "\n}"
|
| 36 |
+
|
| 37 |
+
|
| 38 |
MAP_HTML = """
|
| 39 |
<div id="sis-map-shell">
|
| 40 |
<div class="map-controls">
|
|
|
|
| 347 |
|
| 348 |
def build_app() -> gr.Blocks:
|
| 349 |
map_js = read_asset("map.js")
|
| 350 |
+
map_js_callback = gradio_js_callback(map_js)
|
| 351 |
block_kwargs: dict[str, Any] = {"title": "Site Intelligence Studio"}
|
| 352 |
block_parameters = inspect.signature(gr.Blocks).parameters
|
| 353 |
if "css" in block_parameters:
|
| 354 |
block_kwargs["css"] = read_asset("style.css")
|
| 355 |
if "head" in block_parameters:
|
| 356 |
block_kwargs["head"] = HEAD_HTML
|
| 357 |
+
if "js" in block_parameters:
|
| 358 |
+
block_kwargs["js"] = map_js_callback
|
| 359 |
with gr.Blocks(**block_kwargs) as demo:
|
| 360 |
gr.HTML(
|
| 361 |
"""
|
|
|
|
| 431 |
warnings_output,
|
| 432 |
],
|
| 433 |
)
|
| 434 |
+
if "js" not in block_parameters:
|
| 435 |
+
demo.load(fn=None, js=map_js_callback)
|
| 436 |
return demo
|
| 437 |
|
| 438 |
|
|
|
|
| 448 |
launch_kwargs["css"] = read_asset("style.css")
|
| 449 |
if "head" in launch_parameters:
|
| 450 |
launch_kwargs["head"] = HEAD_HTML
|
| 451 |
+
if "js" in launch_parameters:
|
| 452 |
+
launch_kwargs["js"] = gradio_js_callback(read_asset("map.js"))
|
| 453 |
if "ssr_mode" in launch_parameters:
|
| 454 |
launch_kwargs["ssr_mode"] = False
|
| 455 |
demo.launch(**launch_kwargs)
|
assets/map.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
function initSiteMap() {
|
| 2 |
const mapNode = document.getElementById("site-map");
|
| 3 |
-
if (!mapNode || mapNode.dataset.ready === "true" || typeof L === "undefined") return;
|
| 4 |
mapNode.dataset.ready = "true";
|
| 5 |
|
| 6 |
const map = L.map("site-map", { zoomControl: true }).setView([21.0, 70.0], 14);
|
|
@@ -163,6 +163,8 @@ function initSiteMap() {
|
|
| 163 |
if (pinMarker) placePin(pinMarker.getLatLng());
|
| 164 |
});
|
| 165 |
setMode("pin");
|
|
|
|
|
|
|
| 166 |
}
|
| 167 |
|
| 168 |
let leafletFallbackRequested = false;
|
|
@@ -190,15 +192,19 @@ function requestLeafletFallback() {
|
|
| 190 |
}
|
| 191 |
|
| 192 |
function waitForLeafletAndInit(attempt = 0) {
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
return;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
}
|
| 197 |
-
if (attempt === 6) requestLeafletFallback();
|
| 198 |
-
if (attempt >
|
| 199 |
const status = document.getElementById("map-status");
|
| 200 |
if (status) {
|
| 201 |
-
status.textContent = "Map
|
| 202 |
}
|
| 203 |
return;
|
| 204 |
}
|
|
|
|
| 1 |
function initSiteMap() {
|
| 2 |
const mapNode = document.getElementById("site-map");
|
| 3 |
+
if (!mapNode || mapNode.dataset.ready === "true" || typeof L === "undefined") return false;
|
| 4 |
mapNode.dataset.ready = "true";
|
| 5 |
|
| 6 |
const map = L.map("site-map", { zoomControl: true }).setView([21.0, 70.0], 14);
|
|
|
|
| 163 |
if (pinMarker) placePin(pinMarker.getLatLng());
|
| 164 |
});
|
| 165 |
setMode("pin");
|
| 166 |
+
setTimeout(() => map.invalidateSize(), 250);
|
| 167 |
+
return true;
|
| 168 |
}
|
| 169 |
|
| 170 |
let leafletFallbackRequested = false;
|
|
|
|
| 192 |
}
|
| 193 |
|
| 194 |
function waitForLeafletAndInit(attempt = 0) {
|
| 195 |
+
const hasMapNode = Boolean(document.getElementById("site-map"));
|
| 196 |
+
if (typeof L !== "undefined" && hasMapNode) {
|
| 197 |
+
if (initSiteMap()) return;
|
| 198 |
+
}
|
| 199 |
+
if (typeof L !== "undefined" && !hasMapNode) {
|
| 200 |
+
const status = document.getElementById("map-status");
|
| 201 |
+
if (status) status.textContent = "Map UI is loading...";
|
| 202 |
}
|
| 203 |
+
if (attempt === 6 && typeof L === "undefined") requestLeafletFallback();
|
| 204 |
+
if (attempt > 80) {
|
| 205 |
const status = document.getElementById("map-status");
|
| 206 |
if (status) {
|
| 207 |
+
status.textContent = "Map could not initialize. Use the lat/lon field or upload DXF/GeoJSON, then generate the report.";
|
| 208 |
}
|
| 209 |
return;
|
| 210 |
}
|