spagestic commited on
Commit
2b49599
·
1 Parent(s): e85907a

Add central globe panel and fix MapLibre loading in Gradio 6

Browse files
Files changed (5) hide show
  1. app.py +5 -1
  2. assets/globe.css +13 -2
  3. assets/globe.js +34 -4
  4. ui/chat.py +4 -2
  5. ui/globe.py +6 -2
app.py CHANGED
@@ -1,14 +1,18 @@
 
1
  import gradio as gr
2
 
3
  from ui.chat import create_chat_interface
 
4
  from ui.sidebar import render_sidebar
5
 
6
 
7
  def create_demo() -> gr.Blocks:
8
- with gr.Blocks() as demo:
9
  history_host = render_sidebar()
10
  create_chat_interface(history_container=history_host)
11
 
 
 
12
  return demo
13
 
14
 
 
1
+ # app.py
2
  import gradio as gr
3
 
4
  from ui.chat import create_chat_interface
5
+ from ui.globe import globe_head_html
6
  from ui.sidebar import render_sidebar
7
 
8
 
9
  def create_demo() -> gr.Blocks:
10
+ with gr.Blocks(fill_height=True) as demo:
11
  history_host = render_sidebar()
12
  create_chat_interface(history_container=history_host)
13
 
14
+ # Injected at launch (Gradio 6); also picked up by Hugging Face Spaces auto-launch.
15
+ demo._deprecated_head = globe_head_html()
16
  return demo
17
 
18
 
assets/globe.css CHANGED
@@ -1,6 +1,17 @@
1
  /* assets/globe.css */
2
- #map {
 
 
 
 
 
 
 
 
 
 
3
  width: 100%;
4
  height: 100%;
5
- min-height: 100vh;
 
6
  }
 
1
  /* assets/globe.css */
2
+ .globe-column {
3
+ flex: 1 1 auto;
4
+ min-height: 0;
5
+ }
6
+
7
+ .globe-panel {
8
+ height: 100%;
9
+ min-height: 0;
10
+ }
11
+
12
+ .globe-map {
13
  width: 100%;
14
  height: 100%;
15
+ min-height: calc(100vh - 6rem);
16
+ background: #0d1117;
17
  }
assets/globe.js CHANGED
@@ -1,4 +1,32 @@
1
  /* assets/globe.js */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  const GLOBE_DEFAULTS = {
3
  style: "https://tiles.openfreemap.org/styles/liberty",
4
  center: [0, 20],
@@ -90,13 +118,15 @@ function mergeGlobeOptions(...sources) {
90
  return merged;
91
  }
92
 
93
- function initGlobe(options = {}) {
94
- if (typeof maplibregl === "undefined") {
95
- setTimeout(() => initGlobe(options), 50);
 
 
96
  return;
97
  }
98
 
99
- const container = element.querySelector("#map");
100
  if (!container || container.dataset.mapInit) return;
101
  container.dataset.mapInit = "1";
102
 
 
1
  /* assets/globe.js */
2
+ const MAPLIBRE_VERSION = "5.24.0";
3
+ const MAPLIBRE_BASE = `https://unpkg.com/maplibre-gl@${MAPLIBRE_VERSION}/dist`;
4
+
5
+ function ensureMapLibre() {
6
+ if (typeof maplibregl !== "undefined") {
7
+ return Promise.resolve();
8
+ }
9
+ if (window.__maplibreLoadPromise) {
10
+ return window.__maplibreLoadPromise;
11
+ }
12
+ window.__maplibreLoadPromise = new Promise((resolve, reject) => {
13
+ if (!document.querySelector('link[data-globe-maplibre-css="1"]')) {
14
+ const link = document.createElement("link");
15
+ link.rel = "stylesheet";
16
+ link.href = `${MAPLIBRE_BASE}/maplibre-gl.css`;
17
+ link.dataset.globeMaplibreCss = "1";
18
+ document.head.appendChild(link);
19
+ }
20
+
21
+ const script = document.createElement("script");
22
+ script.src = `${MAPLIBRE_BASE}/maplibre-gl.js`;
23
+ script.onload = () => resolve();
24
+ script.onerror = () => reject(new Error("Failed to load MapLibre GL"));
25
+ document.head.appendChild(script);
26
+ });
27
+ return window.__maplibreLoadPromise;
28
+ }
29
+
30
  const GLOBE_DEFAULTS = {
31
  style: "https://tiles.openfreemap.org/styles/liberty",
32
  center: [0, 20],
 
118
  return merged;
119
  }
120
 
121
+ async function initGlobe(options = {}) {
122
+ try {
123
+ await ensureMapLibre();
124
+ } catch (err) {
125
+ console.error("Globe failed to load MapLibre GL:", err);
126
  return;
127
  }
128
 
129
+ const container = element.querySelector(".globe-map");
130
  if (!container || container.dataset.mapInit) return;
131
  container.dataset.mapInit = "1";
132
 
ui/chat.py CHANGED
@@ -1,5 +1,7 @@
 
1
  import gradio as gr
2
 
 
3
  from ui.inference import respond
4
 
5
  ADDITIONAL_INPUTS = [
@@ -32,8 +34,8 @@ class SidebarChatInterface(gr.ChatInterface):
32
  if self._history_container is not None:
33
  with self._history_container:
34
  super()._render_history_area()
35
- with gr.Column(scale=0, min_width=0, visible=False):
36
- gr.Markdown("")
37
  return
38
 
39
  super()._render_history_area()
 
1
+ # ui/chat.py
2
  import gradio as gr
3
 
4
+ from ui.globe import render_globe_panel
5
  from ui.inference import respond
6
 
7
  ADDITIONAL_INPUTS = [
 
34
  if self._history_container is not None:
35
  with self._history_container:
36
  super()._render_history_area()
37
+ with gr.Column(scale=10, min_width=400, elem_classes=["globe-column"]):
38
+ render_globe_panel(height="70vh")
39
  return
40
 
41
  super()._render_history_area()
ui/globe.py CHANGED
@@ -11,6 +11,11 @@ def _read_asset(name: str) -> str:
11
  return (ASSETS_DIR / name).read_text(encoding="utf-8")
12
 
13
 
 
 
 
 
 
14
  def _map_data_attrs(
15
  *,
16
  style: str = "https://tiles.openfreemap.org/styles/liberty",
@@ -122,8 +127,7 @@ def render_globe_panel(
122
  show_accuracy_circle=show_accuracy_circle,
123
  )
124
  return gr.HTML(
125
- value=f'<div id="map" {data_attrs}></div>',
126
- head=_read_asset("globe_head.html"),
127
  css_template=_read_asset("globe.css"),
128
  html_template="${value}",
129
  js_on_load=_read_asset("globe.js"),
 
11
  return (ASSETS_DIR / name).read_text(encoding="utf-8")
12
 
13
 
14
+ def globe_head_html() -> str:
15
+ """MapLibre assets for the page <head> (used by app launch)."""
16
+ return _read_asset("globe_head.html")
17
+
18
+
19
  def _map_data_attrs(
20
  *,
21
  style: str = "https://tiles.openfreemap.org/styles/liberty",
 
127
  show_accuracy_circle=show_accuracy_circle,
128
  )
129
  return gr.HTML(
130
+ value=f'<div class="globe-map" {data_attrs}></div>',
 
131
  css_template=_read_asset("globe.css"),
132
  html_template="${value}",
133
  js_on_load=_read_asset("globe.js"),