NathanRoll commited on
Commit
7bfcbcf
·
verified ·
1 Parent(s): 551e234

Use panel router for submit entry

Browse files
Files changed (1) hide show
  1. app.py +49 -18
app.py CHANGED
@@ -138,6 +138,11 @@ gradio-app,
138
  font-family: Helvetica, Arial, sans-serif !important;
139
  }
140
 
 
 
 
 
 
141
  .hero-stage {
142
  border-bottom: 2px solid var(--line);
143
  padding: 30px 0 20px;
@@ -1194,6 +1199,27 @@ APP_JS = """
1194
  const href = element.getAttribute && element.getAttribute("href");
1195
  return href === "/" || href === "./" || href === cleanHomeUrl();
1196
  };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1197
  const routeFromClick = (event) => {
1198
  const control = event.target && event.target.closest
1199
  ? event.target.closest("[data-route], .site-nav-link")
@@ -1214,11 +1240,14 @@ APP_JS = """
1214
  window.location.assign(cleanHomeUrl());
1215
  return;
1216
  }
1217
- if (route === "leaderboard") {
1218
- return;
1219
- }
1220
- if (route === "submit") {
1221
- return;
 
 
 
1222
  }
1223
  };
1224
  const nativeHomeFallback = (event) => {
@@ -1237,6 +1266,11 @@ APP_JS = """
1237
  };
1238
  document.addEventListener("click", routeFromClick, true);
1239
  document.addEventListener("click", nativeHomeFallback, true);
 
 
 
 
 
1240
  hideGradioChrome();
1241
  window.setTimeout(hideGradioChrome, 100);
1242
  window.setTimeout(hideGradioChrome, 700);
@@ -2172,20 +2206,15 @@ def loading_html() -> str:
2172
  """
2173
 
2174
 
2175
- def routed_page_for_request(request: gr.Request):
2176
  try:
2177
  query = dict(request.query_params) if request else {}
2178
  except Exception:
2179
  query = {}
2180
  setup = str(query.get("family") or "").strip()
2181
- view = str(query.get("view") or "").strip().lower()
2182
- if view == "leaderboard":
2183
- return gr.update(value=leaderboard_html(), visible=True), gr.update(visible=False)
2184
- if view == "submit":
2185
- return gr.update(value="", visible=False), gr.update(visible=True)
2186
  if setup in setup_choices():
2187
- return gr.update(value=family_page_html(setup), visible=True), gr.update(visible=False)
2188
- return gr.update(value=directory_html(), visible=True), gr.update(visible=False)
2189
 
2190
 
2191
  def condensed_case_links(cases: list[str]) -> str:
@@ -2387,10 +2416,11 @@ def submit_solution(
2387
  message,
2388
  preview_html(stored),
2389
  pretty,
 
2390
  gr.update(choices=existing_author_choices(), value=submitter),
2391
  )
2392
  except Exception as exc:
2393
- return f"### Submission Rejected\n\n- {exc}", empty_preview_html(), json_text, gr.update()
2394
 
2395
 
2396
  THEME = gr.themes.Base()
@@ -2411,9 +2441,10 @@ with gr.Blocks(
2411
  footer_links=[],
2412
  )
2413
  ) as demo:
2414
- browse_page = gr.HTML(directory_html())
 
2415
 
2416
- with gr.Group(visible=False, elem_classes=["submit-entry-panel"]) as submit_panel:
2417
  gr.HTML(submit_schema_intro_html())
2418
  with gr.Group(elem_classes=["submit-shell"]):
2419
  with gr.Row(elem_classes=["submit-layout"]):
@@ -2482,12 +2513,12 @@ with gr.Blocks(
2482
  with gr.Accordion("Schema And Coordinate Rules", open=False):
2483
  gr.Markdown(SCHEMA_DOCS_MD)
2484
 
2485
- demo.load(routed_page_for_request, outputs=[browse_page, submit_panel], show_progress="hidden")
2486
  verify_btn.click(verify_only, inputs=[json_code, json_file, tolerance], outputs=[report, preview, normalized_json])
2487
  submit_btn.click(
2488
  submit_solution,
2489
  inputs=[json_code, json_file, submitter, notes, source_url, tolerance],
2490
- outputs=[report, preview, normalized_json, submitter],
2491
  )
2492
 
2493
 
 
138
  font-family: Helvetica, Arial, sans-serif !important;
139
  }
140
 
