-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: sandboxed exports for consistent wasm envs #9519
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1899fe6
feat: sandboxed exports for wasm
dmadisetti a88f587
fix: missing file
dmadisetti 070037c
tidy: ruff format
dmadisetti 7806c18
cleanup: fix url and function name
dmadisetti d0abbb6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 43cda2b
Merge branch 'main' of github:marimo-team/marimo into dm/sandbox-export
dmadisetti efbdfb3
comments: Doc string updates and pyodide_constraints refresh
dmadisetti 268fe33
Merge branch 'main' of github:marimo-team/marimo into dm/sandbox-export
dmadisetti c3d686d
merge res
dmadisetti 9b8286b
fix: failing tests
dmadisetti d030ced
comments: suggestions from code review
dmadisetti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| # Copyright 2026 Marimo. All rights reserved. | ||
| """Resolve Pyodide-compatible package constraints from pyodide-lock.json. | ||
|
|
||
| The lockfile is fetched from `wasm.marimo.app` (which serves a marimo-patched | ||
| fork of upstream `pyodide-lock.json`). Set `MARIMO_PYODIDE_LOCK_FILE` to read | ||
| a local copy instead — useful for offline / air-gapped environments and for | ||
| testing. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import re | ||
|
|
||
| import msgspec | ||
|
|
||
| from marimo import _loggers | ||
| from marimo._utils import requests | ||
| from marimo._version import __version__ | ||
|
|
||
| LOGGER = _loggers.marimo_logger() | ||
|
|
||
| # Pyodide version matching frontend/package.json — update together. | ||
| PYODIDE_VERSION = "0.27.7" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thoughts on keeping this file in
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or at least the constants |
||
|
|
||
| # Derived from the lockfile's info.python field (Pyodide 0.27.7 → 3.12.7). | ||
| PYODIDE_PYTHON_VERSION = "3.12" | ||
|
|
||
| # Env var pointing at a local pyodide-lock.json. Lets offline / air-gapped | ||
| # users supply the lockfile out-of-band instead of fetching from the host. | ||
| PYODIDE_LOCK_FILE_ENV = "MARIMO_PYODIDE_LOCK_FILE" | ||
|
|
||
| # We host our own version of | ||
| # "https://cdn.jsdelivr.net/pyodide/v{version}/full/pyodide-lock.json" | ||
| # as marimo contains patches to support more packages. The `v` query is | ||
| # the marimo version (cache-buster); the server selects the lockfile for | ||
| # whichever pyodide release that marimo build was pinned to. | ||
| _LOCKFILE_URL = f"https://wasm.marimo.app/pyodide-lock.json?v={__version__}" | ||
|
dmadisetti marked this conversation as resolved.
|
||
|
|
||
|
|
||
| class PyodidePackage(msgspec.Struct, kw_only=True): | ||
| """A single package entry inside pyodide-lock.json.""" | ||
|
|
||
| name: str | ||
| version: str | ||
| package_type: str = "" | ||
|
|
||
|
|
||
| class PyodideLockfile(msgspec.Struct, kw_only=True): | ||
| """Top-level shape of pyodide-lock.json. | ||
|
|
||
| Other fields (`info`, `package` indices, etc.) are ignored. | ||
| """ | ||
|
|
||
| packages: dict[str, PyodidePackage] = msgspec.field(default_factory=dict) | ||
|
|
||
|
|
||
| def _read_lockfile() -> PyodideLockfile: | ||
| override = os.environ.get(PYODIDE_LOCK_FILE_ENV) | ||
| if override: | ||
| with open(override, "rb") as f: | ||
| payload = f.read() | ||
| else: | ||
| # `marimo._utils.requests` ships its own `marimo/<version>` UA. | ||
| payload = ( | ||
| requests.get(_LOCKFILE_URL, timeout=30).raise_for_status().content | ||
| ) | ||
| return msgspec.json.decode(payload, type=PyodideLockfile) | ||
|
|
||
|
|
||
| def fetch_pyodide_package_versions() -> dict[str, str]: | ||
| """Fetch pyodide-lock.json and return {package_name: version}. | ||
|
|
||
| Reads from the path in `$MARIMO_PYODIDE_LOCK_FILE` if set, otherwise | ||
| fetches from `wasm.marimo.app`. Only entries with | ||
| `package_type == "package"` are included; test packages | ||
| (names ending in `-tests`) are excluded. | ||
| """ | ||
| lockfile = _read_lockfile() | ||
| return { | ||
| spec.name: spec.version | ||
| for spec in lockfile.packages.values() | ||
| if spec.package_type == "package" and not spec.name.endswith("-tests") | ||
| } | ||
|
|
||
|
|
||
| def write_constraint_file( | ||
| path: str, | ||
| ) -> bool: | ||
| """Fetch Pyodide lockfile and write a pip constraints file. | ||
|
|
||
| Returns True on success, False if the lockfile couldn't be fetched. | ||
| """ | ||
| try: | ||
| versions = fetch_pyodide_package_versions() | ||
| except Exception: | ||
| LOGGER.warning( | ||
| "Could not fetch Pyodide lockfile — " | ||
| "running without package version constraints." | ||
| ) | ||
| return False | ||
| with open(path, "w", encoding="utf-8") as f: | ||
| f.writelines( | ||
| f"{pkg}=={version}\n" for pkg, version in sorted(versions.items()) | ||
| ) | ||
| return True | ||
|
|
||
|
|
||
| def normalize_package_name(name: str) -> str: | ||
| """Normalize package name per PEP 503.""" | ||
| return re.sub(r"[-_.]+", "-", name).lower() | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.