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
fix: detect !pip and give better error message (#7525)
Dylan Madisetti91af3c06a47f
diff --git a/frontend/src/components/editor/chrome/panels/constants.ts b/frontend/src/components/editor/chrome/panels/constants.tsdeleted file mode 100644index 0e7a515c7e..0000000000--- a/frontend/src/components/editor/chrome/panels/constants.ts+++ /dev/null@@ -1,2 +0,0 @@-/* Copyright 2024 Marimo. All rights reserved. */-export const PACKAGES_INPUT_ID = "packages-install-input";diff --git a/frontend/src/components/editor/chrome/panels/packages-panel.tsx b/frontend/src/components/editor/chrome/panels/packages-panel.tsxindex 9d0ccc6647..ec46353197 100644--- a/frontend/src/components/editor/chrome/panels/packages-panel.tsx+++ b/frontend/src/components/editor/chrome/panels/packages-panel.tsx@@ -35,9 +35,8 @@ import { ErrorBanner } from "@/plugins/impl/common/error-banner";import { cn } from "@/utils/cn";import { copyToClipboard } from "@/utils/copy";import { Events } from "@/utils/events";-import { PACKAGES_INPUT_ID } from "./constants";import { PanelEmptyState } from "./empty-state";-import { packagesToInstallAtom } from "./packages-state";+import { PACKAGES_INPUT_ID, packagesToInstallAtom } from "./packages-utils";type ViewMode = "tree" | "list";diff --git a/frontend/src/components/editor/chrome/panels/packages-state.ts b/frontend/src/components/editor/chrome/panels/packages-state.tsdeleted file mode 100644index 04a527a47c..0000000000--- a/frontend/src/components/editor/chrome/panels/packages-state.ts+++ /dev/null@@ -1,5 +0,0 @@-/* Copyright 2024 Marimo. All rights reserved. */-import { atom } from "jotai";--// Atom to store the packages to prefill in the packages panel-export const packagesToInstallAtom = atom<string | null>(null);diff --git a/frontend/src/components/editor/chrome/panels/packages-utils.ts b/frontend/src/components/editor/chrome/panels/packages-utils.tsnew file mode 100644index 0000000000..6fd88c8c9a--- /dev/null+++ b/frontend/src/components/editor/chrome/panels/packages-utils.ts@@ -0,0 +1,26 @@+/* Copyright 2024 Marimo. All rights reserved. */+import { atom } from "jotai";+import type { PanelType } from "../types";++// Atom to store the packages to prefill in the packages panel+export const packagesToInstallAtom = atom<string | null>(null);++export const PACKAGES_INPUT_ID = "packages-install-input";++export const focusPackagesInput = () => {+ requestAnimationFrame(() => {+ const input = document.getElementById(PACKAGES_INPUT_ID);+ if (input) {+ input.focus();+ }+ });+};++type ChromeActions = {+ openApplication: (panel: PanelType) => void;+};++export const openPackageManager = (chromeActions: ChromeActions) => {+ chromeActions.openApplication("packages");+ focusPackagesInput();+};diff --git a/frontend/src/components/editor/output/MarimoErrorOutput.tsx b/frontend/src/components/editor/output/MarimoErrorOutput.tsxindex b44d7c4f25..3327052544 100644--- a/frontend/src/components/editor/output/MarimoErrorOutput.tsx+++ b/frontend/src/components/editor/output/MarimoErrorOutput.tsx@@ -1,6 +1,11 @@/* Copyright 2024 Marimo. All rights reserved. */-import { NotebookPenIcon, SquareArrowOutUpRightIcon } from "lucide-react";+import {+ NotebookPenIcon,+ PackageIcon,+ SquareArrowOutUpRightIcon,+ TerminalIcon,+} from "lucide-react";import { Fragment, type JSX } from "react";import {Accordion,@@ -15,9 +20,11 @@ import type { CellId } from "@/core/cells/ids";import type { MarimoError } from "../../../core/kernel/messages";import { cn } from "../../../utils/cn";import { Alert, AlertTitle } from "../../ui/alert";+import { openPackageManager } from "../chrome/panels/packages-utils";import { useChromeActions } from "../chrome/state";import { AutoFixButton } from "../errors/auto-fix";import { CellLinkError } from "../links/cell-link";+import { processTextForUrls } from "./console/text-rendering";const Tip = (props: {title?: string;@@ -142,14 +149,53 @@ export const MarimoErrorOutput = ({const messages: JSX.Element[] = [];if (syntaxErrors.length > 0 || unknownErrors.length > 0) {+ // Detect if this is a pip/package manager related syntax error+ const hasPipHint = syntaxErrors.some((error) =>+ error.msg.includes("!pip"),+ );+ // Detect if this is a shell command syntax error (not pip)+ const hasShellHint =+ !hasPipHint &&+ syntaxErrors.some((error) => error.msg.includes("use os.subprocess"));++ const openTerminal = () => {+ chromeActions.setIsTerminalOpen(true);+ };+messages.push(<div key="syntax-unknown">{syntaxErrors.map((error, idx) => (- <p key={`syntax-${idx}`}>{error.msg}</p>+ <pre key={`syntax-${idx}`}>+ {processTextForUrls(error.msg, `syntax-${idx}`)}+ </pre>))}{unknownErrors.map((error, idx) => (- <p key={`unknown-${idx}`}>{error.msg}</p>+ <pre key={`unknown-${idx}`}>+ {processTextForUrls(error.msg, `unknown-${idx}`)}+ </pre>))}+ {hasPipHint && (+ <Button+ size="xs"+ variant="outline"+ className="mt-2 font-normal"+ onClick={() => openPackageManager(chromeActions)}+ >+ <PackageIcon className="h-3.5 w-3.5 mr-1.5 mb-0.5" />+ <span>Open package manager</span>+ </Button>+ )}+ {hasShellHint && (+ <Button+ size="xs"+ variant="outline"+ className="mt-2 font-normal"+ onClick={openTerminal}+ >+ <TerminalIcon className="h-3.5 w-3.5 mr-1.5" />+ <span>Open terminal</span>+ </Button>+ )}{cellId && (<AutoFixButtonerrors={[...syntaxErrors, ...unknownErrors]}@@ -439,14 +485,16 @@ export const MarimoErrorOutput = ({<li className="my-2" key={`exception-${idx}`}>{error.raising_cell == null ? (<div>- <p className="text-muted-foreground">{error.msg}</p>+ <p className="text-muted-foreground">+ {processTextForUrls(error.msg, `exception-${idx}`)}+ </p><div className="text-muted-foreground mt-2">See the console area for a traceback.</div></div>) : (<div>- {error.msg}+ {processTextForUrls(error.msg, `exception-${idx}`)}<CellLinkError cellId={error.raising_cell as CellId} /></div>)}diff --git a/frontend/src/components/editor/output/console/text-rendering.tsx b/frontend/src/components/editor/output/console/text-rendering.tsxindex 79d909580d..c5aa3bf7b0 100644--- a/frontend/src/components/editor/output/console/text-rendering.tsx+++ b/frontend/src/components/editor/output/console/text-rendering.tsx@@ -91,7 +91,10 @@ type Replacer = (* Helper function to process text content for URLs and images* This is used by multiple replacers to avoid code duplication*/-function processTextForUrls(text: string, keyPrefix = ""): React.ReactNode {+export function processTextForUrls(+ text: string,+ keyPrefix = "",+): React.ReactNode {if (!text) {return null;}diff --git a/frontend/src/core/codemirror/compat/jupyter.tsx b/frontend/src/core/codemirror/compat/jupyter.tsxindex b4d855efac..2c8cceba5d 100644--- a/frontend/src/core/codemirror/compat/jupyter.tsx+++ b/frontend/src/core/codemirror/compat/jupyter.tsx@@ -1,7 +1,7 @@/* Copyright 2024 Marimo. All rights reserved. */import type { Extension } from "@codemirror/state";import { EditorView } from "@codemirror/view";-import { PACKAGES_INPUT_ID } from "@/components/editor/chrome/panels/constants";+import { focusPackagesInput } from "@/components/editor/chrome/panels/packages-utils";import { chromeAtom } from "@/components/editor/chrome/state";import { Kbd } from "@/components/ui/kbd";import { userConfigAtom } from "@/core/config/config";@@ -60,15 +60,6 @@ export function jupyterHelpExtension(): Extension {);};- const focusPackagesInput = () => {- requestAnimationFrame(() => {- const input = document.getElementById(PACKAGES_INPUT_ID);- if (input) {- input.focus();- }- });- };-const commands: ReplaceCommand[] = [// Package installation{diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.pyindex 5dc1dd4481..77df4facda 100644--- a/marimo/_runtime/runtime.py+++ b/marimo/_runtime/runtime.py@@ -881,8 +881,24 @@ class Kernel:if isinstance(e, ImportStarError):error = MarimoImportStarError(msg=str(e), lineno=lineno)else:++ def _get_syntax_hints(broken_line: str) -> str:+ if broken_line.startswith("!"):+ if "pip" in broken_line:+ return "\nTo install packages, use the package manager panel."+ return "\nTo run shell commands, use os.subprocess(...)"+ elif broken_line.startswith("%"):+ return (+ "\nIPython magic commands (starting with %) are not supported."+ "\nSee: https://links.marimo.app/from-jupyter-magics"+ )+ return ""++ hint = ""+ if len(syntax_error) > 1:+ hint = _get_syntax_hints(syntax_error[1].strip())error = MarimoSyntaxError(- msg="\n".join(syntax_error), lineno=lineno+ msg="\n".join(syntax_error) + hint, lineno=lineno)else:tmpio = io.StringIO()diff --git a/tests/_runtime/test_runtime.py b/tests/_runtime/test_runtime.pyindex d2d7d82b43..f16fefa0e0 100644--- a/tests/_runtime/test_runtime.py+++ b/tests/_runtime/test_runtime.py@@ -949,6 +949,49 @@ except NameError:# Python reports line numbers starting from 1assert k.errors["0"][0].lineno >= 1+ async def test_syntax_error_hint_pip_install(+ self, any_kernel: Kernel+ ) -> None:+ """Test that !pip commands show package manager hint"""+ k = any_kernel+ await k.run(+ [+ ExecutionRequest(cell_id="0", code="!pip install pandas"),+ ]+ )+ assert set(k.errors.keys()) == {"0"}+ assert isinstance(k.errors["0"][0], MarimoSyntaxError)+ assert "package manager panel" in k.errors["0"][0].msg++ async def test_syntax_error_hint_shell_command(+ self, any_kernel: Kernel+ ) -> None:+ """Test that shell commands (not pip) show os.subprocess hint"""+ k = any_kernel+ await k.run(+ [+ ExecutionRequest(cell_id="0", code="!ls -la"),+ ]+ )+ assert set(k.errors.keys()) == {"0"}+ assert isinstance(k.errors["0"][0], MarimoSyntaxError)+ assert "os.subprocess" in k.errors["0"][0].msg++ async def test_syntax_error_hint_ipython_magic(+ self, any_kernel: Kernel+ ) -> None:+ """Test that IPython magic commands show unsupported hint"""+ k = any_kernel+ await k.run(+ [+ ExecutionRequest(cell_id="0", code="%timeit x = 1"),+ ]+ )+ assert set(k.errors.keys()) == {"0"}+ assert isinstance(k.errors["0"][0], MarimoSyntaxError)+ assert "IPython magic commands" in k.errors["0"][0].msg+ assert "not supported" in k.errors["0"][0].msg+async def test_child_of_errored_cell_with_error_not_stale(self,any_kernel: Kernel,