-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_site.py
More file actions
50 lines (38 loc) · 1.54 KB
/
Copy pathbuild_site.py
File metadata and controls
50 lines (38 loc) · 1.54 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
"""Assemble the static GitHub Pages site into ``_site/``.
The deployed app is the *same* source that runs locally: the Python modules
are copied verbatim next to ``index.html`` and executed in the browser by
stlite (Streamlit compiled to WebAssembly). Nothing is duplicated or forked.
Usage::
python scripts/build_site.py # writes ./_site
python -m http.server -d _site 8000 # preview at localhost:8000
"""
import json
import shutil
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
OUT = ROOT / "_site"
TEMPLATE = ROOT / "web" / "index.html"
#: Python modules shipped to the browser. The first entry is the entrypoint.
APP_FILES = ["app.py", "core_logic.py", "schemas.py", "storage.py"]
PLACEHOLDER = "__APP_FILES__"
def build(out: Path = OUT) -> Path:
"""Build the site into ``out`` (replacing it) and return the path."""
if out.exists():
shutil.rmtree(out)
(out / "app").mkdir(parents=True)
for name in APP_FILES:
shutil.copy2(ROOT / name, out / "app" / name)
html = TEMPLATE.read_text(encoding="utf-8")
if PLACEHOLDER not in html:
raise RuntimeError(f"{TEMPLATE} is missing {PLACEHOLDER}")
(out / "index.html").write_text(
html.replace(PLACEHOLDER, json.dumps(APP_FILES)), encoding="utf-8"
)
# Tell GitHub Pages to serve files as-is (no Jekyll processing).
(out / ".nojekyll").touch()
return out
if __name__ == "__main__":
target = build()
print(f"Built {target.relative_to(ROOT)}/ ({len(APP_FILES)} modules)")
sys.exit(0)