"""
space/_three.py
--------------
Generates a self-contained HTML+Three.js snippet for the 3D soul space.
Called both at startup (base model points) and on each probe (adds probe point).
The snippet is injected into a gr.HTML component. Camera position persists
across rebuilds via localStorage so the view doesn't reset on each probe.
"""
from __future__ import annotations
import html as _html
import json
import numpy as np
MODEL_COLORS = ["#e6edf3", "#7c3aed", "#06b6d4", "#f59e0b", "#34d399", "#f472b6"]
_EMPTY = """
no embedding data — run export_viz to populate
"""
def build_canvas_page(viz: dict, coords3d: "np.ndarray | None",
probe_points: list[dict]) -> str:
"""Full standalone HTML page served by the /ofa-canvas FastAPI route."""
if coords3d is None or len(coords3d) == 0 or not viz.get("model_names"):
return (
""
f"{_EMPTY}"
)
fragment = build_umap_html(viz, coords3d, probe_points)
return (
""
""
"" + fragment + ""
)
def build_iframe_tag(ts: str = "0") -> str:
"""Iframe pointing to /ofa-canvas with cache-busting timestamp."""
return (
f''
)
def build_iframe_srcdoc(viz: dict, coords3d: "np.ndarray | None",
probe_points: list[dict]) -> str:
"""Embed the full standalone page in an iframe via srcdoc.
Scripts injected straight into gr.HTML never run: when Gradio sets
innerHTML, the HTML5 spec says
"""