-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_html.py
More file actions
109 lines (90 loc) · 3.1 KB
/
generate_html.py
File metadata and controls
109 lines (90 loc) · 3.1 KB
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
#!/usr/bin/env python3
"""Generate HTML gallery for a timestamped folder and update index.html."""
from __future__ import annotations
import argparse
from pathlib import Path
from datetime import datetime
def generate_gallery(outdir: Path) -> Path:
pngs = sorted(outdir.glob("*.png"))
title = f"Figures: {outdir.name}"
html = [
"<!DOCTYPE html>",
"<html>",
"<head>",
" <meta charset='utf-8'>",
f" <title>{title}</title>",
" <style>",
" body { font-family: Arial, sans-serif; margin: 20px; }",
" .grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(360px, 1fr)); gap: 16px; }",
" figure { margin: 0; padding: 8px; border: 1px solid #ddd; border-radius: 8px; }",
" img { width: 100%; height: auto; }",
" figcaption { margin-top: 6px; font-size: 0.9em; color: #333; }",
" </style>",
"</head>",
"<body>",
f" <h1>{title}</h1>",
" <div class='grid'>",
]
for png in pngs:
html.extend([
" <figure>",
f" <img src='{png.name}' alt='{png.name}'>",
f" <figcaption>{png.name}</figcaption>",
" </figure>",
])
html.extend([
" </div>",
"</body>",
"</html>",
])
out_path = outdir / "index.html"
out_path.write_text("\n".join(html), encoding="utf-8")
return out_path
def generate_root_index(fig_root: Path) -> Path:
folders = sorted([p for p in fig_root.iterdir() if p.is_dir()], reverse=True)
html = [
"<!DOCTYPE html>",
"<html>",
"<head>",
" <meta charset='utf-8'>",
" <title>Figure Index</title>",
" <style>",
" body { font-family: Arial, sans-serif; margin: 20px; }",
" ul { line-height: 1.8; }",
" </style>",
"</head>",
"<body>",
" <h1>Figure Index</h1>",
" <ul>",
]
for folder in folders:
try:
ts = datetime.strptime(folder.name, "%Y%m%d_%H%M%S")
label = ts.strftime("%Y-%m-%d %H:%M:%S")
except ValueError:
label = folder.name
html.append(f" <li><a href='{folder.name}/index.html'>{label}</a></li>")
html.extend([
" </ul>",
"</body>",
"</html>",
])
out_path = fig_root / "index.html"
out_path.write_text("\n".join(html), encoding="utf-8")
return out_path
def main():
parser = argparse.ArgumentParser(description="Generate HTML for figures")
parser.add_argument("--outdir", required=True, help="Timestamped folder with PNGs")
parser.add_argument("--index", action="store_true", help="Update root index.html")
args = parser.parse_args()
outdir = Path(args.outdir).resolve()
if not outdir.exists():
raise SystemExit(f"Outdir not found: {outdir}")
gallery = generate_gallery(outdir)
print(f"Wrote {gallery}")
if args.index:
fig_root = outdir.parent
index = generate_root_index(fig_root)
print(f"Wrote {index}")
if __name__ == "__main__":
main()