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.

improvement: make ruff optional, fallback to global ruff, prompt to install (#4731)

Myles Scolnickc6c4694595e1
diff --git a/marimo/_server/errors.py b/marimo/_server/errors.py
index 9a37fd922c..2e0f4171a6 100644
--- a/marimo/_server/errors.py
+++ b/marimo/_server/errors.py
@@ -50,6 +50,7 @@ async def handle_error(request: Request, response: Any) -> Any:
status_code=response.status_code,
)
if isinstance(response, ModuleNotFoundError) and response.name:
+ LOGGER.warning(response.msg) # print to terminal
try:
app_state = AppState(request)
session_id = app_state.get_current_session_id()
diff --git a/marimo/_server/lsp.py b/marimo/_server/lsp.py
index 62ddc4d7b4..138845c0a7 100644
--- a/marimo/_server/lsp.py
+++ b/marimo/_server/lsp.py
@@ -70,10 +70,8 @@ class BaseLspServer(LspServer):
stdout=subprocess.PIPE
if GLOBAL_SETTINGS.DEVELOPMENT_MODE
else subprocess.DEVNULL,
- # pipe error output
- # temporarily comment to make lsp-server work on windows
- # TODO! Find a nice way to keep stderr in future
- # stderr=subprocess.PIPE,
+ # subprocess.PIPE in Windows breaks the lsp-server
+ stderr=subprocess.DEVNULL,
stdin=None,
text=True,
)
diff --git a/marimo/_server/main.py b/marimo/_server/main.py
index 9b2f86c045..c78f247a10 100644
--- a/marimo/_server/main.py
+++ b/marimo/_server/main.py
@@ -105,6 +105,7 @@ def create_starlette_app(
Exception: handle_error,
HTTPException: handle_error,
MarimoHTTPException: handle_error,
+ ModuleNotFoundError: handle_error,
},
)
diff --git a/marimo/_utils/formatter.py b/marimo/_utils/formatter.py
index 98503f6d60..7f6e0199be 100644
--- a/marimo/_utils/formatter.py
+++ b/marimo/_utils/formatter.py
@@ -15,13 +15,19 @@ CellCodes = dict[CellId_t, str]
def ruff(codes: CellCodes, *cmd: str) -> CellCodes:
+ # Try with sys.executable first
ruff_cmd = [sys.executable, "-m", "ruff"]
process = subprocess.run([*ruff_cmd, "--help"], capture_output=True)
+
+ # If that fails, try global ruff
if process.returncode != 0:
- LOGGER.warning(
- "To enable code formatting, install ruff (pip install ruff)"
- )
- return {}
+ ruff_cmd = ["ruff"]
+ process = subprocess.run([*ruff_cmd, "--help"], capture_output=True)
+ if process.returncode != 0:
+ raise ModuleNotFoundError(
+ "To enable code formatting, install ruff (pip install ruff)",
+ name="ruff",
+ )
formatted_codes: CellCodes = {}
for key, code in codes.items():
@@ -63,16 +69,19 @@ class DefaultFormatter(Formatter):
"""
def format(self, codes: CellCodes) -> CellCodes:
- if DependencyManager.ruff.has():
+ # Ruff may be installed in venv or globally
+ if DependencyManager.ruff.has() or DependencyManager.which("ruff"):
return RuffFormatter(self.line_length).format(codes)
+ # Black must be installed in venv
elif DependencyManager.black.has():
return BlackFormatter(self.line_length).format(codes)
else:
- LOGGER.warning(
+ raise ModuleNotFoundError(
"To enable code formatting, install ruff (pip install ruff) "
- "or black (pip install black)"
+ "or black (pip install black).",
+ # This asks the frontend to install ruff
+ name="ruff",
)
- return {}
class RuffFormatter(Formatter):
@@ -82,14 +91,9 @@ class RuffFormatter(Formatter):
class BlackFormatter(Formatter):
def format(self, codes: CellCodes) -> CellCodes:
- try:
- import black
- except ModuleNotFoundError:
- LOGGER.warning(
- "To enable code formatting, install ruff (pip install ruff) "
- "or black (pip install black)"
- )
- return {}
+ DependencyManager.black.require("to enable code formatting")
+
+ import black
formatted_codes: CellCodes = {}
for key, code in codes.items():
diff --git a/pyproject.toml b/pyproject.toml
index 78832401ec..5cdbb8c5d2 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -46,9 +46,6 @@ dependencies = [
"itsdangerous>=2.0.0",
# for dataframe support
"narwhals>=1.12.0",
- # for cell formatting; if user version is not compatible, no-op
- # so no lower bound needed
- "ruff",
# for packaging.version; not sure what the lower bound is.
"packaging",
]