Nanny7 commited on
Commit
9a65f4e
·
1 Parent(s): 87ac600

Serve Chemiscope viewer from local file

Browse files
Files changed (1) hide show
  1. app.py +12 -24
app.py CHANGED
@@ -301,38 +301,26 @@ def _infer_space_origin():
301
 
302
 
303
  def _build_chemiscope_embed(dataset_payload: dict, dataset_path: str) -> str:
304
- """Create HTML content for Chemiscope visualization, preferring hosted datasets."""
305
- space_origin = _infer_space_origin()
306
- if space_origin and dataset_path:
307
- relative_path = Path(dataset_path).as_posix()
308
- dataset_url = f"{space_origin}/file={urllib.parse.quote(relative_path)}"
309
- iframe_src = f"https://chemiscope.org/?load={urllib.parse.quote(dataset_url, safe='')}"
310
- return (
311
- "<div style='width:100%;'>"
312
- "<iframe "
313
- "title='Chemiscope explorer' "
314
- "style='width:100%;height:620px;border:none;border-radius:8px;' "
315
- f"src='{iframe_src}'></iframe>"
316
- "<p style='font-size:0.9em;margin-top:0.5rem;'>"
317
- "Open in a new tab if the viewer looks blank: "
318
- f"<a href='{iframe_src}' target='_blank' rel='noopener'>chemiscope.org</a>"
319
- "</p>"
320
- "</div>"
321
- )
322
-
323
  template_html = _load_chemiscope_template()
324
  dataset_json = json.dumps(dataset_payload, ensure_ascii=False, separators=(",", ":"))
325
  combined = template_html + dataset_json
326
- encoded = base64.b64encode(combined.encode("utf-8")).decode("ascii")
 
 
 
 
 
 
327
  return (
328
  "<div style='width:100%;'>"
329
  "<iframe "
330
- "title='Chemiscope explorer (offline fallback)' "
331
  "style='width:100%;height:620px;border:none;border-radius:8px;' "
332
- f"src='data:text/html;base64,{encoded}'></iframe>"
333
  "<p style='font-size:0.9em;margin-top:0.5rem;'>"
334
- "Use the map view to explore descriptors interactively or download the dataset for "
335
- "https://chemiscope.org/. "
336
  "</p>"
337
  "</div>"
338
  )
 
301
 
302
 
303
  def _build_chemiscope_embed(dataset_payload: dict, dataset_path: str) -> str:
304
+ """Create HTML content for Chemiscope visualization served locally from the Space."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
305
  template_html = _load_chemiscope_template()
306
  dataset_json = json.dumps(dataset_payload, ensure_ascii=False, separators=(",", ":"))
307
  combined = template_html + dataset_json
308
+
309
+ dataset_file = Path(dataset_path)
310
+ viewer_name = dataset_file.name.replace(".json.gz", "_viewer.html")
311
+ viewer_path = dataset_file.parent / viewer_name
312
+ viewer_path.write_text(combined, encoding="utf-8")
313
+
314
+ iframe_src = f"/file={urllib.parse.quote(viewer_path.as_posix())}"
315
  return (
316
  "<div style='width:100%;'>"
317
  "<iframe "
318
+ "title='Chemiscope explorer' "
319
  "style='width:100%;height:620px;border:none;border-radius:8px;' "
320
+ f"src='{iframe_src}'></iframe>"
321
  "<p style='font-size:0.9em;margin-top:0.5rem;'>"
322
+ "Open in a new tab if the viewer looks blank: "
323
+ f"<a href='{iframe_src}' target='_blank' rel='noopener'>Chemiscope standalone</a>"
324
  "</p>"
325
  "</div>"
326
  )