diff --git a/README.md b/README.md index 2cccfe7..6bdbcd3 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,12 @@ By default, the name of the project folder created will be `pyscript-command-app `-o/--output` option can be used with the `-c/--command` option to configure name of the project folder as well as the name of the resulting HTML file. +By default, the content of `index.html` will be a copy of `templates/basic.html`, which is a jinja2 template. + +`-t/--template` option can be used to configure the name of the template file that will be used. Custom templates may be added to the `templates` folder and referenced using this option. + +When creating custom templates, make sure to include the pyscript `core.css` stylesheet and `core.js` module before invoking your python script. + ```shell $ pyscript create --wrap -c 'print("Hello World!")' -o ``` diff --git a/src/pyscript/_generator.py b/src/pyscript/_generator.py index 0489f84..0bffbfc 100644 --- a/src/pyscript/_generator.py +++ b/src/pyscript/_generator.py @@ -78,6 +78,7 @@ def create_project( wrap: bool = False, command: Optional[str] = None, output: Optional[str] = None, + template: Optional[str] = "basic.html", ) -> None: """ New files created: @@ -110,7 +111,7 @@ def create_project( pyscript_version = _get_latest_pyscript_version() if project_type == "app": - template = "basic.html" + project_template = template or "basic.html" else: raise ValueError( f"Unknown project type: {project_type}. Valid values are: 'app'" @@ -155,7 +156,7 @@ def create_project( config["project_config_filename"], output_path, pyscript_version=pyscript_version, - template=template, + template=project_template, ) diff --git a/src/pyscript/plugins/create.py b/src/pyscript/plugins/create.py index cbc896a..38f9478 100644 --- a/src/pyscript/plugins/create.py +++ b/src/pyscript/plugins/create.py @@ -42,6 +42,12 @@ def create( "--output", help="""Name of the resulting HTML output file. Meant to be used with `-w/--wrap`""", ), + template: Optional[str] = typer.Option( + "basic.html", + "-t", + "--template", + help="""Name of the Jinja2 template file to use. Meant to be used with `-w/--wrap`""", + ), ): """ Create a new pyscript project with the passed in name, creating a new @@ -78,6 +84,7 @@ def create( wrap, command, output, + template, ) except FileExistsError: raise cli.Abort( diff --git a/tests/test_generator.py b/tests/test_generator.py index c9c8c4a..3999e8d 100644 --- a/tests/test_generator.py +++ b/tests/test_generator.py @@ -104,6 +104,41 @@ def test_create_project_explicit_toml( check_project_manifest(manifest_path, toml, app_name, is_not_none) +def test_create_project_explicit_template( + tmp_cwd: Path, is_not_none: Any, monkeypatch +) -> None: + app_name = "TEMPLATE_app_name" + app_description = "A longer, human friendly, app description." + + # Let's set a custom template to use + project_template = "custom_template.html" + project_template_content = "dummy text for comparison" + project_template_dir = tmp_cwd / "templates" + project_template_dir.mkdir() + project_template_path = project_template_dir / project_template + project_template_path.write_text(project_template_content) + + # Let's monkeypatch the jinja2 template loader to use the temporary templates dir + monkeypatch.setattr( + gen._env.loader, "_template_root", str(project_template_dir.resolve()) + ) + + # GIVEN a new project + gen.create_project( + app_name, + app_description, + TESTS_AUTHOR_NAME, + TESTS_AUTHOR_EMAIL, + template=project_template, + ) + + # get the path where the config file is being created + manifest_path = tmp_cwd / app_name / config["project_config_filename"] + + check_project_manifest(manifest_path, toml, app_name, is_not_none) + assert (tmp_cwd / app_name / "index.html").read_text() == project_template_content + + def check_project_manifest( config_path: Path, serializer: Any, @@ -237,7 +272,7 @@ def check_plugin_project_files( assert dedent( f"""

Description

-

{ plugin_description }

+

{plugin_description}

""" ) assert f'' in contents