Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions app/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class CodeGenerator:
def __init__(self, templates_dir: str = "./templates", dist_dir: str = "./dist"):
self.templates_dir = Path(templates_dir)
self.dist_dir = Path(dist_dir)
self.dist_dir.mkdir(parents=True, exist_ok=True)
self.rendered_code = {}
self.available_archive_formats = [x[0] for x in shutil.get_archive_formats()[::-1]]

Expand All @@ -28,7 +29,7 @@ def render_templates(self, template_name: str, config: dict):
self.rendered_code[fname] = code
yield fname, code

def make_and_write(self, template_name: str):
def make_and_write(self, template_name: str, dest_path: Path):
"""Make the directories first and write to the files"""
for p in (self.templates_dir / template_name).rglob("*"):
if not p.stem.startswith("_") and p.is_dir():
Expand All @@ -39,20 +40,20 @@ def make_and_write(self, template_name: str):
else:
p = template_name

if not (self.dist_dir / p).is_dir():
(self.dist_dir / p).mkdir(parents=True, exist_ok=True)
if not (dest_path / p).is_dir():
(dest_path / p).mkdir(parents=True, exist_ok=True)

for fname, code in self.rendered_code.items():
(self.dist_dir / template_name / fname).write_text(code)
(dest_path / template_name / fname).write_text(code)

def make_archive(self, template_name, archive_format):
def make_archive(self, template_name, archive_format, dest_path):
"""Creates dist dir with generated code, then makes the archive."""

self.make_and_write(template_name)
self.make_and_write(template_name, dest_path)
archive_fname = shutil.make_archive(
base_name=template_name,
root_dir=self.dist_dir,
root_dir=dest_path,
format=archive_format,
base_dir=template_name,
)
return shutil.move(archive_fname, self.dist_dir / archive_fname.split("/")[-1])
archive_fname = shutil.move(archive_fname, dest_path / archive_fname.split("/")[-1])
return archive_fname
27 changes: 17 additions & 10 deletions app/streamlit_app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import shutil
import tempfile
from pathlib import Path

import streamlit as st
Expand Down Expand Up @@ -135,16 +136,22 @@ def add_download(self):
# https://github.com/streamlit/streamlit/issues/400
# https://github.com/streamlit/streamlit/issues/400#issuecomment-648580840
if st.button("Generate an archive"):
archive_fname = self.codegen.make_archive(self.template_name, archive_format)
# this is where streamlit serves static files
# ~/site-packages/streamlit/static/static/
dist_path = Path(st.__path__[0]) / "static/static/dist"
if not dist_path.is_dir():
dist_path.mkdir()
shutil.copy(archive_fname, dist_path)
st.success(f"Download link : [{archive_fname}](./static/{archive_fname})")
with col2:
self.render_directory(Path(self.codegen.dist_dir, self.template_name))
with tempfile.TemporaryDirectory(prefix="", dir=self.codegen.dist_dir) as tmp_dir:
tmp_dir = Path(tmp_dir)

archive_fname = self.codegen.make_archive(self.template_name, archive_format, tmp_dir)
# this is where streamlit serves static files
# ~/site-packages/streamlit/static/static/
dist_path = Path(st.__path__[0]) / "static/static" / tmp_dir

if not dist_path.is_dir():
dist_path.mkdir(parents=True, exist_ok=True)

shutil.copy(archive_fname, dist_path)
st.success(f"Download link : [{archive_fname}](./static/{archive_fname})")

with col2:
self.render_directory(Path(tmp_dir, self.template_name))

def run(self):
self.add_sidebar()
Expand Down
2 changes: 1 addition & 1 deletion tests/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def generate():
configs = import_from_file("template_config", f"./templates/{p.stem}/_sidebar.py").get_configs()
code_gen = CodeGenerator(dist_dir=dist_dir)
[*code_gen.render_templates(p.stem, configs)]
code_gen.make_and_write(p.stem)
code_gen.make_and_write(p.stem, Path(dist_dir))


if __name__ == "__main__":
Expand Down