Skip to content

Commit

Permalink
feat: Allow setting Pyodide version
Browse files Browse the repository at this point in the history
Also upgrade default version to 0.26.4.

Issue-66: #66
PR-67: #67
Co-authored-by: Timothée Mazzucotelli <[email protected]>
  • Loading branch information
ahuang11 and pawamoy authored Dec 5, 2024
1 parent 3562fbb commit 912c8c7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
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

0 comments on commit 912c8c7

Please sign in to comment.