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.

improvement: install nbformat when running 'marimo export ipynb' in a sandbox (#4391)

Myles Scolnick6c4bf368e915
diff --git a/marimo/_cli/cli.py b/marimo/_cli/cli.py
index 97831100aa..fef6653076 100644
--- a/marimo/_cli/cli.py
+++ b/marimo/_cli/cli.py
@@ -384,7 +384,7 @@ def edit(
from marimo._cli.sandbox import run_in_sandbox
# TODO: consider adding recommended as well
- run_in_sandbox(sys.argv[1:], name, additional_features=["lsp"])
+ run_in_sandbox(sys.argv[1:], name=name, additional_features=["lsp"])
return
GLOBAL_SETTINGS.PROFILE_DIR = profile_dir
@@ -519,7 +519,7 @@ def new(
from marimo._cli.sandbox import run_in_sandbox
# TODO: consider adding recommended as well
- run_in_sandbox(sys.argv[1:], None, additional_features=["lsp"])
+ run_in_sandbox(sys.argv[1:], name=None, additional_features=["lsp"])
return
file_router: Optional[AppFileRouter] = None
@@ -716,7 +716,7 @@ def run(
if sandbox:
from marimo._cli.sandbox import run_in_sandbox
- run_in_sandbox(sys.argv[1:], name)
+ run_in_sandbox(sys.argv[1:], name=name)
return
# Validate name, or download from URL
diff --git a/marimo/_cli/export/commands.py b/marimo/_cli/export/commands.py
index 86baa39337..b71a7081ec 100644
--- a/marimo/_cli/export/commands.py
+++ b/marimo/_cli/export/commands.py
@@ -172,7 +172,7 @@ def html(
if sandbox:
from marimo._cli.sandbox import run_in_sandbox
- run_in_sandbox(sys.argv[1:], name)
+ run_in_sandbox(sys.argv[1:], name=name)
return
cli_args = parse_args(args)
@@ -253,7 +253,7 @@ def script(
if sandbox:
from marimo._cli.sandbox import run_in_sandbox
- run_in_sandbox(sys.argv[1:], name)
+ run_in_sandbox(sys.argv[1:], name=name)
return
def export_callback(file_path: MarimoPath) -> ExportResult:
@@ -325,7 +325,7 @@ def md(
if sandbox:
from marimo._cli.sandbox import run_in_sandbox
- run_in_sandbox(sys.argv[1:], name)
+ run_in_sandbox(sys.argv[1:], name=name)
return
def export_callback(file_path: MarimoPath) -> ExportResult:
@@ -404,10 +404,6 @@ def ipynb(
"""
Export a marimo notebook as a Jupyter notebook in topological order.
"""
- DependencyManager.nbformat.require(
- why="to convert marimo notebooks to ipynb"
- )
-
import sys
if include_outputs:
@@ -420,9 +416,17 @@ def ipynb(
if sandbox:
from marimo._cli.sandbox import run_in_sandbox
- run_in_sandbox(sys.argv[1:], name)
+ run_in_sandbox(
+ sys.argv[1:],
+ name=name,
+ additional_deps=["nbformat"],
+ )
return
+ DependencyManager.nbformat.require(
+ why="to convert marimo notebooks to ipynb"
+ )
+
def export_callback(file_path: MarimoPath) -> ExportResult:
if include_outputs:
return asyncio_run(
@@ -521,7 +525,7 @@ def html_wasm(
if sandbox:
from marimo._cli.sandbox import run_in_sandbox
- run_in_sandbox(sys.argv[1:], name)
+ run_in_sandbox(sys.argv[1:], name=name)
return
out_dir = output
diff --git a/marimo/_cli/sandbox.py b/marimo/_cli/sandbox.py
index 71b3b74402..bccebaf557 100644
--- a/marimo/_cli/sandbox.py
+++ b/marimo/_cli/sandbox.py
@@ -277,6 +277,7 @@ def construct_uv_command(
args: list[str],
name: str | None,
additional_features: list[DepFeatures],
+ additional_deps: list[str],
) -> list[str]:
cmd = ["marimo"] + args
if "--sandbox" in cmd:
@@ -302,6 +303,9 @@ def construct_uv_command(
dependencies, __version__, additional_features
)
+ # Add additional dependencies
+ dependencies.extend(additional_deps)
+
with tempfile.NamedTemporaryFile(
mode="w", delete=False, suffix=".txt"
) as temp_file:
@@ -357,12 +361,16 @@ def construct_uv_command(
def run_in_sandbox(
args: list[str],
+ *,
name: Optional[str] = None,
additional_features: Optional[list[DepFeatures]] = None,
+ additional_deps: Optional[list[str]] = None,
) -> int:
if not DependencyManager.which("uv"):
raise click.UsageError("uv must be installed to use --sandbox")
- uv_cmd = construct_uv_command(args, name, additional_features or [])
+ uv_cmd = construct_uv_command(
+ args, name, additional_features or [], additional_deps or []
+ )
echo(f"Running in a sandbox: {muted(' '.join(uv_cmd))}")
diff --git a/tests/_cli/test_sandbox.py b/tests/_cli/test_sandbox.py
index 27745a9d77..3e941dc587 100644
--- a/tests/_cli/test_sandbox.py
+++ b/tests/_cli/test_sandbox.py
@@ -367,14 +367,19 @@ def test_is_marimo_dependency():
def test_construct_uv_cmd_marimo_new() -> None:
- uv_cmd = construct_uv_command(["new"], None, additional_features=[])
+ uv_cmd = construct_uv_command(
+ ["new"], None, additional_features=[], additional_deps=[]
+ )
assert "--refresh" in uv_cmd
def test_construct_uv_cmd_marimo_edit_empty_file() -> None:
# a file that doesn't yet exist
uv_cmd = construct_uv_command(
- ["edit", "foo_123.py"], "foo_123.py", additional_features=[]
+ ["edit", "foo_123.py"],
+ "foo_123.py",
+ additional_features=[],
+ additional_deps=[],
)
assert "--refresh" in uv_cmd
assert uv_cmd[0] == "uv"
@@ -386,7 +391,10 @@ def test_construct_uv_cmd_marimo_edit_file_no_sandbox(
) -> None:
# a file that has no inline metadata yet
uv_cmd = construct_uv_command(
- ["edit", temp_marimo_file], temp_marimo_file, additional_features=[]
+ ["edit", temp_marimo_file],
+ temp_marimo_file,
+ additional_features=[],
+ additional_deps=[],
)
assert "--refresh" in uv_cmd
assert uv_cmd[0] == "uv"
@@ -402,6 +410,7 @@ def test_construct_uv_cmd_marimo_edit_sandboxed_file(
["edit", temp_sandboxed_marimo_file],
temp_sandboxed_marimo_file,
additional_features=[],
+ additional_deps=[],
)
assert "--refresh" not in uv_cmd
assert uv_cmd[0] == "uv"
@@ -424,6 +433,7 @@ import marimo
["edit", str(script_path), "--sandbox"],
str(script_path),
additional_features=[],
+ additional_deps=[],
)
assert "--python" in uv_cmd
assert ">=3.11" in uv_cmd
@@ -447,7 +457,10 @@ def test_construct_uv_cmd_with_index_urls() -> None:
with patch("marimo._cli.sandbox.PyProjectReader.from_filename") as mock:
mock.return_value = PyProjectReader(pyproject)
uv_cmd = construct_uv_command(
- ["edit", "test.py", "--sandbox"], "test.py", additional_features=[]
+ ["edit", "test.py", "--sandbox"],
+ "test.py",
+ additional_features=[],
+ additional_deps=[],
)
assert "--index-url" in uv_cmd
assert "https://custom.pypi.org/simple" in uv_cmd
@@ -472,7 +485,10 @@ def test_construct_uv_cmd_with_index_configs() -> None:
with patch("marimo._cli.sandbox.PyProjectReader.from_filename") as mock:
mock.return_value = PyProjectReader(pyproject)
uv_cmd = construct_uv_command(
- ["edit", "test.py", "--sandbox"], "test.py", additional_features=[]
+ ["edit", "test.py", "--sandbox"],
+ name="test.py",
+ additional_features=[],
+ additional_deps=[],
)
assert "--index" in uv_cmd
assert "https://download.pytorch.org/whl/cu124" in uv_cmd
@@ -481,7 +497,10 @@ def test_construct_uv_cmd_with_index_configs() -> None:
def test_construct_uv_cmd_with_sandbox_flag() -> None:
# Test --sandbox flag is removed
uv_cmd = construct_uv_command(
- ["edit", "test.py", "--sandbox"], "test.py", additional_features=[]
+ ["edit", "test.py", "--sandbox"],
+ name="test.py",
+ additional_features=[],
+ additional_deps=[],
)
assert "--sandbox" not in uv_cmd
@@ -491,7 +510,10 @@ def test_construct_uv_cmd_empty_dependencies() -> None:
with patch("marimo._cli.sandbox.PyProjectReader.from_filename") as mock:
mock.return_value = PyProjectReader({})
uv_cmd = construct_uv_command(
- ["edit", "test.py"], "test.py", additional_features=[]
+ ["edit", "test.py"],
+ name="test.py",
+ additional_features=[],
+ additional_deps=[],
)
assert "--refresh" in uv_cmd
assert "--isolated" in uv_cmd
@@ -509,7 +531,9 @@ def test_construct_uv_cmd_with_complex_args() -> None:
"8000",
"--sandbox",
]
- uv_cmd = construct_uv_command(args, "test.py", additional_features=[])
+ uv_cmd = construct_uv_command(
+ args, name="test.py", additional_features=[], additional_deps=[]
+ )
assert "edit" in uv_cmd
assert "test.py" in uv_cmd
assert "--theme" in uv_cmd
@@ -517,3 +541,24 @@ def test_construct_uv_cmd_with_complex_args() -> None:
assert "--port" in uv_cmd
assert "8000" in uv_cmd
assert "--sandbox" not in uv_cmd
+
+
+def test_construct_uv_cmd_with_additional_deps() -> None:
+ # Test additional dependencies are added
+ additional_deps = ["numpy>=1.20.0", "pandas"]
+ uv_cmd = construct_uv_command(
+ ["edit", "test.py"],
+ "test.py",
+ additional_features=[],
+ additional_deps=additional_deps,
+ )
+
+ # Get the requirements file path from the command
+ req_file_index = uv_cmd.index("--with-requirements") + 1
+ req_file_path = uv_cmd[req_file_index]
+
+ # Read the requirements file to verify additional deps were added
+ with open(req_file_path) as f:
+ requirements = f.read()
+ assert "numpy>=1.20.0" in requirements
+ assert "pandas" in requirements