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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: clean up export for python and md fileformats (#8681)
Dylan Madisettia0717e2b9918
diff --git a/marimo/_cli/export/commands.py b/marimo/_cli/export/commands.pyindex 196e474ae5..8e904ef2c4 100644--- a/marimo/_cli/export/commands.py+++ b/marimo/_cli/export/commands.py@@ -299,12 +299,6 @@ def script("""import sys- # Set default, if not provided- if sandbox is None:- from marimo._cli.sandbox import maybe_prompt_run_in_sandbox-- sandbox = maybe_prompt_run_in_sandbox(name)-if sandbox:from marimo._cli.sandbox import run_in_sandbox@@ -376,12 +370,6 @@ def md("""import sys- # Set default, if not provided- if sandbox is None:- from marimo._cli.sandbox import maybe_prompt_run_in_sandbox-- sandbox = maybe_prompt_run_in_sandbox(name)-if sandbox:from marimo._cli.sandbox import run_in_sandboxdiff --git a/marimo/_convert/script.py b/marimo/_convert/script.pyindex 34672c9aa7..a430aeff54 100644--- a/marimo/_convert/script.py+++ b/marimo/_convert/script.py@@ -1,7 +1,6 @@# Copyright 2026 Marimo. All rights reserved.from __future__ import annotations-from marimo._ast import codegenfrom marimo._ast.app import InternalAppfrom marimo._ast.load import load_notebook_irfrom marimo._runtime.dataflow import topological_sort@@ -9,6 +8,38 @@ from marimo._schemas.serialization import NotebookSerializationfrom marimo._version import __version__+def _header_for_script(ir: NotebookSerialization) -> str:+ """Extract a Python script-appropriate header from IR.++ Python-origin notebooks store raw header text (PEP 723 block, copyright+ comments) in ir.header.value. Markdown-origin notebooks store YAML+ frontmatter, which must be parsed to recover the pyproject/header fields.+ """+ value = (ir.header.value if ir.header else None) or ""+ if not value:+ return ""++ # Python-origin: all comment lines parse as YAML comments → None+ # Markdown-origin: YAML dict with possible "pyproject"/"header" keys+ try:+ import yaml++ frontmatter = yaml.safe_load(value)+ if isinstance(frontmatter, dict) and (+ "pyproject" in frontmatter or "header" in frontmatter+ ):+ from marimo._utils.inline_script_metadata import (+ get_headers_from_frontmatter,+ )++ headers = get_headers_from_frontmatter(frontmatter)+ return headers.get("pyproject") or headers.get("header") or ""+ except Exception:+ pass++ return value++def convert_from_ir_to_script(ir: NotebookSerialization) -> str:app = InternalApp(load_notebook_ir(ir))@@ -24,10 +55,7 @@ def convert_from_ir_to_script(ir: NotebookSerialization) -> str:)graph = app.graph- if ir.filename:- header = codegen.get_header_comments(ir.filename) or ""- else:- header = ""+ header = _header_for_script(ir)codes: list[str] = ["# %%\n" + graph.cells[cid].codefor cid in topological_sort(graph, graph.cells.keys())diff --git a/tests/_cli/snapshots/export/script/script_sandboxed.txt b/tests/_cli/snapshots/export/script/script_sandboxed.txtnew file mode 100644index 0000000000..d8a9c25f05--- /dev/null+++ b/tests/_cli/snapshots/export/script/script_sandboxed.txt@@ -0,0 +1,19 @@+# Copyright 2026 Marimo. All rights reserved.+# /// script+# requires-python = ">=3.11"+# dependencies = [+# "polars",+# "marimo>=0.8.0",+# "quak",+# "vega-datasets",+# ]+# ///+++__generated_with = "0.0.0"++# %%+import marimo as mo++# %%+slider = mo.ui.slider(0, 10)diff --git a/tests/_cli/test_cli_export.py b/tests/_cli/test_cli_export.pyindex e9789e63e3..a5563852bc 100644--- a/tests/_cli/test_cli_export.py+++ b/tests/_cli/test_cli_export.py@@ -598,6 +598,17 @@ class TestExportScript:p.output,)+ @staticmethod+ def test_export_script_with_inline_deps(+ temp_sandboxed_marimo_file: str,+ ) -> None:+ p = _run_export("script", temp_sandboxed_marimo_file)+ _assert_success(p)+ output = p.stdout+ assert "# /// script" in output+ assert "polars" in output+ snapshot(_get_snapshot_path("script", "script_sandboxed"), output)+@pytest.mark.skipif(condition=DependencyManager.watchdog.has() or _is_win32(),reason="hangs when watchdog is installed, flaky on Windows",diff --git a/tests/_convert/test_convert_py.py b/tests/_convert/test_convert_py.pyindex 2b49fb908e..b8e62f1f63 100644--- a/tests/_convert/test_convert_py.py+++ b/tests/_convert/test_convert_py.py@@ -5,6 +5,7 @@ from textwrap import dedentfrom marimo import __version__from marimo._convert.converters import MarimoConvert+from marimo._convert.script import _header_for_scriptfrom tests.mocks import snapshottersnapshot = snapshotter(__file__)@@ -124,3 +125,71 @@ def test_unparsable_cell_with_escaped_quotes():snapshot("unparsable_cell_with_escaped_quotes.py.txt", identity(marimo_script))+++def test_inline_deps_preserved_in_script_export() -> None:+ """PEP 723 inline deps in Python notebooks are preserved when exporting to script."""+ marimo_py = dedent("""\+ # /// script+ # requires-python = ">=3.11"+ # dependencies = [+ # "pandas",+ # ]+ # ///++ import marimo++ __generated_with = "0.0.0"+ app = marimo.App()+++ @app.cell+ def _():+ import pandas as pd+ return (pd,)+++ if __name__ == "__main__":+ app.run()+ """)++ ir = MarimoConvert.from_py(marimo_py).ir+ header = _header_for_script(ir)+ assert "# /// script" in header+ assert "pandas" in header+++def test_inline_deps_preserved_via_markdown_roundtrip() -> None:+ """PEP 723 inline deps survive Python → markdown → script conversion."""+ marimo_py = dedent("""\+ # /// script+ # requires-python = ">=3.11"+ # dependencies = [+ # "pandas",+ # ]+ # ///++ import marimo++ __generated_with = "0.0.0"+ app = marimo.App()+++ @app.cell+ def _():+ import pandas as pd+ return (pd,)+++ if __name__ == "__main__":+ app.run()+ """)++ # Python → markdown+ md = MarimoConvert.from_py(marimo_py).to_markdown()++ # markdown → script IR+ ir = MarimoConvert.from_md(md).ir+ header = _header_for_script(ir)+ assert "# /// script" in header+ assert "pandas" in header