SchemID / derived /Source /scripts /generate_visual_table.py
lsnu's picture
Add files using upload-large-folder tool
9c0afa4 verified
#!/usr/bin/env python3
from __future__ import annotations
import csv
import html
import json
import os
from pathlib import Path
REPO_ROOT = Path("/workspace/SchemID")
SOURCE_ROOT = REPO_ROOT / "derived" / "Source"
ROWS_PATH = SOURCE_ROOT / "metadata" / "rows.jsonl"
VIS_ROOT = SOURCE_ROOT / "visuals"
CSV_PATH = VIS_ROOT / "four_column_table.csv"
HTML_PATH = VIS_ROOT / "four_column_table.html"
def rel_to_visuals(path: Path) -> str:
return os.path.relpath(path, VIS_ROOT)
def image_rel(path_str: str) -> str:
path = Path(path_str)
return rel_to_visuals(path)
def main() -> None:
VIS_ROOT.mkdir(parents=True, exist_ok=True)
rows: list[dict[str, str]] = []
with ROWS_PATH.open() as handle:
for line in handle:
raw = json.loads(line)
source_payload = json.loads(raw["Source"])
original_path = Path(raw["Circuit generated image"])
no_text_path = Path(raw["Circuit Generated image with no text"])
eps_path = SOURCE_ROOT / source_payload["eps_path"]
row = {
"Circuit Generation Code": raw["Circuit Generation Code"],
"Circuit generated image": image_rel(str(original_path)),
"Circuit Generated image with no text": image_rel(str(no_text_path)),
"Source": json.dumps(source_payload, sort_keys=True),
"Code File": rel_to_visuals(eps_path),
}
rows.append(row)
with CSV_PATH.open("w", newline="") as handle:
writer = csv.DictWriter(
handle,
fieldnames=[
"Circuit Generation Code",
"Circuit generated image",
"Circuit Generated image with no text",
"Source",
"Code File",
],
)
writer.writeheader()
writer.writerows(rows)
parts = [
"<!doctype html>",
"<html lang='en'>",
"<head>",
"<meta charset='utf-8'>",
"<meta name='viewport' content='width=device-width, initial-scale=1'>",
"<title>SchemID 4-Column Circuit Table</title>",
"<style>",
"body { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; margin: 0; background: #f5f5f5; color: #111; }",
".wrap { padding: 16px; }",
"h1 { margin: 0 0 8px; font-size: 24px; }",
"p { margin: 0 0 12px; }",
"a { color: #0b57d0; }",
".meta { margin-bottom: 16px; }",
"table { border-collapse: collapse; width: 100%; background: #fff; table-layout: fixed; }",
"thead th { position: sticky; top: 0; background: #ececec; z-index: 1; }",
"th, td { border: 1px solid #ccc; padding: 8px; vertical-align: top; }",
"th:nth-child(1), td:nth-child(1) { width: 38%; }",
"th:nth-child(2), td:nth-child(2) { width: 18%; }",
"th:nth-child(3), td:nth-child(3) { width: 18%; }",
"th:nth-child(4), td:nth-child(4) { width: 26%; }",
"pre { margin: 0; white-space: pre-wrap; word-break: break-word; max-height: 360px; overflow: auto; }",
".imgbox { background: #fff; min-height: 120px; display: flex; align-items: center; justify-content: center; }",
"img { max-width: 100%; height: auto; border: 1px solid #ddd; background: #fff; }",
".source { max-height: 360px; overflow: auto; white-space: pre-wrap; word-break: break-word; }",
".small { font-size: 12px; color: #555; margin-top: 6px; }",
"</style>",
"</head>",
"<body>",
"<div class='wrap'>",
"<h1>SchemID 4-Column Circuit Table</h1>",
f"<p class='meta'>Rows: {len(rows)}. CSV companion: <a href='four_column_table.csv'>four_column_table.csv</a>.</p>",
"<table>",
"<thead><tr><th>Circuit Generation Code</th><th>Circuit generated image</th><th>Circuit Generated image with no text</th><th>Source</th></tr></thead>",
"<tbody>",
]
for row in rows:
code_text = html.escape(row["Circuit Generation Code"])
source_text = html.escape(row["Source"])
code_file = html.escape(row["Code File"])
original_rel = html.escape(row["Circuit generated image"])
no_text_rel = html.escape(row["Circuit Generated image with no text"])
parts.extend(
[
"<tr>",
(
"<td>"
f"<pre>{code_text}</pre>"
f"<div class='small'>File: <a href='{code_file}'>{code_file}</a></div>"
"</td>"
),
(
"<td>"
f"<div class='imgbox'><a href='{original_rel}'><img loading='lazy' src='{original_rel}' alt='original circuit'></a></div>"
f"<div class='small'><a href='{original_rel}'>{original_rel}</a></div>"
"</td>"
),
(
"<td>"
f"<div class='imgbox'><a href='{no_text_rel}'><img loading='lazy' src='{no_text_rel}' alt='no-text circuit'></a></div>"
f"<div class='small'><a href='{no_text_rel}'>{no_text_rel}</a></div>"
"</td>"
),
f"<td><div class='source'>{source_text}</div></td>",
"</tr>",
]
)
parts.extend(
[
"</tbody>",
"</table>",
"</div>",
"</body>",
"</html>",
]
)
HTML_PATH.write_text("\n".join(parts))
if __name__ == "__main__":
main()