141
+ #leaderboard-panel,
142
+ #submit-entry-panel {
143
+ display: none;
144
+ }
145
+
146
  .hero-stage {
147
  border-bottom: 2px solid var(--line);
148
  padding: 30px 0 20px;
 
1199
  const href = element.getAttribute && element.getAttribute("href");
1200
  return href === "/" || href === "./" || href === cleanHomeUrl();
1201
  };
1202
+ const panelIds = {
1203
+ home: "home-panel",
1204
+ leaderboard: "leaderboard-panel",
1205
+ submit: "submit-entry-panel",
1206
+ };
1207
+ const setPanelVisible = (id, visible) => {
1208
+ const element = document.getElementById(id);
1209
+ if (!element) {
1210
+ return;
1211
+ }
1212
+ if (visible) {
1213
+ element.style.setProperty("display", "block", "important");
1214
+ } else {
1215
+ element.style.setProperty("display", "none", "important");
1216
+ }
1217
+ };
1218
+ const applyRoute = (route) => {
1219
+ const selected = route === "leaderboard" || route === "submit" ? route : "home";
1220
+ Object.entries(panelIds).forEach(([name, id]) => setPanelVisible(id, name === selected));
1221
+ hideGradioChrome();
1222
+ };
1223
  const routeFromClick = (event) => {
1224
  const control = event.target && event.target.closest
1225
  ? event.target.closest("[data-route], .site-nav-link")
 
1240
  window.location.assign(cleanHomeUrl());
1241
  return;
1242
  }
1243
+ if (route === "leaderboard" || route === "submit") {
1244
+ event.preventDefault();
1245
+ event.stopPropagation();
1246
+ if (event.stopImmediatePropagation) {
1247
+ event.stopImmediatePropagation();
1248
+ }
1249
+ window.history.pushState(null, "", `${cleanHomeUrl()}?view=${route}`);
1250
+ applyRoute(route);
1251
  }
1252
  };
1253
  const nativeHomeFallback = (event) => {
 
1266
  };
1267
  document.addEventListener("click", routeFromClick, true);
1268
  document.addEventListener("click", nativeHomeFallback, true);
1269
+ window.addEventListener("popstate", () => {
1270
+ const view = new URLSearchParams(window.location.search).get("view");
1271
+ applyRoute(view || "home");
1272
+ });
1273
+ applyRoute(new URLSearchParams(window.location.search).get("view") || "home");
1274
  hideGradioChrome();
1275
  window.setTimeout(hideGradioChrome, 100);
1276
  window.setTimeout(hideGradioChrome, 700);
 
2206
  """
2207
 
2208
 
2209
+ def home_page_for_request(request: gr.Request):
2210
  try:
2211
  query = dict(request.query_params) if request else {}
2212
  except Exception:
2213
  query = {}
2214
  setup = str(query.get("family") or "").strip()
 
 
 
 
 
2215
  if setup in setup_choices():
2216
+ return family_page_html(setup)
2217
+ return gr.update()
2218
 
2219
 
2220
  def condensed_case_links(cases: list[str]) -> str:
 
2416
  message,
2417
  preview_html(stored),
2418
  pretty,
2419
+ leaderboard_html(),
2420
  gr.update(choices=existing_author_choices(), value=submitter),
2421
  )
2422
  except Exception as exc:
2423
+ return f"### Submission Rejected\n\n- {exc}", empty_preview_html(), json_text, gr.update(), gr.update()
2424
 
2425
 
2426
  THEME = gr.themes.Base()
 
2441
  footer_links=[],
2442
  )
2443
  ) as demo:
2444
+ browse_page = gr.HTML(directory_html(), elem_id="home-panel")
2445
+ leaderboard_page = gr.HTML(leaderboard_html(), elem_id="leaderboard-panel")
2446
 
2447
+ with gr.Group(elem_id="submit-entry-panel", elem_classes=["submit-entry-panel"]):
2448
  gr.HTML(submit_schema_intro_html())
2449
  with gr.Group(elem_classes=["submit-shell"]):
2450
  with gr.Row(elem_classes=["submit-layout"]):
 
2513
  with gr.Accordion("Schema And Coordinate Rules", open=False):
2514
  gr.Markdown(SCHEMA_DOCS_MD)
2515
 
2516
+ demo.load(home_page_for_request, outputs=[browse_page], show_progress="hidden")
2517
  verify_btn.click(verify_only, inputs=[json_code, json_file, tolerance], outputs=[report, preview, normalized_json])
2518
  submit_btn.click(
2519
  submit_solution,
2520
  inputs=[json_code, json_file, submitter, notes, source_url, tolerance],
2521
+ outputs=[report, preview, normalized_json, leaderboard_page, submitter],
2522
  )
2523
 
2524