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
tidy: break up the sandbox api for use in quarto (#4679)
Dylan Madisettif03e12dd9614
diff --git a/marimo/_cli/sandbox.py b/marimo/_cli/sandbox.pyindex a7813159eb..788c1f01ea 100644--- a/marimo/_cli/sandbox.py+++ b/marimo/_cli/sandbox.py@@ -274,21 +274,13 @@ def get_marimo_dir() -> Path:return Path(__file__).parent.parent.parent-def construct_uv_command(- args: list[str],- name: str | None,+def construct_uv_flags(+ pyproject: PyProjectReader,+ temp_file: "tempfile._TemporaryFileWrapper[str]", # noqa: UP037additional_features: list[DepFeatures],additional_deps: list[str],) -> list[str]:- cmd = ["marimo"] + args- if "--sandbox" in cmd:- cmd.remove("--sandbox")-- pyproject = (- PyProjectReader.from_filename(name)- if name is not None- else PyProjectReader({})- )+ # NB. Used in quarto plugin# If name if a filepath, parse the dependencies from the filedependencies = pyproject.requirements_txt_lines@@ -307,18 +299,10 @@ def construct_uv_command(# Add additional dependenciesdependencies.extend(additional_deps)- 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))+ temp_file.write("\n".join(dependencies))# Construct base UV command- uv_cmd = [- "uv",- "run",+ uv_flags = ["--isolated",# sandboxed notebook shouldn't pick up existing pyproject.toml,# which may conflict with the sandbox requirements@@ -326,28 +310,28 @@ def construct_uv_command(# trade installation time for faster start time"--compile-bytecode","--with-requirements",- temp_file_path,+ temp_file.name,]# Add refreshif uv_needs_refresh:- uv_cmd.append("--refresh")+ uv_flags.append("--refresh")# Add Python version if specifiedpython_version = pyproject.python_versionif python_version:- uv_cmd.extend(["--python", python_version])+ uv_flags.extend(["--python", python_version])# Add index URL if specifiedindex_url = pyproject.index_urlif index_url:- uv_cmd.extend(["--index-url", index_url])+ uv_flags.extend(["--index-url", index_url])# Add extra-index-urls if specifiedextra_index_urls = pyproject.extra_index_urlsif extra_index_urls:for url in extra_index_urls:- uv_cmd.extend(["--extra-index-url", url])+ uv_flags.extend(["--extra-index-url", url])# Add index configs if specifiedindex_configs = pyproject.index_configs@@ -355,7 +339,38 @@ def construct_uv_command(for config in index_configs:if "url" in config:# Looks like: https://docs.astral.sh/uv/guides/scripts/#using-alternative-package-indexes- uv_cmd.extend(["--index", config["url"]])+ uv_flags.extend(["--index", config["url"]])+ return uv_flags+++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:+ cmd.remove("--sandbox")++ pyproject = (+ PyProjectReader.from_filename(name)+ if name is not None+ else PyProjectReader({})+ )++ uv_cmd = ["uv", "run"]+ with tempfile.NamedTemporaryFile(+ mode="w", delete=False, suffix=".txt"+ ) as temp_file:+ temp_file_path = temp_file.name+ uv_cmd.extend(+ construct_uv_flags(+ pyproject, temp_file, additional_features, additional_deps+ )+ )+ # Clean up the temporary file after the subprocess has run+ atexit.register(lambda: os.unlink(temp_file_path))# Final command assembly: combine the uv prefix with the original marimo# command.