From 0666ba93f51fe12e67938a41ef99a239c44bc29e Mon Sep 17 00:00:00 2001 From: Bernardo Fontes Date: Sun, 31 Oct 2021 13:25:21 -0300 Subject: [PATCH 01/11] Fix argument name --- pyp5js/cli.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyp5js/cli.py b/pyp5js/cli.py index ae4647d8..078c8ca8 100755 --- a/pyp5js/cli.py +++ b/pyp5js/cli.py @@ -26,12 +26,12 @@ def command_line_entrypoint(): @click.option('--interpreter', '-i', type=click.Choice(AVAILABLE_INTERPRETERS), default=PYODIDE_INTERPRETER, help='Which python tool to use to run the sketch. (defaults to pyodide)') @click.option('--template', '-t', type=click.Path(exists=True), help='Specify a custom index.html template to use.') @click.option('--cdn/--local', default=True) -def configure_new_sketch(sketch_name, monitor, interpreter, template, use_cdn): +def configure_new_sketch(sketch_name, monitor, interpreter, template, cdn): """ Create dir and configure boilerplate - Example:\n $ pyp5js new my_sketch -i pyodide """ - files = commands.new_sketch(sketch_name, interpreter, template_file=template, use_cdn=use_cdn) + files = commands.new_sketch(sketch_name, interpreter, template_file=template, use_cdn=cdn) cprint.ok(f"Your sketch was created!") From a92259a3433541a85476f8318e1ac3f2655f96db Mon Sep 17 00:00:00 2001 From: Bernardo Fontes Date: Sun, 31 Oct 2021 13:28:28 -0300 Subject: [PATCH 02/11] Fix wrong comment syntax --- pyp5js/templates/pyodide/target_sketch.js.template | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyp5js/templates/pyodide/target_sketch.js.template b/pyp5js/templates/pyodide/target_sketch.js.template index 610bc21d..3c26fc24 100644 --- a/pyp5js/templates/pyodide/target_sketch.js.template +++ b/pyp5js/templates/pyodide/target_sketch.js.template @@ -1674,7 +1674,7 @@ function runCode() { async function main() { const config = { - indexURL : "/static/js/pyodide/", # TODO: replace hardcoded string by {{ indexURL }} + indexURL : "/static/js/pyodide/", // TODO: replace hardcoded string by {{ indexURL }} fullStdLib: false, } window.pyodide = await loadPyodide(config); From cebef7987747d1989aaee1c5e70aedc0fd36adbf Mon Sep 17 00:00:00 2001 From: Bernardo Fontes Date: Mon, 1 Nov 2021 12:03:26 -0300 Subject: [PATCH 03/11] Render pyodide sketch index.html reading pyodide URL from config --- pyp5js/config/sketch.py | 9 ++++-- pyp5js/sketch.py | 4 +-- pyp5js/templates/pyodide/index.html | 2 +- pyp5js/templates_renderers.py | 1 + pyp5js/tests/fixtures.py | 8 +++++ pyp5js/tests/test_config/test_sketch.py | 8 +++-- pyp5js/tests/test_sketch.py | 4 ++- pyp5js/tests/test_templates_renderers.py | 39 ++++++++++++++++++------ 8 files changed, 56 insertions(+), 19 deletions(-) diff --git a/pyp5js/config/sketch.py b/pyp5js/config/sketch.py index e1bccc69..e98fe76f 100644 --- a/pyp5js/config/sketch.py +++ b/pyp5js/config/sketch.py @@ -6,6 +6,8 @@ TRANSCRYPT_INTERPRETER = 'transcrypt' PYODIDE_INTERPRETER = 'pyodide' P5_JS_CDN = 'https://cdn.jsdelivr.net/npm/p5@1.4.0/lib/p5.min.js' +PYODIDE_JS_CDN = 'https://cdn.jsdelivr.net/pyodide/v0.18.1/full/pyodide.js' + class SketchConfig: @@ -19,6 +21,7 @@ def __init__(self, interpreter, **kwargs): self.interpreter = interpreter self.index_template = kwargs.get("index_template", "") self.p5_js_url = kwargs.get("p5_js_url", P5_JS_CDN) + self.pyodide_js_url = kwargs.get("pyodide_js_url", PYODIDE_JS_CDN) @property def index_template_path(self): @@ -31,10 +34,12 @@ def write(self, fname): with open(fname, "w") as fd: data = { "interpreter": self.interpreter, - "index_template": index_template, - # TODO: also store pyodide_js_url "p5_js_url": self.p5_js_url, } + if self.index_template: + data.update({"index_template": index_template}) + if self.is_pyodide: + data.update({"pyodide_js_url": self.pyodide_js_url}) json.dump(data, fd) @property diff --git a/pyp5js/sketch.py b/pyp5js/sketch.py index da7a9b19..42d1e380 100644 --- a/pyp5js/sketch.py +++ b/pyp5js/sketch.py @@ -7,7 +7,7 @@ from pyp5js.exceptions import SketchDirAlreadyExistException, InvalidName -SketchUrls = namedtuple('SketchUrls', ['p5_js_url', 'sketch_js_url']) +SketchUrls = namedtuple('SketchUrls', ['p5_js_url', 'pyodide_js_url', 'sketch_js_url']) class Sketch: @@ -109,7 +109,7 @@ def __eq__(self, other): @property def urls(self): return SketchUrls( - # TODO: add pyodide_js_url p5_js_url=self.config.p5_js_url, + pyodide_js_url=self.config.pyodide_js_url, sketch_js_url=f"{self.TARGET_NAME}/target_sketch.js", ) diff --git a/pyp5js/templates/pyodide/index.html b/pyp5js/templates/pyodide/index.html index 3c64cfca..3cce84d8 100644 --- a/pyp5js/templates/pyodide/index.html +++ b/pyp5js/templates/pyodide/index.html @@ -9,7 +9,7 @@ {{ sketch_name }} - pyp5js (using pyodide) - + diff --git a/pyp5js/templates_renderers.py b/pyp5js/templates_renderers.py index 07ca2606..645acdcf 100644 --- a/pyp5js/templates_renderers.py +++ b/pyp5js/templates_renderers.py @@ -20,6 +20,7 @@ def get_sketch_index_content(sketch): context = { "sketch_name": sketch.sketch_name, "p5_js_url": sketch.urls.p5_js_url, + "pyodide_js_url": sketch.urls.pyodide_js_url, "sketch_js_url": sketch.urls.sketch_js_url, "sketch_content": sketch.sketch_content, } diff --git a/pyp5js/tests/fixtures.py b/pyp5js/tests/fixtures.py index ced00d47..37ec5170 100644 --- a/pyp5js/tests/fixtures.py +++ b/pyp5js/tests/fixtures.py @@ -23,6 +23,14 @@ def sketch(): yield files shutil.rmtree(SKETCHBOOK_DIR) + +@fixture() +def sketch_pyodide(): + files = Sketch('foo', interpreter=PYODIDE_INTERPRETER) + files.create_sketch_dir() + yield files + shutil.rmtree(SKETCHBOOK_DIR) + @fixture def transcrypt_json_file(): try: diff --git a/pyp5js/tests/test_config/test_sketch.py b/pyp5js/tests/test_config/test_sketch.py index 9eaea4f1..4ca2ef0a 100644 --- a/pyp5js/tests/test_config/test_sketch.py +++ b/pyp5js/tests/test_config/test_sketch.py @@ -4,7 +4,7 @@ from tempfile import NamedTemporaryFile from pyp5js.config import TRANSCRYPT_INTERPRETER, PYODIDE_INTERPRETER -from pyp5js.config.sketch import SketchConfig, P5_JS_CDN +from pyp5js.config.sketch import SketchConfig, P5_JS_CDN, PYODIDE_JS_CDN from pyp5js.config.fs import PYP5JS_FILES from ..fixtures import transcrypt_json_file, pyodide_json_file, transcrypt_config, pyodide_config, custom_index_json_file @@ -36,7 +36,7 @@ def test_write_sketch_interpreter_config(custom_index_json_file): assert data == expected -def test_wrrite_defaults(): +def test_write_defaults(): config = SketchConfig(PYODIDE_INTERPRETER) fd = NamedTemporaryFile(mode="w", delete=False) config.write(fd.name) @@ -44,7 +44,9 @@ def test_wrrite_defaults(): with open(fd.name) as fd: data = json.load(fd) - assert "" == data["index_template"] + assert PYODIDE_INTERPRETER == data["interpreter"] + assert P5_JS_CDN == data["p5_js_url"] + assert PYODIDE_JS_CDN == data["pyodide_js_url"] def test_get_transcrypt_index_template(transcrypt_config): diff --git a/pyp5js/tests/test_sketch.py b/pyp5js/tests/test_sketch.py index aafb24e2..09876737 100644 --- a/pyp5js/tests/test_sketch.py +++ b/pyp5js/tests/test_sketch.py @@ -90,12 +90,14 @@ def test_loads_config_from_config_file(self): assert same_files.config.interpreter == PYODIDE_INTERPRETER def test_sketch_custom_urls(self): - files = Sketch(self.files.sketch_name, p5_js_url="static/p5.js") + files = Sketch(self.files.sketch_name, p5_js_url="static/p5.js", pyodide_js_url="static/pyodide/pyodide.js") urls = files.urls assert "static/p5.js" == urls.p5_js_url + assert "static/pyodide/pyodide.js" == urls.pyodide_js_url assert "target/target_sketch.js" == urls.sketch_js_url def test_sketch_urls(self): urls = self.files.urls assert P5_JS_CDN == urls.p5_js_url + assert PYODIDE_JS_CDN == urls.pyodide_js_url assert "target/target_sketch.js" == urls.sketch_js_url diff --git a/pyp5js/tests/test_templates_renderers.py b/pyp5js/tests/test_templates_renderers.py index d106ca76..bc09cc51 100644 --- a/pyp5js/tests/test_templates_renderers.py +++ b/pyp5js/tests/test_templates_renderers.py @@ -1,22 +1,41 @@ -from jinja2 import Environment, FileSystemLoader - from pyp5js import templates_renderers as renderers -from pyp5js.sketch import Sketch -from pyp5js.config.fs import PYP5JS_FILES -from .fixtures import sketch -from ..config.sketch import P5_JS_CDN +from .fixtures import sketch, sketch_pyodide +from ..config.sketch import P5_JS_CDN, PYODIDE_JS_CDN -def test_get_sketch_index_content(sketch): +def test_get_transcrypt_index_content(sketch): expected_template = renderers.templates.get_template('transcrypt/index.html') + url = sketch.TARGET_NAME + "/target_sketch.js" expected_content = expected_template.render({ - 'sketch_name': sketch.sketch_name, + "sketch_name": sketch.sketch_name, + "p5_js_url": P5_JS_CDN, + "sketch_js_url": url, + }) + + content = renderers.get_sketch_index_content(sketch) + assert expected_content == content + assert sketch.sketch_name in content + assert P5_JS_CDN in content + assert url in content + + +def test_get_pyodide_index_content(sketch_pyodide): + expected_template = renderers.templates.get_template('pyodide/index.html') + url = sketch_pyodide.TARGET_NAME + "/target_sketch.js" + expected_content = expected_template.render({ + "sketch_name": sketch_pyodide.sketch_name, "p5_js_url": P5_JS_CDN, - "sketch_js_url": sketch.TARGET_NAME + "/target_sketch.js", + "sketch_js_url": url, + "pyodide_js_url": PYODIDE_JS_CDN, }) - assert expected_content == renderers.get_sketch_index_content(sketch) + content = renderers.get_sketch_index_content(sketch_pyodide) + assert sketch_pyodide.sketch_name in content + assert P5_JS_CDN in content + assert PYODIDE_JS_CDN in content + assert url in content + assert expected_content == content def test_get_target_sketch_content(sketch): From 70ada4cce1498ecbfc5535dd014a3d1409d0f836 Mon Sep 17 00:00:00 2001 From: Bernardo Fontes Date: Mon, 1 Nov 2021 12:06:21 -0300 Subject: [PATCH 04/11] Refactor by moving context definition to sketch object --- pyp5js/sketch.py | 9 +++++++++ pyp5js/templates_renderers.py | 7 +------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/pyp5js/sketch.py b/pyp5js/sketch.py index 42d1e380..814cdb3c 100644 --- a/pyp5js/sketch.py +++ b/pyp5js/sketch.py @@ -113,3 +113,12 @@ def urls(self): pyodide_js_url=self.config.pyodide_js_url, sketch_js_url=f"{self.TARGET_NAME}/target_sketch.js", ) + + def get_target_sketch_context(self): + context = { + "sketch_name": self.sketch_name, + "sketch_content": self.sketch_content, + # TODO: if pyodide, add the pyodide indexUrl here too + # (details about this here https://github.com/berinhard/pyp5js/pull/186#pullrequestreview-782362038) + } + return context diff --git a/pyp5js/templates_renderers.py b/pyp5js/templates_renderers.py index 645acdcf..043b4b72 100644 --- a/pyp5js/templates_renderers.py +++ b/pyp5js/templates_renderers.py @@ -32,11 +32,6 @@ def get_target_sketch_content(sketch): """ Renders the content to be written in the temporary SKETCH_NAME/target_sketch.py file """ - context = { - "sketch_name": sketch.sketch_name, - "sketch_content": sketch.sketch_content, - # TODO: if pyodide, add the pyodide indexUrl here too - # (details about this here https://github.com/berinhard/pyp5js/pull/186#pullrequestreview-782362038) - } + context = sketch.get_target_sketch_context() target_js_file = sketch.config.get_target_js_template() return _template_from_file(target_js_file, context) From 7aaf4d64a10953df2132220c98a9f53b6a7e5840 Mon Sep 17 00:00:00 2001 From: Bernardo Fontes Date: Mon, 1 Nov 2021 12:23:17 -0300 Subject: [PATCH 05/11] Define pyodide's indexURL from context as well --- pyp5js/sketch.py | 11 +++++++++-- .../templates/pyodide/target_sketch.js.template | 2 +- pyp5js/tests/test_sketch.py | 13 ++++++++++++- pyp5js/tests/test_templates_renderers.py | 17 ++++++++++++++++- 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/pyp5js/sketch.py b/pyp5js/sketch.py index 814cdb3c..739623b4 100644 --- a/pyp5js/sketch.py +++ b/pyp5js/sketch.py @@ -1,6 +1,8 @@ import os import re from collections import namedtuple +from pathlib import Path + from pyp5js import config from pyp5js.config.sketch import SketchConfig @@ -115,10 +117,15 @@ def urls(self): ) def get_target_sketch_context(self): + """ + This method is used by the template renderers to get the context to be used + to render final target/target_sketch.js file. + """ context = { "sketch_name": self.sketch_name, "sketch_content": self.sketch_content, - # TODO: if pyodide, add the pyodide indexUrl here too - # (details about this here https://github.com/berinhard/pyp5js/pull/186#pullrequestreview-782362038) } + if self.config.is_pyodide: + index = "/".join(self.config.pyodide_js_url.split("/")[:-1]) + "/" + context.update({'pyodide_index_url': index}) return context diff --git a/pyp5js/templates/pyodide/target_sketch.js.template b/pyp5js/templates/pyodide/target_sketch.js.template index 3c26fc24..e0ef0217 100644 --- a/pyp5js/templates/pyodide/target_sketch.js.template +++ b/pyp5js/templates/pyodide/target_sketch.js.template @@ -1674,7 +1674,7 @@ function runCode() { async function main() { const config = { - indexURL : "/static/js/pyodide/", // TODO: replace hardcoded string by {{ indexURL }} + indexURL : "{{ pyodide_index_url }}", fullStdLib: false, } window.pyodide = await loadPyodide(config); diff --git a/pyp5js/tests/test_sketch.py b/pyp5js/tests/test_sketch.py index 09876737..ec0e62fb 100644 --- a/pyp5js/tests/test_sketch.py +++ b/pyp5js/tests/test_sketch.py @@ -3,7 +3,7 @@ from unittest import TestCase from pyp5js.config import SKETCHBOOK_DIR, PYODIDE_INTERPRETER -from pyp5js.config.sketch import P5_JS_CDN +from pyp5js.config.sketch import P5_JS_CDN, PYODIDE_JS_CDN from pyp5js.exceptions import SketchDirAlreadyExistException from pyp5js.sketch import Sketch from pyp5js.exceptions import InvalidName @@ -101,3 +101,14 @@ def test_sketch_urls(self): assert P5_JS_CDN == urls.p5_js_url assert PYODIDE_JS_CDN == urls.pyodide_js_url assert "target/target_sketch.js" == urls.sketch_js_url + + def test_get_target_sketch_context_for_local_pyodide(self): + files = Sketch( + self.files.sketch_name, pyodide_js_url="static/pyodide/pyodide.js", + interpreter=PYODIDE_INTERPRETER) + expected_context = { + "sketch_name": files.sketch_name, + "sketch_content": files.sketch_content, + "pyodide_index_url": "static/pyodide/" + } + assert expected_context == files.get_target_sketch_context() diff --git a/pyp5js/tests/test_templates_renderers.py b/pyp5js/tests/test_templates_renderers.py index bc09cc51..df05708b 100644 --- a/pyp5js/tests/test_templates_renderers.py +++ b/pyp5js/tests/test_templates_renderers.py @@ -38,7 +38,7 @@ def test_get_pyodide_index_content(sketch_pyodide): assert expected_content == content -def test_get_target_sketch_content(sketch): +def test_get_transcrypt_target_sketch_content(sketch): with open(sketch.sketch_py, 'w') as fd: fd.write('content') @@ -50,3 +50,18 @@ def test_get_target_sketch_content(sketch): content = renderers.get_target_sketch_content(sketch) assert expected_content == content + + +def test_get_pyodide_target_sketch_content(sketch_pyodide): + with open(sketch_pyodide.sketch_py, 'w') as fd: + fd.write('content') + + expected_template = renderers.templates.get_template('pyodide/target_sketch.js.template') + expected_content = expected_template.render({ + 'sketch_name': sketch_pyodide.sketch_name, + 'sketch_content': 'content', + 'pyodide_index_url': 'https://cdn.jsdelivr.net/pyodide/v0.18.1/full/', + }) + content = renderers.get_target_sketch_content(sketch_pyodide) + + assert expected_content == content From 73500d2cc668b2319827bcce8a33cce6d37d45fd Mon Sep 17 00:00:00 2001 From: Bernardo Fontes Date: Mon, 1 Nov 2021 12:32:38 -0300 Subject: [PATCH 06/11] Refactor code by moving operation to copy files to Sketch class --- pyp5js/commands.py | 14 +------------- pyp5js/sketch.py | 29 ++++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/pyp5js/commands.py b/pyp5js/commands.py index 03da992d..e83ae2dc 100644 --- a/pyp5js/commands.py +++ b/pyp5js/commands.py @@ -4,7 +4,6 @@ from cprint import cprint from jinja2 import Environment, FileSystemLoader -from pyp5js.config.fs import PYP5JS_FILES from pyp5js.compiler import compile_sketch_js from pyp5js.exceptions import PythonSketchDoesNotExist from pyp5js.sketch import Sketch @@ -30,21 +29,10 @@ def new_sketch(sketch_name, interpreter=PYODIDE_INTERPRETER, template_file="", u "interpreter": interpreter, "index_template": template_file, } - if not use_cdn: - cfg["p5_js_url"] = "/static/p5.js" - # TODO: static version for pyodide too sketch = Sketch(sketch_name, **cfg) sketch.create_sketch_dir() - - templates_files = [ - (sketch.config.get_base_sketch_template(), sketch.sketch_py), - ] - if not use_cdn: - templates_files.append((PYP5JS_FILES.p5js, sketch.p5js)) - # TODO: copy pyodide files to static dir too - for src, dest in templates_files: - shutil.copyfile(src, dest) + sketch.copy_initial_files(use_cdn=use_cdn) index_contet = get_sketch_index_content(sketch) with open(sketch.index_html, "w") as fd: diff --git a/pyp5js/sketch.py b/pyp5js/sketch.py index 739623b4..d13059fb 100644 --- a/pyp5js/sketch.py +++ b/pyp5js/sketch.py @@ -1,14 +1,13 @@ import os import re +import shutil from collections import namedtuple -from pathlib import Path - from pyp5js import config +from pyp5js.config.fs import PYP5JS_FILES from pyp5js.config.sketch import SketchConfig from pyp5js.exceptions import SketchDirAlreadyExistException, InvalidName - SketchUrls = namedtuple('SketchUrls', ['p5_js_url', 'pyodide_js_url', 'sketch_js_url']) @@ -31,10 +30,13 @@ def validate_name(self): does_not_start_with_letter_or_underscore = r'^[^a-zA-Z_]' contains_non_alphanumerics_except_underscore = r'[^a-zA-Z0-9_]' if re.match(does_not_start_with_letter_or_underscore, self.sketch_name) or \ - re.search(contains_non_alphanumerics_except_underscore, self.sketch_name): + re.search(contains_non_alphanumerics_except_underscore, self.sketch_name): raise InvalidName(self) def create_sketch_dir(self): + """ + Create sketch required directories + """ self.validate_name() if self.sketch_dir.exists(): @@ -45,6 +47,23 @@ def create_sketch_dir(self): self.target_dir.mkdir() self.config.write(self.config_file) + def copy_initial_files(self, use_cdn=True): + """ + Copy requlred template files to the sketch directory + """ + if not use_cdn: + self.config.p5_js_url = "/static/p5.js" + # TODO: static version for pyodide too + + templates_files = [ + (self.config.get_base_sketch_template(), self.sketch_py), + ] + if not use_cdn: + templates_files.append((PYP5JS_FILES.p5js, self.p5js)) + # TODO: copy pyodide files to static dir too + for src, dest in templates_files: + shutil.copyfile(src, dest) + @property def sketch_exists(self): return self.sketch_py.exists() @@ -56,7 +75,6 @@ def sketch_content(self): with self.sketch_py.open() as fd: return fd.read() - @property def has_all_files(self): return all([ @@ -129,3 +147,4 @@ def get_target_sketch_context(self): index = "/".join(self.config.pyodide_js_url.split("/")[:-1]) + "/" context.update({'pyodide_index_url': index}) return context + From 39d8905df4f92f569166cb9ae5420b862c80a3c1 Mon Sep 17 00:00:00 2001 From: Bernardo Fontes Date: Mon, 1 Nov 2021 12:46:59 -0300 Subject: [PATCH 07/11] Copy pyodide files when creating sketch without CDN assets --- pyp5js/config/fs.py | 4 ++++ pyp5js/sketch.py | 9 +++++++-- pyp5js/tests/test_commands.py | 1 - pyp5js/tests/test_sketch.py | 18 ++++++++++++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/pyp5js/config/fs.py b/pyp5js/config/fs.py index 563439d7..0d70b458 100644 --- a/pyp5js/config/fs.py +++ b/pyp5js/config/fs.py @@ -65,5 +65,9 @@ def pyodide_index_html(self): def pyodide_base_sketch_template(self): return self.templates_dir.joinpath('pyodide', 'base_sketch.py.template') + @property + def pyodide_js_dir(self): + return self.static_dir / "js" / "pyodide" + PYP5JS_FILES = LibFiles() diff --git a/pyp5js/sketch.py b/pyp5js/sketch.py index d13059fb..cdc5c5f9 100644 --- a/pyp5js/sketch.py +++ b/pyp5js/sketch.py @@ -53,14 +53,19 @@ def copy_initial_files(self, use_cdn=True): """ if not use_cdn: self.config.p5_js_url = "/static/p5.js" - # TODO: static version for pyodide too + if self.config.is_pyodide: + self.config.pyodide_js_url = "/static/pyodide/pyodide_v0.18.1.js" templates_files = [ (self.config.get_base_sketch_template(), self.sketch_py), ] if not use_cdn: templates_files.append((PYP5JS_FILES.p5js, self.p5js)) - # TODO: copy pyodide files to static dir too + if self.config.is_pyodide: + shutil.copytree(PYP5JS_FILES.pyodide_js_dir, self.static_dir / "pyodide") + # delete packages.json that's not necessary + (self.static_dir / "pyodide" / "packages.json").unlink() + for src, dest in templates_files: shutil.copyfile(src, dest) diff --git a/pyp5js/tests/test_commands.py b/pyp5js/tests/test_commands.py index 85b9065a..99f8267e 100644 --- a/pyp5js/tests/test_commands.py +++ b/pyp5js/tests/test_commands.py @@ -106,4 +106,3 @@ def test_create_sketch_using_local_installed_assets(self): commands.new_sketch(self.sketch_name, use_cdn=False) assert self.sketch.p5js.exists() - # TODO: assertion on pyodide assets too diff --git a/pyp5js/tests/test_sketch.py b/pyp5js/tests/test_sketch.py index ec0e62fb..7ad14829 100644 --- a/pyp5js/tests/test_sketch.py +++ b/pyp5js/tests/test_sketch.py @@ -112,3 +112,21 @@ def test_get_target_sketch_context_for_local_pyodide(self): "pyodide_index_url": "static/pyodide/" } assert expected_context == files.get_target_sketch_context() + + def test_copy_local_files_if_not_using_cdn(self): + files = Sketch('test', interpreter=PYODIDE_INTERPRETER) + files.create_sketch_dir() + files.copy_initial_files(use_cdn=False) + pyodide_js_dir = files.static_dir / "pyodide" + + assert files.config.p5_js_url == "/static/p5.js" + assert files.config.pyodide_js_url == "/static/pyodide/pyodide_v0.18.1.js" + assert files.p5js.exists() + assert pyodide_js_dir.exists() + + assert (pyodide_js_dir / "pyodide.asm.data").exists() + assert (pyodide_js_dir / "pyodide.asm.js").exists() + assert (pyodide_js_dir / "pyodide.asm.wasm").exists() + assert (pyodide_js_dir / "pyodide.js.map").exists() + assert (pyodide_js_dir / "pyodide_v0.18.1.js").exists() + assert not (pyodide_js_dir / "packages.json").exists() From 80eddbea3eb32471d3993650fb3bd4d34ace9612 Mon Sep 17 00:00:00 2001 From: Bernardo Fontes Date: Mon, 1 Nov 2021 13:00:50 -0300 Subject: [PATCH 08/11] Pyodide must be loaded after p5.js and before the script code --- pyp5js/http/templates/view_sketch.html | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pyp5js/http/templates/view_sketch.html b/pyp5js/http/templates/view_sketch.html index a054e89b..2f3044f6 100644 --- a/pyp5js/http/templates/view_sketch.html +++ b/pyp5js/http/templates/view_sketch.html @@ -63,6 +63,11 @@ } + + + {% if live_run %} + + {% endif %} {% endblock %} {% block content %} @@ -119,11 +124,6 @@ }); - {% if live_run %} - - {% endif %} - - + {% endblock %} From 2a09edb90ef45d8e08f89d095a44a61aaa1da0f9 Mon Sep 17 00:00:00 2001 From: Bernardo Fontes Date: Mon, 1 Nov 2021 13:10:17 -0300 Subject: [PATCH 09/11] Update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a756eb0e..df3d93ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,9 @@ Development ----------- -- Serve pyodide JS files locally [PR #186](https://github.com/berinhard/pyp5js/pull/186) - Create sketch using p5.js from CDN [PR #191](https://github.com/berinhard/pyp5js/pull/191) - Add `keyIsDown` event to Transcrypt mode - [PR #187](https://github.com/berinhard/pyp5js/pull/187) - Fix bug with multiple events calls - PR #187 too +- Serve JS files if `--local` flag [PR #195](https://github.com/berinhard/pyp5js/pull/195) 0.7.0 ----- From 7f33220ae8b03a0504e9aaaff8b187e055216775 Mon Sep 17 00:00:00 2001 From: Bernardo Fontes Date: Mon, 1 Nov 2021 13:26:15 -0300 Subject: [PATCH 10/11] Try to use event listeners --- pyp5js/templates/pyodide/target_sketch.js.template | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pyp5js/templates/pyodide/target_sketch.js.template b/pyp5js/templates/pyodide/target_sketch.js.template index e0ef0217..d92a1e1b 100644 --- a/pyp5js/templates/pyodide/target_sketch.js.template +++ b/pyp5js/templates/pyodide/target_sketch.js.template @@ -1692,4 +1692,6 @@ async function main() { runCode(); }; -await main(); +document.addEventListener('DOMContentLoaded', async function() { + await main() +}, false); From f6c8536d221648cf95978e920c7ca66001ef67b2 Mon Sep 17 00:00:00 2001 From: Bernardo Fontes Date: Mon, 1 Nov 2021 17:19:00 -0300 Subject: [PATCH 11/11] Main call shouldn't use async statement --- pyp5js/http/templates/view_sketch.html | 3 +-- pyp5js/templates/pyodide/target_sketch.js.template | 5 ++--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/pyp5js/http/templates/view_sketch.html b/pyp5js/http/templates/view_sketch.html index 2f3044f6..445c951d 100644 --- a/pyp5js/http/templates/view_sketch.html +++ b/pyp5js/http/templates/view_sketch.html @@ -66,7 +66,7 @@ {% if live_run %} - + {% endif %} {% endblock %} @@ -171,7 +171,6 @@ {% endif %} } } - }); diff --git a/pyp5js/templates/pyodide/target_sketch.js.template b/pyp5js/templates/pyodide/target_sketch.js.template index d92a1e1b..5ea7e4f6 100644 --- a/pyp5js/templates/pyodide/target_sketch.js.template +++ b/pyp5js/templates/pyodide/target_sketch.js.template @@ -1692,6 +1692,5 @@ async function main() { runCode(); }; -document.addEventListener('DOMContentLoaded', async function() { - await main() -}, false); +// async method +main();