39 lines
1.9 KiB
Python
39 lines
1.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Checks a chart page made from template.html: python check_html.py chart.html
|
|
Prints OK or the first problem found (exit code 1)."""
|
|
import json
|
|
import re
|
|
import sys
|
|
|
|
def fail(msg: str) -> None:
|
|
print("ERROR: " + msg); sys.exit(1)
|
|
|
|
if len(sys.argv) != 2:
|
|
fail("usage: python check_html.py chart.html")
|
|
html = open(sys.argv[1], encoding="utf-8").read()
|
|
m = re.search(r'<script id="chart-data" type="application/json">(.*?)</script>', html, re.S)
|
|
if not m:
|
|
fail('JSON block <script id="chart-data" type="application/json"> is missing - start again from template.html')
|
|
try:
|
|
spec = json.loads(m.group(1), parse_constant=lambda c: fail(f"{c} is not valid JSON - use null"))
|
|
except json.JSONDecodeError as e:
|
|
fail(f"the JSON block is not valid JSON: {e}")
|
|
traces = spec.get("traces") if isinstance(spec, dict) else None
|
|
if not isinstance(traces, list) or not traces:
|
|
fail('"traces" is empty - the template placeholder was not replaced')
|
|
for i, t in enumerate(traces):
|
|
if not isinstance(t, dict) or "type" not in t:
|
|
fail(f'trace {i} needs a "type" (scatter, bar, histogram, box, pie, heatmap)')
|
|
data = [k for k in ("x", "y", "z", "values") if isinstance(t.get(k), list) and t[k]]
|
|
if not data:
|
|
fail(f"trace {i} ({t['type']}) contains no data (x / y / z / values)")
|
|
pairs = [("labels", "values")] if t["type"] == "pie" else [] if t["type"] == "heatmap" else [("x", "y")]
|
|
for a, b in pairs:
|
|
if isinstance(t.get(a), list) and isinstance(t.get(b), list) and len(t[a]) != len(t[b]):
|
|
fail(f"trace {i}: {a} has {len(t[a])} entries but {b} has {len(t[b])}")
|
|
if not (spec.get("layout") or {}).get("title"):
|
|
fail('layout has no "title"')
|
|
if "<title>Chart</title>" in html:
|
|
fail("the page <title> is still the template default 'Chart'")
|
|
print(f"OK: {len(traces)} trace(s): " + ", ".join(t["type"] for t in traces))
|