-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_static.py
More file actions
47 lines (38 loc) · 1.22 KB
/
build_static.py
File metadata and controls
47 lines (38 loc) · 1.22 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
"""Pre-render Flask templates to static HTML for deploy."""
import shutil
from pathlib import Path
from app import app
ROOT = Path(__file__).parent
DIST = ROOT / "dist"
def main():
# Clean
if DIST.exists():
shutil.rmtree(DIST)
DIST.mkdir()
with app.test_client() as client:
# Render each page
for url, out in [
("/", "index.html"),
("/criar", "criar.html"),
("/tudo", "tudo.html"),
]:
r = client.get(url)
assert r.status_code == 200, f"{url} -> {r.status_code}"
(DIST / out).write_bytes(r.data)
print(f" {url} -> {out} ({len(r.data)} bytes)")
# Copy data (JSON endpoints become static files)
data_out = DIST / "data"
data_out.mkdir()
for fn in ["catalog.json", "everything.json"]:
src = ROOT / "data" / fn
if src.exists():
shutil.copy2(src, data_out / fn)
print(f" data/{fn}")
# Copy static assets (images)
static_out = DIST / "static"
shutil.copytree(ROOT / "static", static_out)
n = sum(1 for _ in static_out.rglob("*.png"))
print(f" static/ ({n} PNGs)")
print(f"\ndist/ ready: {DIST}")
if __name__ == "__main__":
main()