bpHigh commited on
Commit
b36b193
·
verified ·
1 Parent(s): 507a111

Pages show a new deploy at once

Browse files
openenv/rail_openenv/gradio_ui.py CHANGED
@@ -6,11 +6,27 @@ A frame is only created when its tab is first opened, so the 3D replay does not
6
 
7
  from __future__ import annotations
8
 
 
9
  import html
 
10
 
11
  import gradio as gr
12
 
13
- PAGES = [("Eval explorer", "/explorer/?embed=1"), ("3D replay", "/twin/"), ("Train graph", "/viewer/?embed=1")]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  # OpenEnv's page caps the container width; the pages want the full window.
15
  STYLE = ("<style>.gradio-container .main.fillable{max-width:none!important;padding-left:12px!important;"
16
  "padding-right:12px!important}.gradio-container .html-container:has(iframe){padding:0!important}</style>")
 
6
 
7
  from __future__ import annotations
8
 
9
+ import hashlib
10
  import html
11
+ from pathlib import Path
12
 
13
  import gradio as gr
14
 
15
+ WEB = Path(__file__).resolve().parent / "web"
16
+
17
+
18
+ def version() -> str:
19
+ """A tag that changes whenever a page or script does, so a frame never shows a copy cached before a deploy."""
20
+ h = hashlib.sha1()
21
+ for p in sorted(WEB.rglob("*")):
22
+ if p.is_file() and p.suffix in (".html", ".js", ".css"):
23
+ h.update(p.read_bytes())
24
+ return h.hexdigest()[:10]
25
+
26
+
27
+ V = version()
28
+ PAGES = [("Eval explorer", f"/explorer/?embed=1&v={V}"), ("3D replay", f"/twin/?v={V}"),
29
+ ("Train graph", f"/viewer/?embed=1&v={V}")]
30
  # OpenEnv's page caps the container width; the pages want the full window.
31
  STYLE = ("<style>.gradio-container .main.fillable{max-width:none!important;padding-left:12px!important;"
32
  "padding-right:12px!important}.gradio-container .html-container:has(iframe){padding:0!important}</style>")
openenv/rail_openenv/server.py CHANGED
@@ -187,10 +187,23 @@ def timeline(task, plan) -> dict:
187
  "optimal_cost": task.reference.get("optimal_cost")}
188
 
189
 
 
 
 
190
  def mount_api(app, pack: TaskPack) -> None:
191
  plans_by_task = run_plans()
192
  runs = runs_summary(load_runs())
193
 
 
 
 
 
 
 
 
 
 
 
194
  @app.get("/api/tasks", include_in_schema=False)
195
  def tasks():
196
  return [_summary(t, plans_by_task) for t in pack.tasks]
 
187
  "optimal_cost": task.reference.get("optimal_cost")}
188
 
189
 
190
+ PAGES = ("/twin", "/explorer", "/viewer")
191
+
192
+
193
  def mount_api(app, pack: TaskPack) -> None:
194
  plans_by_task = run_plans()
195
  runs = runs_summary(load_runs())
196
 
197
+ @app.middleware("http")
198
+ async def revalidate_pages(request, call_next):
199
+ """The pages and their scripts change with every deploy. Without a Cache-Control header browsers guess how
200
+ long to reuse a stored copy (often hours), so a new version did not show; `no-cache` makes them ask every time,
201
+ and an unchanged file costs a 304."""
202
+ response = await call_next(request)
203
+ if request.url.path.startswith(PAGES):
204
+ response.headers.setdefault("Cache-Control", "no-cache")
205
+ return response
206
+
207
  @app.get("/api/tasks", include_in_schema=False)
208
  def tasks():
209
  return [_summary(t, plans_by_task) for t in pack.tasks]