Skip to content
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
18 changes: 18 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jupyter lite build --contents content --output-dir dist
python3 - <<'PY'
import hashlib
import json
import re
import shutil
from pathlib import Path

Expand All @@ -29,6 +30,23 @@ kernel_settings = config["jupyter-config-data"]["litePluginSettings"][
]
kernel_settings["pyodideUrl"] = f"./static/pyodide/{versioned_module.name}"
config_path.write_text(json.dumps(config, indent=2) + "\n")

kernel_extension_dir = Path(
"dist/extensions/@jupyterlite/pyodide-kernel-extension/static"
)
load_pyodide_pattern = re.compile(
r"\(await ([A-Za-z_$][\w$]*|__webpack_require__)\(476\)\(([A-Za-z_$][\w$]*)\)\)\.loadPyodide"
)
patched_files = []
for path in kernel_extension_dir.glob("*.js"):
source = path.read_text()
patched = load_pyodide_pattern.sub(r"(await import(\2)).loadPyodide", source)
if patched != source:
path.write_text(patched)
patched_files.append(path.name)

if not patched_files:
raise SystemExit("No Pyodide dynamic import bundle entry was patched")
PY
find content/ -type f -exec cat {} \; | sha256sum | cut -d' ' -f1 > content-hash.txt
HASH=$(cat content-hash.txt)
Expand Down
11 changes: 11 additions & 0 deletions tests/test_notebooks_pyodide.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ def test_configured_pyodide_module_exists():
assert (DIST / pyodide_url.removeprefix("./")).exists()


def test_pyodide_kernel_uses_native_dynamic_import():
extension_dir = DIST / "extensions" / "@jupyterlite" / "pyodide-kernel-extension" / "static"
js_files = list(extension_dir.glob("*.js"))
assert js_files
bundle_source = "\n".join(path.read_text(encoding="utf8") for path in js_files)
assert ".endsWith(\".mjs\")?t=(await import(" in bundle_source
assert "(476)(r)).loadPyodide" not in bundle_source
assert "(476)(n)).loadPyodide" not in bundle_source
assert "(476)(s)).loadPyodide" not in bundle_source


def _execute_notebook(notebook_path: Path, *, cells: int | None = None, timeout: int = 600) -> None:
"""Execute a notebook using the local CPython kernel.

Expand Down
Loading