Skip to content

Commit 280bebd

Browse files
trsvchnydcjeff
andauthored
Use tmpdir for archiving ops (#68)
* Use tmpdir to store generated files * Remove unused imports * Fix tests * fix: create dist in advance for TemporaryDirectory Co-authored-by: ydcjeff <ydcjeff@outlook.com>
1 parent d184822 commit 280bebd

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

app/codegen.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class CodeGenerator:
1010
def __init__(self, templates_dir: str = "./templates", dist_dir: str = "./dist"):
1111
self.templates_dir = Path(templates_dir)
1212
self.dist_dir = Path(dist_dir)
13+
self.dist_dir.mkdir(parents=True, exist_ok=True)
1314
self.rendered_code = {}
1415
self.available_archive_formats = [x[0] for x in shutil.get_archive_formats()[::-1]]
1516

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

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

42-
if not (self.dist_dir / p).is_dir():
43-
(self.dist_dir / p).mkdir(parents=True, exist_ok=True)
43+
if not (dest_path / p).is_dir():
44+
(dest_path / p).mkdir(parents=True, exist_ok=True)
4445

4546
for fname, code in self.rendered_code.items():
46-
(self.dist_dir / template_name / fname).write_text(code)
47+
(dest_path / template_name / fname).write_text(code)
4748

48-
def make_archive(self, template_name, archive_format):
49+
def make_archive(self, template_name, archive_format, dest_path):
4950
"""Creates dist dir with generated code, then makes the archive."""
50-
51-
self.make_and_write(template_name)
51+
self.make_and_write(template_name, dest_path)
5252
archive_fname = shutil.make_archive(
5353
base_name=template_name,
54-
root_dir=self.dist_dir,
54+
root_dir=dest_path,
5555
format=archive_format,
5656
base_dir=template_name,
5757
)
58-
return shutil.move(archive_fname, self.dist_dir / archive_fname.split("/")[-1])
58+
archive_fname = shutil.move(archive_fname, dest_path / archive_fname.split("/")[-1])
59+
return archive_fname

app/streamlit_app.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import shutil
2+
import tempfile
23
from pathlib import Path
34

45
import streamlit as st
@@ -135,16 +136,22 @@ def add_download(self):
135136
# https://github.com/streamlit/streamlit/issues/400
136137
# https://github.com/streamlit/streamlit/issues/400#issuecomment-648580840
137138
if st.button("Generate an archive"):
138-
archive_fname = self.codegen.make_archive(self.template_name, archive_format)
139-
# this is where streamlit serves static files
140-
# ~/site-packages/streamlit/static/static/
141-
dist_path = Path(st.__path__[0]) / "static/static/dist"
142-
if not dist_path.is_dir():
143-
dist_path.mkdir()
144-
shutil.copy(archive_fname, dist_path)
145-
st.success(f"Download link : [{archive_fname}](./static/{archive_fname})")
146-
with col2:
147-
self.render_directory(Path(self.codegen.dist_dir, self.template_name))
139+
with tempfile.TemporaryDirectory(prefix="", dir=self.codegen.dist_dir) as tmp_dir:
140+
tmp_dir = Path(tmp_dir)
141+
142+
archive_fname = self.codegen.make_archive(self.template_name, archive_format, tmp_dir)
143+
# this is where streamlit serves static files
144+
# ~/site-packages/streamlit/static/static/
145+
dist_path = Path(st.__path__[0]) / "static/static" / tmp_dir
146+
147+
if not dist_path.is_dir():
148+
dist_path.mkdir(parents=True, exist_ok=True)
149+
150+
shutil.copy(archive_fname, dist_path)
151+
st.success(f"Download link : [{archive_fname}](./static/{archive_fname})")
152+
153+
with col2:
154+
self.render_directory(Path(tmp_dir, self.template_name))
148155

149156
def run(self):
150157
self.add_sidebar()

tests/generate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def generate():
1616
configs = import_from_file("template_config", f"./templates/{p.stem}/_sidebar.py").get_configs()
1717
code_gen = CodeGenerator(dist_dir=dist_dir)
1818
[*code_gen.render_templates(p.stem, configs)]
19-
code_gen.make_and_write(p.stem)
19+
code_gen.make_and_write(p.stem, Path(dist_dir))
2020

2121

2222
if __name__ == "__main__":

0 commit comments

Comments
 (0)