Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix(wasm): skip lockfile pins for offline exports (#11104)

Péter Gyarmatif4f28449a975
diff --git a/marimo/_export/file.py b/marimo/_export/file.py
index ea24fca990..e7cbb15569 100644
--- a/marimo/_export/file.py
+++ b/marimo/_export/file.py
@@ -207,6 +207,12 @@ async def export_ipynb(
async def _prepare_wasm_export(
code: str, request: WASMFileExportRequest
) -> tuple[str, WASMExportOptions]:
+ if request.offline_export_dir is None:
+ # Pin Pyodide-bundled deps to the lockfile version: micropip installs
+ # those builds regardless of the header's specifier, and rejects the
+ # whole install when the two disagree. Offline exports resolve the
+ # header against their own lockfile and pin it while bundling.
+ code = pin_pep723_dependencies_for_wasm(code, request.path)
if request.code_transform is not None:
code = request.code_transform(code)
options = request.options
@@ -251,12 +257,7 @@ async def export_wasm(
)
resolved = config.get_config()
- # Pin Pyodide-bundled deps to the lockfile version, as the executed
- # export does: micropip installs those builds regardless of the
- # header's specifier, and rejects the whole install when the two
- # disagree.
- code = pin_pep723_dependencies_for_wasm(app.to_py(), request.path)
- code, options = await _prepare_wasm_export(code, request)
+ code, options = await _prepare_wasm_export(app.to_py(), request)
result = Exporter().export_as_wasm(
WASMExportRequest(
@@ -550,10 +551,9 @@ async def _export_wasm_with_execution(
drop_virtual_file_outputs=True,
)
- code = pin_pep723_dependencies_for_wasm(
- file_manager.app.to_py(), request.path
+ code, options = await _prepare_wasm_export(
+ file_manager.app.to_py(), request
)
- code, options = await _prepare_wasm_export(code, request)
html, filename = Exporter().export_as_wasm(
WASMExportRequest(
diff --git a/tests/_export/test_exporter.py b/tests/_export/test_exporter.py
index cdfa16263d..62b2a69e19 100644
--- a/tests/_export/test_exporter.py
+++ b/tests/_export/test_exporter.py
@@ -2954,3 +2954,63 @@ async def test_export_wasm_unexecuted_pins_header_to_pyodide_lock(
assert "numpy==1.26.0" not in result.text
assert "nltools==0.6.0.dev2" in result.text
assert 'lock_kind = \\"observed\\"' in result.text
+
+
+async def test_export_wasm_offline_resolves_header_as_written(
+ tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """Offline exports resolve the header against their own lockfile."""
+ lockfile = tmp_path / "pyodide-lock.json"
+ lockfile.write_text(
+ json.dumps(
+ {
+ "packages": {
+ "numpy": {
+ "name": "numpy",
+ "version": "2.4.3",
+ "package_type": "package",
+ }
+ }
+ }
+ )
+ )
+ monkeypatch.setenv("MARIMO_PYODIDE_LOCK_FILE", str(lockfile))
+
+ notebook = tmp_path / "notebook.py"
+ notebook.write_text(
+ textwrap.dedent(
+ """
+ # /// script
+ # dependencies = ["numpy", "pandas>=2"]
+ # ///
+
+ import marimo
+
+ __generated_with = "0.0.0"
+ app = marimo.App()
+
+
+ if __name__ == "__main__":
+ app.run()
+ """
+ ).lstrip()
+ )
+
+ async def bundle(code: str, *_args: Any, sources: Any, **_kwargs: Any):
+ return code, sources
+
+ with patch(
+ "marimo._export.offline.bundle_wasm_runtime",
+ AsyncMock(side_effect=bundle),
+ ) as bundle_wasm_runtime:
+ result = await export_wasm(
+ WASMFileExportRequest(
+ path=MarimoPath(notebook),
+ options=WASMExportOptions(mode="run", show_code=True),
+ offline_export_dir=tmp_path / "dist",
+ )
+ )
+
+ assert result.did_error is False
+ bundled_code = bundle_wasm_runtime.await_args.args[0]
+ assert 'dependencies = ["numpy", "pandas>=2"]' in bundled_code