From 912c8c75a5f579a949644f33bcead0b71e9637fd Mon Sep 17 00:00:00 2001 From: Andrew <15331990+ahuang11@users.noreply.github.com> Date: Thu, 5 Dec 2024 15:46:30 -0800 Subject: [PATCH] feat: Allow setting Pyodide version MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also upgrade default version to 0.26.4. Issue-66: https://github.com/pawamoy/markdown-exec/issues/66 PR-67: https://github.com/pawamoy/markdown-exec/pull/67 Co-authored-by: Timothée Mazzucotelli --- docs/usage/pyodide.md | 12 ++++++++++++ src/markdown_exec/formatters/pyodide.py | 5 +++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/docs/usage/pyodide.md b/docs/usage/pyodide.md index 3077a92..8953b86 100644 --- a/docs/usage/pyodide.md +++ b/docs/usage/pyodide.md @@ -33,6 +33,18 @@ cowsay.cow("Hello World") Packages installed with Micropip are cached by the browser as well, making future installations much faster. +## Pyodide version + +You can select a specific Pyodide version with the `version` option: + +````md +```pyodide version="0.26.4" +print("Hello.") +``` +````md + +NOTE: **All Pyodide blocks on the same page should use the same version!** + ## Sessions Editors with the same session share the same `globals()` dictionary, diff --git a/src/markdown_exec/formatters/pyodide.py b/src/markdown_exec/formatters/pyodide.py index 7620999..f6e3f86 100644 --- a/src/markdown_exec/formatters/pyodide.py +++ b/src/markdown_exec/formatters/pyodide.py @@ -16,7 +16,7 @@ assets = """ - + """ @@ -46,6 +46,7 @@ def _format_pyodide(code: str, md: Markdown, session: str, extra: dict, **options: Any) -> str: # noqa: ARG001 global _counter # noqa: PLW0603 _counter += 1 + version = extra.pop("version", "0.26.4").lstrip("v") install = extra.pop("install", "") install = install.split(",") if install else [] exclude_assets = extra.pop("assets", "1").lower() in {"0", "false", "no", "off"} @@ -66,4 +67,4 @@ def _format_pyodide(code: str, md: Markdown, session: str, extra: dict, **option rendered = template % data if exclude_assets: return rendered - return assets + rendered + return assets.format(version=version) + rendered