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
feat(cli): add --sandbox flag to `marimo export html` command (#3236)
eugene0dfa8bb28840
diff --git a/marimo/_cli/export/commands.py b/marimo/_cli/export/commands.pyindex 0b44f015f1..522033358d 100644--- a/marimo/_cli/export/commands.py+++ b/marimo/_cli/export/commands.py@@ -132,6 +132,17 @@ Optionally pass CLI args to the notebook:"If not provided, the HTML will be printed to stdout."),)+ "--sandbox",+ is_flag=True,+ default=False,+ show_default=True,+ type=bool,+ help=(+ "Run the command in an isolated virtual environment using "+ "`uv run --isolated`. Requires `uv`."+ ),+)@click.argument("name",required=True,@@ -143,11 +154,19 @@ def html(include_code: bool,output: str,watch: bool,+ sandbox: bool,args: tuple[str],) -> None:- """- Run a notebook and export it as an HTML file.- """+ """Run a notebook and export it as an HTML file."""+ import sys++ from marimo._cli.sandbox import prompt_run_in_sandbox++ if sandbox or prompt_run_in_sandbox(name):+ from marimo._cli.sandbox import run_in_sandbox++ run_in_sandbox(sys.argv[1:], name)+ returncli_args = parse_args(args)@@ -309,6 +328,17 @@ Requires nbformat to be installed.type=bool,help="Run the notebook and include outputs in the exported ipynb file.",)+ "--sandbox",+ is_flag=True,+ default=False,+ show_default=True,+ type=bool,+ help=(+ "Run the command in an isolated virtual environment using "+ "`uv run --isolated`. Requires `uv`."+ ),+)@click.argument("name",required=True,@@ -320,6 +350,7 @@ def ipynb(watch: bool,sort: Literal["top-down", "topological"],include_outputs: bool,+ sandbox: bool,) -> None:"""Export a marimo notebook as a Jupyter notebook in topological order.@@ -328,6 +359,16 @@ def ipynb(why="to convert marimo notebooks to ipynb")+ import sys++ from marimo._cli.sandbox import prompt_run_in_sandbox++ if include_outputs and (sandbox or prompt_run_in_sandbox(name)):+ from marimo._cli.sandbox import run_in_sandbox++ run_in_sandbox(sys.argv[1:], name)+ return+def export_callback(file_path: MarimoPath) -> ExportResult:if include_outputs:return asyncio_run(diff --git a/tests/_cli/test_cli_export.py b/tests/_cli/test_cli_export.pyindex b5434992e7..df32e6a628 100644--- a/tests/_cli/test_cli_export.py+++ b/tests/_cli/test_cli_export.py@@ -3,6 +3,7 @@ from __future__ import annotationsimport asyncioimport inspect+import jsonimport subprocessimport sysfrom os import path@@ -19,6 +20,7 @@ from tests.mocks import snapshotterif TYPE_CHECKING:import pathlib+HAS_UV = DependencyManager.which("uv")snapshot = snapshotter(__file__)@@ -248,6 +250,30 @@ class TestExportHTML:)break+ @staticmethod+ @pytest.mark.skipif(not HAS_UV, reason="uv is required for sandbox tests")+ def test_cli_export_html_sandbox(temp_marimo_file: str) -> None:+ p = subprocess.run(+ [+ "marimo",+ "export",+ "html",+ temp_marimo_file,+ "--sandbox",+ ],+ capture_output=True,+ )+ assert p.returncode == 0, p.stderr.decode()+ output = p.stdout.decode()+ # Check for sandbox message+ assert "Running in a sandbox" in output+ assert "uv run --isolated" in output+ html = normalize_index_html(output)+ # Remove folder path+ dirname = path.dirname(temp_marimo_file)+ html = html.replace(dirname, "path")+ assert '<marimo-code hidden=""></marimo-code>' not in html+class TestExportHtmlSmokeTests:def assert_not_errored(@@ -740,6 +766,56 @@ class TestExportIpynb:output = _delete_lines_with_files(output.replace(__version__, "0.0.0"))snapshot("ipynb_with_errors.txt", output)+ @staticmethod+ @pytest.mark.skipif(+ not HAS_UV or not DependencyManager.nbformat.has(),+ reason="This test requires both uv and nbformat.",+ )+ def test_cli_export_ipynb_sandbox(temp_marimo_file: str) -> None:+ p = subprocess.run(+ [+ "marimo",+ "export",+ "ipynb",+ temp_marimo_file,+ "--sandbox",+ "--include-outputs",+ ],+ capture_output=True,+ )+ assert p.returncode == 0, p.stderr.decode()+ output = p.stdout.decode()+ # Check for sandbox message+ assert "Running in a sandbox" in output+ assert "uv run --isolated" in output++ @staticmethod+ @pytest.mark.skipif(+ not HAS_UV or not DependencyManager.nbformat.has(),+ reason="This test requires both uv and nbformat.",+ )+ def test_cli_export_ipynb_sandbox_no_outputs(+ temp_marimo_file: str,+ ) -> None:+ # Should not use sandbox when not including outputs+ p = subprocess.run(+ [+ "marimo",+ "export",+ "ipynb",+ temp_marimo_file,+ "--sandbox",+ "--no-include-outputs",+ ],+ capture_output=True,+ )+ assert p.returncode == 0, p.stderr.decode()+ output = p.stdout.decode()+ # Should be valid JSON since sandbox is not used+ notebook = json.loads(output)+ assert "cells" in notebook+ assert "nbformat" in notebook+def _delete_lines_with_files(output: str) -> str:return "\n".join(