| """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>""" |
|
|