Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --template option to pyscript create #150

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <output_filename.html>
```
Expand Down
5 changes: 3 additions & 2 deletions src/pyscript/_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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'"
Expand Down Expand Up @@ -155,7 +156,7 @@ def create_project(
config["project_config_filename"],
output_path,
pyscript_version=pyscript_version,
template=template,
template=project_template,
)


Expand Down
7 changes: 7 additions & 0 deletions src/pyscript/plugins/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -78,6 +84,7 @@ def create(
wrap,
command,
output,
template,
)
except FileExistsError:
raise cli.Abort(
Expand Down
37 changes: 36 additions & 1 deletion tests/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -237,7 +272,7 @@ def check_plugin_project_files(
assert dedent(
f""" <div>
<h2> Description </h2>
<p>{ plugin_description }</p>
<p>{plugin_description}</p>
</div>"""
)
assert f'<py-script src="./{python_file}">' in contents
Expand Down