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: sandbox requires marimo (#2177)

Akshay Agrawal00590387bee1
diff --git a/marimo/_cli/sandbox.py b/marimo/_cli/sandbox.py
index 8baf1aa085..c0fce48bd2 100644
--- a/marimo/_cli/sandbox.py
+++ b/marimo/_cli/sandbox.py
@@ -1,11 +1,15 @@
# Copyright 2024 Marimo. All rights reserved.
from __future__ import annotations
+import atexit
import os
import re
import subprocess
+import tempfile
from typing import Any, Dict, List, Optional, cast
+import click
+
from marimo import _loggers
from marimo._dependencies.dependencies import DependencyManager
@@ -20,8 +24,6 @@ def run_in_sandbox(
args: List[str],
name: Optional[str] = None,
) -> subprocess.CompletedProcess[Any]:
- import click
-
if not DependencyManager.which("uv"):
raise click.UsageError("uv must be installed to use --sandbox")
@@ -33,36 +35,29 @@ def run_in_sandbox(
if name is not None and os.path.isfile(name):
with open(name) as f:
dependencies = _get_dependencies(f.read()) or []
- # Add marimo, if it's not already there
- if "marimo" not in dependencies and len(dependencies) > 0:
- dependencies.append("marimo")
-
- if dependencies:
- import tempfile
-
- with tempfile.NamedTemporaryFile(
- mode="w", delete=False, suffix=".txt"
- ) as temp_file:
- temp_file.write("\n".join(dependencies))
- temp_file_path = temp_file.name
-
- cmd = [
- "uv",
- "run",
- "--isolated",
- "--with-requirements",
- temp_file_path,
- ] + cmd
-
- # Clean up the temporary file after the subprocess has run
- import atexit
-
- atexit.register(lambda: os.unlink(temp_file_path))
- else:
- cmd = ["uv", "run", "--isolated"] + cmd
- click.echo(f"Running in a sandbox: {' '.join(cmd)}")
+ # The sandbox needs to manage marimo, too, to make sure
+ # that the outer environment doesn't leak into the sandbox.
+ if "marimo" not in dependencies:
+ dependencies.append("marimo")
+
+ with tempfile.NamedTemporaryFile(
+ mode="w", delete=False, suffix=".txt"
+ ) as temp_file:
+ temp_file.write("\n".join(dependencies))
+ temp_file_path = temp_file.name
+ # Clean up the temporary file after the subprocess has run
+ atexit.register(lambda: os.unlink(temp_file_path))
+
+ cmd = [
+ "uv",
+ "run",
+ "--isolated",
+ "--with-requirements",
+ temp_file_path,
+ ] + cmd
+ click.echo(f"Running in a sandbox: {' '.join(cmd)}")
return subprocess.run(cmd)