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: clean up export for python and md fileformats (#8681)

Dylan Madisettia0717e2b9918
diff --git a/marimo/_cli/export/commands.py b/marimo/_cli/export/commands.py
index 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_sandbox
diff --git a/marimo/_convert/script.py b/marimo/_convert/script.py
index 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 codegen
from marimo._ast.app import InternalApp
from marimo._ast.load import load_notebook_ir
from marimo._runtime.dataflow import topological_sort
@@ -9,6 +8,38 @@ from marimo._schemas.serialization import NotebookSerialization
from 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].code
for 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.txt
new file mode 100644
index 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.py
index 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.py
index 2b49fb908e..b8e62f1f63 100644
--- a/tests/_convert/test_convert_py.py
+++ b/tests/_convert/test_convert_py.py
@@ -5,6 +5,7 @@ from textwrap import dedent
from marimo import __version__
from marimo._convert.converters import MarimoConvert
+from marimo._convert.script import _header_for_script
from tests.mocks import snapshotter
snapshot = 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