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: automatically add lsp in sandbox mode when running 'marimo edit' (#4279)
Myles Scolnickda7e38350b4d
diff --git a/marimo/_cli/cli.py b/marimo/_cli/cli.pyindex b197eb3760..4082783512 100644--- a/marimo/_cli/cli.py+++ b/marimo/_cli/cli.py@@ -383,7 +383,8 @@ def edit(if sandbox:from marimo._cli.sandbox import run_in_sandbox- run_in_sandbox(sys.argv[1:], name)+ # TODO: consider adding recommended as well+ run_in_sandbox(sys.argv[1:], name, additional_features=["lsp"])returnGLOBAL_SETTINGS.PROFILE_DIR = profile_dir@@ -514,7 +515,8 @@ def new(if sandbox:from marimo._cli.sandbox import run_in_sandbox- run_in_sandbox(sys.argv[1:], None)+ # TODO: consider adding recommended as well+ run_in_sandbox(sys.argv[1:], None, additional_features=["lsp"])returnstart(diff --git a/marimo/_cli/sandbox.py b/marimo/_cli/sandbox.pyindex 509d166f85..71b3b74402 100644--- a/marimo/_cli/sandbox.py+++ b/marimo/_cli/sandbox.py@@ -9,7 +9,7 @@ import subprocessimport sysimport tempfilefrom pathlib import Path-from typing import Any, Optional, cast+from typing import Any, Literal, Optional, castimport click@@ -23,6 +23,8 @@ from marimo._utils.versions import is_editableLOGGER = _loggers.marimo_logger()+DepFeatures = Literal["lsp", "recommended"]+class PyProjectReader:def __init__(self, project: dict[str, Any]):@@ -218,13 +220,26 @@ def _is_versioned(dependency: str) -> bool:def _normalize_sandbox_dependencies(- dependencies: list[str], marimo_version: str+ dependencies: list[str],+ marimo_version: str,+ additional_features: list[DepFeatures],) -> list[str]:"""Normalize marimo dependencies to have only one version.If multiple marimo dependencies exist, prefer the one with brackets.Add version to the remaining one if not already versioned."""++ def include_features(dep: str, features: list[DepFeatures]) -> str:+ if not features:+ return dep++ # If already bracketed, add the features to the existing bracket+ if "[" in dep:+ return dep.replace("marimo[", f"marimo[{','.join(features)},")++ return dep.replace("marimo", f"marimo[{','.join(features)}]")+# Find all marimo dependenciesmarimo_deps = [d for d in dependencies if _is_marimo_dependency(d)]if not marimo_deps:@@ -232,7 +247,9 @@ def _normalize_sandbox_dependencies(LOGGER.info("Using editable of marimo for sandbox")return dependencies + [f"-e {get_marimo_dir()}"]- return dependencies + [f"marimo=={marimo_version}"]+ return dependencies + [+ include_features(f"marimo=={marimo_version}", additional_features)+ ]# Prefer the one with brackets if it existsbracketed = next((d for d in marimo_deps if "[" in d), None)@@ -249,14 +266,18 @@ def _normalize_sandbox_dependencies(if not _is_versioned(chosen):chosen = f"{chosen}=={marimo_version}"- return filtered + [chosen]+ return filtered + [include_features(chosen, additional_features)]def get_marimo_dir() -> Path:return Path(__file__).parent.parent.parent-def construct_uv_command(args: list[str], name: str | None) -> list[str]:+def construct_uv_command(+ args: list[str],+ name: str | None,+ additional_features: list[DepFeatures],+) -> list[str]:cmd = ["marimo"] + argsif "--sandbox" in cmd:cmd.remove("--sandbox")@@ -277,7 +298,9 @@ def construct_uv_command(args: list[str], name: str | None) -> list[str]:uv_needs_refresh = not dependencies# Normalize marimo dependencies- dependencies = _normalize_sandbox_dependencies(dependencies, __version__)+ dependencies = _normalize_sandbox_dependencies(+ dependencies, __version__, additional_features+ )with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".txt"@@ -335,10 +358,11 @@ def construct_uv_command(args: list[str], name: str | None) -> list[str]:def run_in_sandbox(args: list[str],name: Optional[str] = None,+ additional_features: Optional[list[DepFeatures]] = None,) -> int:if not DependencyManager.which("uv"):raise click.UsageError("uv must be installed to use --sandbox")- uv_cmd = construct_uv_command(args, name)+ uv_cmd = construct_uv_command(args, name, additional_features or [])echo(f"Running in a sandbox: {muted(' '.join(uv_cmd))}")diff --git a/tests/_cli/test_sandbox.py b/tests/_cli/test_sandbox.pyindex fedc6b4204..27745a9d77 100644--- a/tests/_cli/test_sandbox.py+++ b/tests/_cli/test_sandbox.py@@ -255,7 +255,9 @@ def test_get_dependencies_with_nonexistent_file():@patch("marimo._cli.sandbox.is_editable", return_value=False)def test_normalize_marimo_dependencies(mock_is_editable: Any):# Test adding marimo when not present- assert _normalize_sandbox_dependencies(["numpy"], "1.0.0") == [+ assert _normalize_sandbox_dependencies(+ ["numpy"], "1.0.0", additional_features=[]+ ) == ["numpy","marimo==1.0.0",]@@ -263,27 +265,54 @@ def test_normalize_marimo_dependencies(mock_is_editable: Any):# Test preferring bracketed versionassert _normalize_sandbox_dependencies(- ["marimo", "marimo[extras]", "numpy"], "1.0.0"+ ["marimo", "marimo[extras]", "numpy"], "1.0.0", additional_features=[]) == ["numpy", "marimo[extras]==1.0.0"]# Test keeping existing version with bracketsassert _normalize_sandbox_dependencies(- ["marimo[extras]>=0.1.0", "numpy"], "1.0.0"+ ["marimo[extras]>=0.1.0", "numpy"], "1.0.0", additional_features=[]) == ["numpy", "marimo[extras]>=0.1.0"]# Test adding version when none existsassert _normalize_sandbox_dependencies(- ["marimo[extras]", "numpy"], "1.0.0"+ ["marimo[extras]", "numpy"], "1.0.0", additional_features=[]) == ["numpy", "marimo[extras]==1.0.0"]# Test keeping only one marimo dependencyassert _normalize_sandbox_dependencies(- ["marimo>=0.1.0", "marimo[extras]>=0.2.0", "numpy"], "1.0.0"+ ["marimo>=0.1.0", "marimo[extras]>=0.2.0", "numpy"],+ "1.0.0",+ additional_features=[],) == ["numpy", "marimo[extras]>=0.2.0"]assert _normalize_sandbox_dependencies(- ["marimo", "marimo[extras]>=0.2.0", "numpy"], "1.0.0"+ ["marimo", "marimo[extras]>=0.2.0", "numpy"],+ "1.0.0",+ additional_features=[],) == ["numpy", "marimo[extras]>=0.2.0"]+ # With additional features+ assert _normalize_sandbox_dependencies(+ ["marimo[extras]", "numpy"], "1.0.0", additional_features=["lsp"]+ ) == ["numpy", "marimo[lsp,extras]==1.0.0"]++ # With multiple additional features+ assert _normalize_sandbox_dependencies(+ ["marimo[extras]", "numpy"],+ "1.0.0",+ additional_features=["lsp", "recommended"],+ ) == ["numpy", "marimo[lsp,recommended,extras]==1.0.0"]++ # With additional features when not present+ assert _normalize_sandbox_dependencies(+ ["marimo", "numpy"], "1.0.0", additional_features=["lsp"]+ ) == ["numpy", "marimo[lsp]==1.0.0"]++ # With duplicate additional features+ # This is ok although it's a bit redundant+ assert _normalize_sandbox_dependencies(+ ["marimo[lsp]", "numpy"], "1.0.0", additional_features=["lsp"]+ ) == ["numpy", "marimo[lsp,lsp]==1.0.0"]+# Test various version specifiers are preservedversion_specs = ["==0.1.0",@@ -295,17 +324,21 @@ def test_normalize_marimo_dependencies(mock_is_editable: Any):]for spec in version_specs:assert _normalize_sandbox_dependencies(- [f"marimo{spec}", "numpy"], "1.0.0"+ [f"marimo{spec}", "numpy"], "1.0.0", additional_features=[]) == ["numpy", f"marimo{spec}"]def test_normalize_marimo_dependencies_editable():- deps = _normalize_sandbox_dependencies(["numpy"], "1.0.0")+ deps = _normalize_sandbox_dependencies(+ ["numpy"], "1.0.0", additional_features=[]+ )assert deps[0] == "numpy"assert deps[1].startswith("-e")assert "marimo" in deps[1]- deps = _normalize_sandbox_dependencies(["numpy", "marimo"], "1.0.0")+ deps = _normalize_sandbox_dependencies(+ ["numpy", "marimo"], "1.0.0", additional_features=[]+ )assert deps[0] == "numpy"assert deps[1].startswith("-e")assert "marimo" in deps[1]@@ -334,13 +367,15 @@ def test_is_marimo_dependency():def test_construct_uv_cmd_marimo_new() -> None:- uv_cmd = construct_uv_command(["new"], None)+ uv_cmd = construct_uv_command(["new"], None, additional_features=[])assert "--refresh" in uv_cmddef test_construct_uv_cmd_marimo_edit_empty_file() -> None:# a file that doesn't yet exist- uv_cmd = construct_uv_command(["edit", "foo_123.py"], "foo_123.py")+ uv_cmd = construct_uv_command(+ ["edit", "foo_123.py"], "foo_123.py", additional_features=[]+ )assert "--refresh" in uv_cmdassert uv_cmd[0] == "uv"assert uv_cmd[1] == "run"@@ -350,7 +385,9 @@ def test_construct_uv_cmd_marimo_edit_file_no_sandbox(temp_marimo_file: str,) -> None:# a file that has no inline metadata yet- uv_cmd = construct_uv_command(["edit", temp_marimo_file], temp_marimo_file)+ uv_cmd = construct_uv_command(+ ["edit", temp_marimo_file], temp_marimo_file, additional_features=[]+ )assert "--refresh" in uv_cmdassert uv_cmd[0] == "uv"assert uv_cmd[1] == "run"@@ -362,7 +399,9 @@ def test_construct_uv_cmd_marimo_edit_sandboxed_file(# a file that has inline metadata; shouldn't refresh the cache, uv# --isolated will do the right thing.uv_cmd = construct_uv_command(- ["edit", temp_sandboxed_marimo_file], temp_sandboxed_marimo_file+ ["edit", temp_sandboxed_marimo_file],+ temp_sandboxed_marimo_file,+ additional_features=[],)assert "--refresh" not in uv_cmdassert uv_cmd[0] == "uv"@@ -382,7 +421,9 @@ import marimo""")uv_cmd = construct_uv_command(- ["edit", str(script_path), "--sandbox"], str(script_path)+ ["edit", str(script_path), "--sandbox"],+ str(script_path),+ additional_features=[],)assert "--python" in uv_cmdassert ">=3.11" in uv_cmd@@ -406,7 +447,7 @@ def test_construct_uv_cmd_with_index_urls() -> None:with patch("marimo._cli.sandbox.PyProjectReader.from_filename") as mock:mock.return_value = PyProjectReader(pyproject)uv_cmd = construct_uv_command(- ["edit", "test.py", "--sandbox"], "test.py"+ ["edit", "test.py", "--sandbox"], "test.py", additional_features=[])assert "--index-url" in uv_cmdassert "https://custom.pypi.org/simple" in uv_cmd@@ -431,7 +472,7 @@ def test_construct_uv_cmd_with_index_configs() -> None:with patch("marimo._cli.sandbox.PyProjectReader.from_filename") as mock:mock.return_value = PyProjectReader(pyproject)uv_cmd = construct_uv_command(- ["edit", "test.py", "--sandbox"], "test.py"+ ["edit", "test.py", "--sandbox"], "test.py", additional_features=[])assert "--index" in uv_cmdassert "https://download.pytorch.org/whl/cu124" in uv_cmd@@ -439,7 +480,9 @@ def test_construct_uv_cmd_with_index_configs() -> None:def test_construct_uv_cmd_with_sandbox_flag() -> None:# Test --sandbox flag is removed- uv_cmd = construct_uv_command(["edit", "test.py", "--sandbox"], "test.py")+ uv_cmd = construct_uv_command(+ ["edit", "test.py", "--sandbox"], "test.py", additional_features=[]+ )assert "--sandbox" not in uv_cmd@@ -447,7 +490,9 @@ def test_construct_uv_cmd_empty_dependencies() -> None:# Test empty dependencies triggers refreshwith patch("marimo._cli.sandbox.PyProjectReader.from_filename") as mock:mock.return_value = PyProjectReader({})- uv_cmd = construct_uv_command(["edit", "test.py"], "test.py")+ uv_cmd = construct_uv_command(+ ["edit", "test.py"], "test.py", additional_features=[]+ )assert "--refresh" in uv_cmdassert "--isolated" in uv_cmdassert "--no-project" in uv_cmd@@ -464,7 +509,7 @@ def test_construct_uv_cmd_with_complex_args() -> None:"8000","--sandbox",]- uv_cmd = construct_uv_command(args, "test.py")+ uv_cmd = construct_uv_command(args, "test.py", additional_features=[])assert "edit" in uv_cmdassert "test.py" in uv_cmdassert "--theme" in uv_cmd