File size: 5,597 Bytes
9c0afa4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/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()