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(sandbox): Prefer `uv export` for dumping requirements.txt (#5045)
Trevor Manz761c88df06cc
diff --git a/marimo/_cli/sandbox.py b/marimo/_cli/sandbox.pyindex ccf1e8318a..fecfe49757 100644--- a/marimo/_cli/sandbox.py+++ b/marimo/_cli/sandbox.py@@ -124,6 +124,38 @@ def _normalize_sandbox_dependencies(return filtered + [include_features(chosen, additional_features)]+def _uv_export_script_requirements_txt(+ name: str | None,+) -> list[str]:+ if not name:+ return []++ result = subprocess.run(+ [+ "uv",+ "export",+ "--no-hashes",+ "--no-annotate",+ "--no-header",+ "--script",+ name,+ ],+ check=True,+ capture_output=True,+ text=True,+ )+ return result.stdout.split("\n")+++def _resolve_requirements_txt_lines(pyproject: PyProjectReader) -> list[str]:+ if pyproject.name and pyproject.name.endswith(".py"):+ try:+ return _uv_export_script_requirements_txt(pyproject.name)+ except subprocess.CalledProcessError:+ pass # Fall back if uv fails+ return pyproject.requirements_txt_lines++def get_marimo_dir() -> Path:return Path(__file__).parent.parent.parent@@ -137,7 +169,7 @@ def construct_uv_flags(# NB. Used in quarto plugin# If name if a filepath, parse the dependencies from the file- dependencies = pyproject.requirements_txt_lines+ dependencies = _resolve_requirements_txt_lines(pyproject)# If there are no dependencies, which can happen for marimo new or# on marimo edit a_new_file.py, uv may use a cached venv, even though@@ -150,9 +182,6 @@ def construct_uv_flags(dependencies, __version__, additional_features)- # Add additional dependencies- dependencies.extend(additional_deps)-temp_file.write("\n".join(dependencies))# Construct base UV command@@ -167,6 +196,10 @@ def construct_uv_flags(temp_file.name,]+ # Layer additional deps on top of the requirements+ if len(additional_deps) > 0:+ uv_flags.extend(["--with", ",".join(additional_deps)])+# Add refreshif uv_needs_refresh:uv_flags.append("--refresh")diff --git a/marimo/_utils/inline_script_metadata.py b/marimo/_utils/inline_script_metadata.pyindex ebb025f104..99e9a3e3c6 100644--- a/marimo/_utils/inline_script_metadata.py+++ b/marimo/_utils/inline_script_metadata.py@@ -13,20 +13,31 @@ LOGGER = _loggers.marimo_logger()class PyProjectReader:- def __init__(self, project: dict[str, Any], *, config_path: str | None):+ def __init__(+ self,+ project: dict[str, Any],+ *,+ config_path: str | None,+ name: str | None = None,+ ):self.project = projectself.config_path = config_path+ self.name = name@staticmethoddef from_filename(name: str) -> PyProjectReader:return PyProjectReader(- _get_pyproject_from_filename(name) or {}, config_path=name+ name=name,+ project=_get_pyproject_from_filename(name) or {},+ config_path=name,)@staticmethoddef from_script(script: str) -> PyProjectReader:return PyProjectReader(- read_pyproject_from_script(script) or {}, config_path=None+ project=read_pyproject_from_script(script) or {},+ config_path=None,+ name=None,)@propertydiff --git a/tests/_cli/test_sandbox.py b/tests/_cli/test_sandbox.pyindex 27ae4c4e0c..61f5e6a0c2 100644--- a/tests/_cli/test_sandbox.py+++ b/tests/_cli/test_sandbox.py@@ -294,15 +294,12 @@ def test_construct_uv_cmd_with_additional_deps() -> None: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]+ # Get the additional (layered) dependencies+ with_dependencies_index = uv_cmd.index("--with") + 1+ with_dependencies = uv_cmd[with_dependencies_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+ assert "pandas" in with_dependencies+ assert "numpy>=1.20.0" in with_dependenciesdef test_markdown_sandbox(tmp_path: Path) -> None: