manikandanj commited on
Commit
346af56
·
verified ·
1 Parent(s): 065b11f

Fix Space cockpit JavaScript loading

Browse files
Files changed (2) hide show
  1. app.py +3 -3
  2. src/time_machine/ui/gradio_app.py +11 -11
app.py CHANGED
@@ -77,8 +77,8 @@ async def blank_redirect():
77
  return RedirectResponse(url="/")
78
 
79
 
80
- head = getattr(demo, "time_machine_head", None)
81
- fastapi_app_with_gradio = gr.mount_gradio_app(fastapi_app, demo, path="/app", head=head)
82
  app = fastapi_app_with_gradio if os.getenv("TIME_MACHINE_FASTAPI_WRAPPER") else demo
83
 
84
 
@@ -90,4 +90,4 @@ if __name__ == "__main__":
90
  if os.getenv("TIME_MACHINE_FASTAPI_WRAPPER"):
91
  uvicorn.run(fastapi_app_with_gradio, host=host, port=port)
92
  else:
93
- demo.launch(server_name=host, server_port=port)
 
77
  return RedirectResponse(url="/")
78
 
79
 
80
+ runtime_js = getattr(demo, "time_machine_js", None)
81
+ fastapi_app_with_gradio = gr.mount_gradio_app(fastapi_app, demo, path="/app", js=runtime_js)
82
  app = fastapi_app_with_gradio if os.getenv("TIME_MACHINE_FASTAPI_WRAPPER") else demo
83
 
84
 
 
90
  if os.getenv("TIME_MACHINE_FASTAPI_WRAPPER"):
91
  uvicorn.run(fastapi_app_with_gradio, host=host, port=port)
92
  else:
93
+ demo.launch(server_name=host, server_port=port, js=runtime_js)
src/time_machine/ui/gradio_app.py CHANGED
@@ -2,7 +2,6 @@ from __future__ import annotations
2
 
3
  from pathlib import Path
4
  import base64
5
- import inspect
6
  import re
7
 
8
  import gradio as gr
@@ -30,14 +29,11 @@ def create_app(container: AppContainer) -> gr.Blocks:
30
 
31
  cockpit_html = _route_static_urls((ASSET_DIR / "cockpit.html").read_text(encoding="utf-8"))
32
  intro_html = _route_static_urls((ASSET_DIR / "intro.html").read_text(encoding="utf-8"))
33
- intro_js = (ASSET_DIR / "intro.js").read_text(encoding="utf-8")
34
- cockpit_js = (ASSET_DIR / "cockpit.js").read_text(encoding="utf-8")
35
-
36
- blocks_kwargs = {"title": "Trans-Temporal Express"}
37
- if "head" in inspect.signature(gr.Blocks).parameters:
38
- blocks_kwargs["head"] = f"<script>{cockpit_js}</script><script>{intro_js}</script>"
39
-
40
- with gr.Blocks(**blocks_kwargs) as demo:
41
  session_state = gr.State(handlers.initial_state())
42
 
43
  gr.HTML(f"<style>{css}</style>{intro_html}{cockpit_html}")
@@ -155,8 +151,8 @@ def create_app(container: AppContainer) -> gr.Blocks:
155
  outputs=[status],
156
  )
157
 
158
- if "head" not in blocks_kwargs:
159
- demo.time_machine_head = f"<script>{cockpit_js}</script><script>{intro_js}</script>"
160
  return demo.queue()
161
 
162
 
@@ -167,3 +163,7 @@ def _route_static_urls(text: str) -> str:
167
  def _replace_static_url(match: re.Match[str]) -> str:
168
  relative_path = match.group("path")
169
  return f"{match.group('quote')}/gradio_api/file=static/{relative_path}{match.group('quote')}"
 
 
 
 
 
2
 
3
  from pathlib import Path
4
  import base64
 
5
  import re
6
 
7
  import gradio as gr
 
29
 
30
  cockpit_html = _route_static_urls((ASSET_DIR / "cockpit.html").read_text(encoding="utf-8"))
31
  intro_html = _route_static_urls((ASSET_DIR / "intro.html").read_text(encoding="utf-8"))
32
+ intro_js = (ASSET_DIR / "intro.js").read_text(encoding="utf-8")
33
+ cockpit_js = (ASSET_DIR / "cockpit.js").read_text(encoding="utf-8")
34
+ runtime_js = _as_onload_js(f"{cockpit_js}\n{intro_js}")
35
+
36
+ with gr.Blocks(title="Trans-Temporal Express") as demo:
 
 
 
37
  session_state = gr.State(handlers.initial_state())
38
 
39
  gr.HTML(f"<style>{css}</style>{intro_html}{cockpit_html}")
 
151
  outputs=[status],
152
  )
153
 
154
+ demo.js = runtime_js
155
+ demo.time_machine_js = runtime_js
156
  return demo.queue()
157
 
158
 
 
163
  def _replace_static_url(match: re.Match[str]) -> str:
164
  relative_path = match.group("path")
165
  return f"{match.group('quote')}/gradio_api/file=static/{relative_path}{match.group('quote')}"
166
+
167
+
168
+ def _as_onload_js(script: str) -> str:
169
+ return f"() => {{\n{script}\n}}"