|
9 | 9 | from pyscript import config |
10 | 10 |
|
11 | 11 | _env = jinja2.Environment(loader=jinja2.PackageLoader("pyscript")) |
| 12 | +TEMPLATE_PYTHON_CODE = """# Replace the code below with your own |
| 13 | +print("Hello, world!") |
| 14 | +""" |
12 | 15 |
|
13 | 16 |
|
14 | | -def string_to_html(input_str: str, title: str, output_path: Path) -> None: |
| 17 | +def create_project_html( |
| 18 | + title: str, python_file_path: str, config_file_path: str, output_file_path: Path |
| 19 | +) -> None: |
15 | 20 | """Write a Python script string to an HTML file template.""" |
16 | 21 | template = _env.get_template("basic.html") |
| 22 | + with output_file_path.open("w") as fp: |
| 23 | + fp.write( |
| 24 | + template.render( |
| 25 | + python_file_path=python_file_path, |
| 26 | + config_file_path=config_file_path, |
| 27 | + title=title, |
| 28 | + ) |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +def save_config_file(config_file: Path, configuration: dict): |
| 33 | + """Write an app configuration dict to `config_file`. |
| 34 | +
|
| 35 | + Params: |
| 36 | +
|
| 37 | + - config_file(Path): path configuration file. (I.e.: "pyscript.toml"). Supported |
| 38 | + formats: `toml` and `json`. |
| 39 | + - configuration(dict): app configuration to be saved |
| 40 | +
|
| 41 | + Return: |
| 42 | + (None) |
| 43 | + """ |
| 44 | + with config_file.open("w", encoding="utf-8") as fp: |
| 45 | + if str(config_file).endswith(".json"): |
| 46 | + json.dump(configuration, fp) |
| 47 | + else: |
| 48 | + toml.dump(configuration, fp) |
| 49 | + |
| 50 | + |
| 51 | +def string_to_html( |
| 52 | + code: str, title: str, output_path: Path, template_name: str = "basic.html" |
| 53 | +) -> None: |
| 54 | + """Write a Python script string to an HTML file template. |
| 55 | +
|
| 56 | + Params: |
| 57 | +
|
| 58 | + - code(str): string containing the application code to be written to the |
| 59 | + PyScript app template |
| 60 | + - title(str): application title, that will be placed as title of the html |
| 61 | + app template |
| 62 | + - template_name(str): name of the template to be used |
| 63 | +
|
| 64 | + Output: |
| 65 | + (None) |
| 66 | + """ |
| 67 | + template = _env.get_template(template_name) |
17 | 68 | with output_path.open("w") as fp: |
18 | | - fp.write(template.render(code=input_str, title=title)) |
| 69 | + fp.write(template.render(code=code, title=title)) |
19 | 70 |
|
20 | 71 |
|
21 | | -def file_to_html(input_path: Path, title: str, output_path: Optional[Path]) -> None: |
| 72 | +def file_to_html( |
| 73 | + input_path: Path, |
| 74 | + title: str, |
| 75 | + output_path: Optional[Path], |
| 76 | + template_name: str = "basic.html", |
| 77 | +) -> None: |
22 | 78 | """Write a Python script string to an HTML file template.""" |
23 | 79 | output_path = output_path or input_path.with_suffix(".html") |
24 | 80 | with input_path.open("r") as fp: |
25 | | - string_to_html(fp.read(), title, output_path) |
| 81 | + string_to_html(fp.read(), title, output_path, template_name) |
26 | 82 |
|
27 | 83 |
|
28 | 84 | def create_project( |
@@ -57,4 +113,15 @@ def create_project( |
57 | 113 | toml.dump(context, fp) |
58 | 114 |
|
59 | 115 | index_file = app_dir / "index.html" |
60 | | - string_to_html('print("Hello, world!")', app_name, index_file) |
| 116 | + |
| 117 | + # Save the new python file |
| 118 | + python_filepath = app_dir / "main.py" |
| 119 | + with python_filepath.open("w", encoding="utf-8") as fp: |
| 120 | + fp.write(TEMPLATE_PYTHON_CODE) |
| 121 | + |
| 122 | + create_project_html( |
| 123 | + app_name, |
| 124 | + config["project_main_filename"], |
| 125 | + config["project_config_filename"], |
| 126 | + index_file, |
| 127 | + ) |
0 commit comments