Skip to content

Commit

Permalink
feat: allow users give project name (#35)
Browse files Browse the repository at this point in the history
* feat: allow users give project name

* iterdir -> rglob
  • Loading branch information
Jeff Yang committed Apr 3, 2021
1 parent ca80e3d commit 07b1115
Show file tree
Hide file tree
Showing 14 changed files with 27 additions and 26 deletions.
25 changes: 12 additions & 13 deletions app/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ 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.template_list = [p.stem for p in self.templates_dir.iterdir() if p.is_dir() and not p.stem.startswith("_")]
self.rendered_code = {t: {} for t in self.template_list}
self.rendered_code = {}
self.available_archive_formats = [x[0] for x in shutil.get_archive_formats()[::-1]]

def render_templates(self, template_name: str, config: dict):
def render_templates(self, template_name: str, project_name: str, config: dict):
"""Renders all the templates files from template folder for the given config."""
self.rendered_code[template_name] = {} # clean up the rendered code for given template
# loading the template files based on given template and from the _base folder
# since we are using some templates from _base folder
loader = FileSystemLoader([self.templates_dir / "_base", self.templates_dir / template_name])
Expand All @@ -27,11 +25,11 @@ def render_templates(self, template_name: str, config: dict):
)
for fname in env.list_templates(filter_func=lambda x: not x.startswith("_")):
code = env.get_template(fname).render(**config)
fname = fname.replace(".pyi", ".py")
self.rendered_code[template_name][fname] = code
fname = fname.replace(".pyi", ".py").replace(template_name, project_name)
self.rendered_code[fname] = code
yield fname, code

def make_and_write(self, template_name: str):
def make_and_write(self, template_name: str, project_name: str):
"""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 @@ -42,20 +40,21 @@ def make_and_write(self, template_name: str):
else:
p = template_name

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

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

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

self.make_and_write(template_name)
self.make_and_write(template_name, project_name)
archive_fname = shutil.make_archive(
base_name=template_name,
base_name=project_name,
root_dir=self.dist_dir,
format=archive_format,
base_dir=template_name,
base_dir=project_name,
)
return shutil.move(archive_fname, self.dist_dir / archive_fname.split("/")[-1])
8 changes: 5 additions & 3 deletions app/streamlit_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ def sidebar(self, template_list=None, config=None):
template_list = template_list or []
st.markdown("### Choose a Template")
self.template_name = st.selectbox("Available Templates are:", options=template_list)
self.project_name = st.text_input("Project Name:", "project_1")
self.template_name = FOLDER_TO_TEMPLATE_NAME[self.template_name]
with st.sidebar:
if self.template_name:
config = config(self.template_name)
self.config = config.get_configs()
self.config["project_name"] = self.project_name
else:
self.config = {}

Expand Down Expand Up @@ -120,7 +122,7 @@ def config(template_name):

def add_content(self):
"""Get generated/rendered code from the codegen."""
content = [*self.codegen.render_templates(self.template_name, self.config)]
content = [*self.codegen.render_templates(self.template_name, self.project_name, self.config)]
if st.checkbox("View rendered code ?", value=True):
for fname, code in content:
if len(code): # don't show files which don't have content in them
Expand All @@ -135,7 +137,7 @@ 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)
archive_fname = self.codegen.make_archive(self.template_name, self.project_name, archive_format)
# this is where streamlit serves static files
# ~/site-packages/streamlit/static/static/
dist_path = Path(st.__path__[0]) / "static/static/dist"
Expand All @@ -144,7 +146,7 @@ def add_download(self):
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))
self.render_directory(Path(self.codegen.dist_dir, self.project_name))

def run(self):
self.add_sidebar()
Expand Down
2 changes: 1 addition & 1 deletion templates/single/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def dependencies(fname):

setup(
# Metadata
name="single_cg",
name="{{project_name}}",
version=VERSION,
long_description_content_type="text/markdown",
long_description=readme,
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ from ignite.engine import Engine
from torch.cuda.amp import autocast
from torch.optim.optimizer import Optimizer

from single_cg.events import TrainEvents, train_events_to_attr
from {{project_name}}.events import TrainEvents, train_events_to_attr


# Edit below functions the way how the model will be training
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import ignite.distributed as idist
from ignite.engine.events import Events
from ignite.utils import manual_seed

from single_cg.engines import create_engines
from single_cg.events import TrainEvents
from single_cg.handlers import get_handlers, get_logger
from single_cg.utils import get_default_parser, setup_logging, log_metrics, log_basic_info, initialize, resume_from
from {{project_name}}.engines import create_engines
from {{project_name}}.events import TrainEvents
from {{project_name}}.handlers import get_handlers, get_logger
from {{project_name}}.utils import get_default_parser, setup_logging, log_metrics, log_basic_info, initialize, resume_from


def run(local_rank: int, config: Any, *args: Any, **kwargs: Any):
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import ignite.distributed as idist
import torch
from ignite.engine.engine import Engine
from single_cg.engines import create_engines, evaluate_function, train_function
from single_cg.events import TrainEvents, train_events_to_attr
from {{project_name}}.engines import create_engines, evaluate_function, train_function
from {{project_name}}.events import TrainEvents, train_events_to_attr
from torch import nn, optim


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from ignite.handlers.checkpoint import Checkpoint
from ignite.handlers.early_stopping import EarlyStopping
from ignite.handlers.timing import Timer
from single_cg.handlers import get_handlers, get_logger
from {{project_name}}.handlers import get_handlers, get_logger
from torch import nn, optim


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import torch
from ignite.engine import Engine
from ignite.utils import setup_logger
from single_cg.utils import (
from {{project_name}}.utils import (
get_default_parser,
hash_checkpoint,
log_metrics,
Expand Down

0 comments on commit 07b1115

Please sign in to comment.