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

Allow setting pyodide version #67

Merged
merged 4 commits into from
Dec 5, 2024
Merged
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
12 changes: 12 additions & 0 deletions docs/usage/pyodide.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions src/markdown_exec/formatters/pyodide.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
assets = """
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.16.0/ace.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/pyodide/v0.23.0/full/pyodide.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/pyodide/v{version}/full/pyodide.js"></script>
<link title="light" rel="alternate stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/tomorrow.min.css" disabled="disabled">
<link title="dark" rel="alternate stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/tomorrow-night-blue.min.css" disabled="disabled">
"""
Expand Down Expand Up @@ -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"}
Expand All @@ -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
Loading