From 570c5d0d50c086cefb84008ef7d76829283eb1d9 Mon Sep 17 00:00:00 2001 From: Josh Wiedemeier Date: Wed, 22 May 2024 12:50:34 -0400 Subject: [PATCH 1/5] add --template option to wrap subcommand --- src/pyscript/_generator.py | 5 +++-- src/pyscript/plugins/create.py | 7 +++++++ 2 files changed, 10 insertions(+), 2 deletions(-) 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( From cad8dd665158e673991dcefd3506803630f6446c Mon Sep 17 00:00:00 2001 From: Josh Wiedemeier Date: Wed, 22 May 2024 12:51:10 -0400 Subject: [PATCH 2/5] unit test for --template option --- tests/test_generator.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/test_generator.py b/tests/test_generator.py index c9c8c4a..8444d48 100644 --- a/tests/test_generator.py +++ b/tests/test_generator.py @@ -12,6 +12,8 @@ import pytest import toml +import jinja2 + from pyscript import _generator as gen from pyscript import config @@ -103,6 +105,31 @@ 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, From cadc3ef38f36a5dfba311f95462a9075269dceb2 Mon Sep 17 00:00:00 2001 From: Josh Wiedemeier Date: Wed, 22 May 2024 13:02:56 -0400 Subject: [PATCH 3/5] add documentation for --template option --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) 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 ``` From 3ef04385a379b6e6add5345e2442f1be5f8ea0c3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 22 May 2024 17:10:50 +0000 Subject: [PATCH 4/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_generator.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/tests/test_generator.py b/tests/test_generator.py index 8444d48..0ce027b 100644 --- a/tests/test_generator.py +++ b/tests/test_generator.py @@ -9,11 +9,10 @@ from textwrap import dedent from typing import Any +import jinja2 import pytest import toml -import jinja2 - from pyscript import _generator as gen from pyscript import config @@ -105,6 +104,7 @@ 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: @@ -120,10 +120,18 @@ def test_create_project_explicit_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())) + 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) + 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"] @@ -131,6 +139,7 @@ def test_create_project_explicit_template( 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, From 8fa9f9cd8552ad0335170a9f707012ed1cd679db Mon Sep 17 00:00:00 2001 From: Josh Wiedemeier Date: Wed, 22 May 2024 13:15:27 -0400 Subject: [PATCH 5/5] nits to appease precommit ci --- tests/test_generator.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_generator.py b/tests/test_generator.py index 0ce027b..3999e8d 100644 --- a/tests/test_generator.py +++ b/tests/test_generator.py @@ -9,7 +9,6 @@ from textwrap import dedent from typing import Any -import jinja2 import pytest import toml @@ -273,7 +272,7 @@ def check_plugin_project_files( assert dedent( f"""

Description

-

{ plugin_description }

+

{plugin_description}

""" ) assert f'' in contents