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 flag to configure whether a notebook is trusted (#7556)
Akshay Agrawalaa2f8c6e9144
diff --git a/marimo/_cli/cli.py b/marimo/_cli/cli.pyindex 18782ff1b4..31d52b6feb 100644--- a/marimo/_cli/cli.py+++ b/marimo/_cli/cli.py@@ -339,6 +339,14 @@ such as incorrectly overwriting package requirements or failing to write outrequirements. These and other issues are described inhttps://github.com/marimo-team/marimo/issues/5219.""",)+ "--trusted/--untrusted",+ is_flag=True,+ default=None,+ show_default=False,+ type=bool,+ help="Run notebooks hosted remotely on the host machine; if --untrusted, runs marimo in a Docker container.",+)@click.option("--profile-dir", default=None, type=str, hidden=True)@click.option("--watch",@@ -421,6 +429,7 @@ def edit(skip_update_check: bool,sandbox: Optional[bool],dangerous_sandbox: Optional[bool],+ trusted: Optional[bool],profile_dir: Optional[str],watch: bool,skew_protection: bool,@@ -448,10 +457,7 @@ def edit()name = path.absolute_name- # If file is a url, we prompt to run in docker- # We only do this for remote files,- # but later we can make this a CLI flag- if name is not None and prompt_run_in_docker_container(name):+ if prompt_run_in_docker_container(name, trusted=trusted):from marimo._cli.run_docker import run_in_dockerrun_in_docker(@@ -884,6 +890,14 @@ Example:type=bool,help=check_message,)+ "--trusted/--untrusted",+ is_flag=True,+ default=None,+ show_default=False,+ type=bool,+ help="Run notebooks hosted remotely on the host machine; if --untrusted, runs marimo in a Docker container.",+)@click.option("--server-startup-command",default=None,@@ -921,6 +935,7 @@ def run(redirect_console_to_browser: bool,sandbox: Optional[bool],check: bool,+ trusted: Optional[bool],server_startup_command: Optional[str],asset_url: Optional[str],name: str,@@ -928,10 +943,7 @@ def run() -> None:from marimo._cli.sandbox import run_in_sandbox, should_run_in_sandbox- # If file is a url, we prompt to run in docker- # We only do this for remote files,- # but later we can make this a CLI flag- if prompt_run_in_docker_container(name):+ if prompt_run_in_docker_container(name, trusted=trusted):from marimo._cli.run_docker import run_in_dockerrun_in_docker(diff --git a/marimo/_cli/run_docker.py b/marimo/_cli/run_docker.pyindex 5715078a86..02860f72a0 100644--- a/marimo/_cli/run_docker.py+++ b/marimo/_cli/run_docker.py@@ -16,16 +16,18 @@ from marimo._utils.url import is_urlLOGGER = _loggers.marimo_logger()-def prompt_run_in_docker_container(name: str | None) -> bool:+def prompt_run_in_docker_container(+ name: str | None, trusted: Optional[bool] = None+) -> bool:if GLOBAL_SETTINGS.IN_SECURE_ENVIRONMENT:return Falseif GLOBAL_SETTINGS.MANAGE_SCRIPT_METADATA:return False- # Only prompt for remote files- if name is None:- return False- if not is_url(name):+ if trusted is not None:+ return not trusted++ if name is None or not is_url(name):return Falseif GLOBAL_SETTINGS.YES:@@ -92,7 +94,7 @@ def _check_port_in_use(port: int) -> Optional[str]:def run_in_docker(- file_path: str,+ file_path: str | None,mode: Literal["edit", "run"],*,port: Optional[int],@@ -140,7 +142,7 @@ def run_in_docker(f"{port}","--host",host,- file_path,+ file_path if file_path is not None else "",]# Remove empty strings from commandcontainer_command = [arg for arg in container_command if arg]diff --git a/tests/_cli/test_cli.py b/tests/_cli/test_cli.pyindex 017fa3cf04..cca33b2990 100644--- a/tests/_cli/test_cli.py+++ b/tests/_cli/test_cli.py@@ -1345,6 +1345,87 @@ def test_cli_run_docker_remote_url():assert "Docker is not installed" in p.stdout.read().decode()+def test_cli_edit_trusted(temp_marimo_file: str) -> None:+ # --trusted should work normally with local files+ port = _get_port()+ p = subprocess.Popen(+ [+ "marimo",+ "edit",+ temp_marimo_file,+ "-p",+ str(port),+ "--headless",+ "--no-token",+ "--trusted",+ ]+ )+ contents = _try_fetch(port)+ _check_contents(p, b'"mode": "edit"', contents)+++ HAS_DOCKER, reason="docker is required to be not installed"+)+def test_cli_edit_no_trusted(temp_marimo_file: str) -> None:+ # --untrusted should try to use Docker, fail if not installed+ p = subprocess.Popen(+ [+ "marimo",+ "edit",+ temp_marimo_file,+ "--untrusted",+ ],+ stdout=subprocess.PIPE,+ stderr=subprocess.PIPE,+ )++ # Should fail with missing docker+ assert p.returncode != 0+ assert p.stdout is not None+ assert "Docker is not installed" in p.stdout.read().decode()+++def test_cli_run_trusted(temp_marimo_file: str) -> None:+ # --trusted should work normally with local files+ port = _get_port()+ p = subprocess.Popen(+ [+ "marimo",+ "run",+ temp_marimo_file,+ "-p",+ str(port),+ "--headless",+ "--trusted",+ ]+ )+ contents = _try_fetch(port)+ _check_contents(p, b'"mode": "read"', contents)+++ HAS_DOCKER, reason="docker is required to be not installed"+)+def test_cli_run_no_trusted(temp_marimo_file: str) -> None:+ # --untrusted should try to use Docker, fail if not installed+ p = subprocess.Popen(+ [+ "marimo",+ "run",+ temp_marimo_file,+ "--untrusted",+ ],+ stdout=subprocess.PIPE,+ stderr=subprocess.PIPE,+ )++ # Should fail with missing docker+ assert p.returncode != 0+ assert p.stdout is not None+ assert "Docker is not installed" in p.stdout.read().decode()++def test_cli_edit_with_convert(temp_possible_file: str,) -> None: