File size: 1,251 Bytes
d171350
 
fa8dc8b
 
d171350
 
fa8dc8b
d171350
fa8dc8b
 
 
d171350
 
 
 
fa8dc8b
 
 
 
89118b9
fa8dc8b
 
 
89118b9
 
 
fa8dc8b
89118b9
 
 
 
 
fa8dc8b
89118b9
 
 
fa8dc8b
 
 
 
d171350
89118b9
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""Build the HTML/JS/CSS for the chart visualizer.

Reads static/visualizer.js and injects chart data via window.CHART_DATA.
Returns an iframe with srcdoc for Chrome/Arc compatibility.
"""

import html
import json
from pathlib import Path

_JS_PATH = Path(__file__).parent / "static" / "visualizer.js"


def build_visualizer_html(chart_json: dict) -> str:
    data_json = json.dumps(chart_json, separators=(",", ":"))
    js_code = _JS_PATH.read_text(encoding="utf-8")

    full_html = _TEMPLATE.replace("__JS_CODE__", js_code).replace("__CHART_DATA__", data_json)
    escaped = html.escape(full_html, quote=True)
    return (
        f'<iframe srcdoc="{escaped}" '
        f'style="width:100%; height:75vw; max-height:780px; min-height:480px; border:none; border-radius:12px;" '
        f'allow="autoplay" sandbox="allow-scripts"></iframe>'
    )


_TEMPLATE = """<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
  * { margin: 0; padding: 0; box-sizing: border-box; }
  body { font-family: system-ui, -apple-system, sans-serif; background: #0a0a0a; overflow: hidden; }
</style>
</head>
<body>
<div id="midmid-viz"></div>
<script>window.CHART_DATA = __CHART_DATA__;</script>
<script type="module">
__JS_CODE__
</script>
</body>
</html>"""