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
improvement: support uv sources when creating a sandbox requirements.txt (#2541)
Myles Scolnick00d0e48aed74
diff --git a/marimo/_cli/sandbox.py b/marimo/_cli/sandbox.pyindex 593481f843..c62e3f39ca 100644--- a/marimo/_cli/sandbox.py+++ b/marimo/_cli/sandbox.py@@ -26,15 +26,85 @@ REGEX = (def _get_dependencies(script: str) -> List[str] | None:- """Get dependencies from string representation of script"""+ """Get dependencies from string representation of script."""try:pyproject = _read_pyproject(script) or {}- return cast(List[str], pyproject.get("dependencies", []))+ return _pyproject_toml_to_requirements_txt(pyproject)except Exception as e:LOGGER.warning(f"Failed to parse dependencies: {e}")return None+def _pyproject_toml_to_requirements_txt(+ pyproject: Dict[str, Any],+) -> List[str]:+ """+ Convert a pyproject.toml file to a requirements.txt file.++ If there is a `[tool.uv.sources]` section, we resolve the dependencies+ to their corresponding source.++ # dependencies = [+ # "python-gcode",+ # ]+ #+ # [tool.uv.sources]+ # python-gcode = { git = "https://github.com/fetlab/python_gcode", rev = "new" }+ """ # noqa: E501+ dependencies = cast(List[str], pyproject.get("dependencies", []))+ if not dependencies:+ return []++ uv_sources = pyproject.get("tool", {}).get("uv", {}).get("sources", {})++ for dependency, source in uv_sources.items():+ # Find the index of the dependency. This may have a version+ # attached, so we cannot do .index()+ dep_index: int | None = None+ for i, dep in enumerate(dependencies):+ if (+ dep == dependency+ or dep.startswith(f"{dependency}==")+ or dep.startswith(f"{dependency}<")+ or dep.startswith(f"{dependency}>")+ or dep.startswith(f"{dependency}~")+ ):+ dep_index = i+ break++ if dep_index is None:+ continue++ new_dependency = None++ # Handle git dependencies+ if "git" in source:+ git_url = f"git+{source['git']}"+ ref = (+ source.get("rev") or source.get("branch") or source.get("tag")+ )+ new_dependency = (+ f"{dependency} @ {git_url}@{ref}"+ if ref+ else f"{dependency} @ {git_url}"+ )+ # Handle local paths+ elif "path" in source:+ new_dependency = f"{dependency} @ {source['path']}"++ # Handle URLs+ elif "url" in source:+ new_dependency = f"{dependency} @ {source['url']}"++ if new_dependency:+ if source.get("marker"):+ new_dependency += f"; {source['marker']}"++ dependencies[dep_index] = new_dependency++ return dependencies++def get_dependencies_from_filename(name: str) -> List[str]:try:contents, _ = FileContentReader().read_file(name)diff --git a/marimo/_smoke_tests/sandbox/git_source.py b/marimo/_smoke_tests/sandbox/git_source.pynew file mode 100644index 0000000000..81a9c68075--- /dev/null+++ b/marimo/_smoke_tests/sandbox/git_source.py@@ -0,0 +1,32 @@+# /// script+# requires-python = ">=3.12"+# dependencies = [+# "marimo",+# "python-gcode==0.1.0",+# ]+#+# [tool.uv.sources]+# python-gcode = { git = "https://github.com/fetlab/python_gcode", rev = "new" }+# ///++import marimo++__generated_with = "0.9.3"+app = marimo.App()+++def foo():+ import marimo as mo+ return (mo,)+++def __():+ import python_gcode+ help(python_gcode)+ return (python_gcode,)+++if __name__ == "__main__":+ app.run()diff --git a/tests/_cli/test_sandbox.py b/tests/_cli/test_sandbox.pyindex 43b59cb599..7a3d386d45 100644--- a/tests/_cli/test_sandbox.py+++ b/tests/_cli/test_sandbox.py@@ -1,6 +1,11 @@from __future__ import annotations-from marimo._cli.sandbox import _get_dependencies+import pytest++from marimo._cli.sandbox import (+ _get_dependencies,+ _pyproject_toml_to_requirements_txt,+)def test_get_dependencies():@@ -37,3 +42,140 @@ __generated_with = "0.8.2"app = marimo.App(width="medium")"""assert _get_dependencies(SCRIPT) == []+++def test_pyproject_toml_to_requirements_txt_git_sources():+ pyproject = {+ "dependencies": [+ "marimo",+ "numpy",+ "polars",+ "altair",+ ],+ "tool": {+ "uv": {+ "sources": {+ "marimo": {+ "git": "https://github.com/marimo-team/marimo.git",+ "rev": "main",+ },+ "numpy": {+ "git": "https://github.com/numpy/numpy.git",+ "branch": "main",+ },+ "polars": {+ "git": "https://github.com/pola/polars.git",+ "branch": "dev",+ },+ }+ }+ },+ }+ assert _pyproject_toml_to_requirements_txt(pyproject) == [+ "marimo @ git+https://github.com/marimo-team/marimo.git@main",+ "numpy @ git+https://github.com/numpy/numpy.git@main",+ "polars @ git+https://github.com/pola/polars.git@dev",+ "altair",+ ]+++def test_pyproject_toml_to_requirements_txt_with_marker():+ pyproject = {+ "dependencies": [+ "marimo",+ "polars",+ ],+ "tool": {+ "uv": {+ "sources": {+ "marimo": {+ "git": "https://github.com/marimo-team/marimo.git",+ "tag": "0.1.0",+ "marker": "python_version >= '3.12'",+ }+ }+ }+ },+ }+ assert _pyproject_toml_to_requirements_txt(pyproject) == [+ "marimo @ git+https://github.com/marimo-team/[email protected]; python_version >= '3.12'", # noqa: E501+ "polars",+ ]+++def test_pyproject_toml_to_requirements_txt_with_url_sources():+ pyproject = {+ "dependencies": [+ "marimo",+ "polars",+ ],+ "tool": {+ "uv": {+ "sources": {+ "marimo": {+ "url": "https://github.com/marimo-team/marimo/archive/refs/heads/main.zip",+ }+ }+ }+ },+ }+ assert _pyproject_toml_to_requirements_txt(pyproject) == [+ "marimo @ https://github.com/marimo-team/marimo/archive/refs/heads/main.zip", # noqa: E501+ "polars",+ ]+++def test_pyproject_toml_to_requirements_txt_with_local_path():+ pyproject = {+ "dependencies": [+ "marimo",+ "polars",+ ],+ "tool": {+ "uv": {+ "sources": {+ "marimo": {+ "path": "/Users/me/work/marimo",+ }+ }+ }+ },+ }+ assert _pyproject_toml_to_requirements_txt(pyproject) == [+ "marimo @ /Users/me/work/marimo",+ "polars",+ ]+++ "version_spec",+ [+ "marimo>=0.1.0",+ "marimo==0.1.0",+ "marimo<=0.1.0",+ "marimo>0.1.0",+ "marimo<0.1.0",+ "marimo~=0.1.0",+ ],+)+def test_pyproject_toml_to_requirements_txt_with_versioned_dependencies(+ version_spec: str,+):+ pyproject = {+ "dependencies": [+ version_spec,+ ],+ "tool": {+ "uv": {+ "sources": {+ "marimo": {+ "git": "https://github.com/marimo-team/marimo.git",+ "rev": "main",+ },+ }+ }+ },+ }+ assert _pyproject_toml_to_requirements_txt(pyproject) == [+ "marimo @ git+https://github.com/marimo-team/marimo.git@main",+ ]