def new_sketch(sketch_name, interpreter=PYODIDE_INTERPRETER): """ Creates a new sketch with the required assets and a index.html file, based on pyp5js's templates :param sketch_name: name for new sketch :param interpreter: interpreter to use (transcrypt or pyodide) :type sketch_name: string :return: file names :rtype: list of strings """ sketch = Sketch(sketch_name, interpreter=interpreter) sketch.create_sketch_dir() templates_files = [ (sketch.config.get_base_sketch_template(), sketch.sketch_py), (PYP5JS_FILES.p5js, sketch.p5js), ] for src, dest in templates_files: shutil.copyfile(src, dest) index_contet = get_sketch_index_content(sketch) with open(sketch.index_html, "w") as fd: fd.write(index_contet) return sketch
def compile_sketch(sketch_name, generate_index=False, index_template=None, force_local=False): """ Transcrypt the sketch python code to javascript. :param sketch_name: name for new sketch :param generate_index: boolean to flag if the index.html file should be updated :param force_local: boolean to flag to force local run (used by web editor only) :type sketch_name: string :return: file names :rtype: list of strings """ sketch = Sketch(sketch_name) sketch.validate_name() if not sketch.sketch_exists: raise PythonSketchDoesNotExist(sketch) compile_sketch_js(sketch, force_local=force_local) if generate_index: # to be able to overwrite default index template file # useful for generating the docs or debugging sketch.config.index_template = index_template index_contet = get_sketch_index_content(sketch) with open(sketch.index_html, "w", encoding="utf-8") as fd: fd.write(index_contet) cprint.info(f"{sketch.index_html.resolve()} updated") return sketch
def new_sketch(sketch_name, interpreter=PYODIDE_INTERPRETER, template_file="", use_cdn=True): """ Creates a new sketch with the required assets and a index.html file, based on pyp5js's templates :param sketch_name: name for new sketch :param interpreter: interpreter to use (transcrypt or pyodide) :param template_file: use a custom template for index.html instead of default one :param use_cdn: if false, the sketch will have copies of required static assets (p5.js and pyodide) :type sketch_name: string :return: file names :rtype: list of strings """ cfg = { "interpreter": interpreter, "index_template": template_file, } sketch = Sketch(sketch_name, **cfg) sketch.create_sketch_dir() sketch.copy_initial_files(use_cdn=use_cdn) index_contet = get_sketch_index_content(sketch) with open(sketch.index_html, "w", encoding="utf-8") as fd: fd.write(index_contet) return sketch
def new_sketch(sketch_name): """ Creates a new sketch with the required assets and a index.html file, based on pyp5js's templates :param sketch_name: name for new sketch :type sketch_name: string :return: file names :rtype: list of strings """ sketch_files = SketchFiles(sketch_name) sketch_files.create_sketch_dir() templates_files = [ (sketch_files.from_lib.base_sketch, sketch_files.sketch_py), (sketch_files.from_lib.p5js, sketch_files.p5js), (sketch_files.from_lib.p5_dom_js, sketch_files.p5_dom_js), ] for src, dest in templates_files: shutil.copyfile(src, dest) index_contet = get_sketch_index_content(sketch_files) with open(sketch_files.index_html, "w") as fd: fd.write(index_contet) return sketch_files
def test_get_sketch_index_content(sketch): expected_template = renderers.templates.get_template( 'transcrypt/index.html') expected_content = expected_template.render({ 'sketch_name': sketch.sketch_name, "p5_js_url": sketch.STATIC_NAME + "/p5.js", "sketch_js_url": sketch.TARGET_NAME + "/target_sketch.js", }) assert expected_content == renderers.get_sketch_index_content(sketch)
def test_get_sketch_index_content(): sketch_files = SketchFiles('foo') expected_template = renderers.templates.get_template( sketch_files.from_lib.index_html.name) expected_content = expected_template.render({ 'sketch_name': sketch_files.sketch_name, "p5_js_url": sketch_files.STATIC_NAME + "/p5.js", "sketch_js_url": sketch_files.TARGET_NAME + "/target_sketch.js", }) assert expected_content == renderers.get_sketch_index_content(sketch_files)
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, "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": url, "pyodide_js_url": PYODIDE_JS_CDN, }) 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