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: toast a link to documenation for magic command replacements (#3408)
Myles Scolnick28596118aec2
diff --git a/docs/guides/coming_from/jupyter.md b/docs/guides/coming_from/jupyter.mdindex 112d530065..0fc135616d 100644--- a/docs/guides/coming_from/jupyter.md+++ b/docs/guides/coming_from/jupyter.md@@ -162,6 +162,27 @@ import subprocesssubprocess.run(["ls", "-l"])```+### Common magic commands replacements++| Magic Command | Replacement |+|--------------|-------------|+| %cd | `os.chdir()`, see also [`mo.notebook_dir()`][marimo.notebook_dir] |+| %clear | Right-click or toggle the cell actions |+| %debug | Python's built-in debugger: `breakpoint()` |+| %env | `os.environ` |+| %load | N/A - use Python imports |+| %load_ext | N/A |+| %matplotlib | marimo auto-displays plots |+| %pwd | `os.getcwd()` |+| %who_ls | `dir()`, `globals()`, [`mo.refs()`][marimo.refs], [`mo.defs()`][marimo.defs] |+| %system | `subprocess.run()` |+| %%time | `time.perf_counter()` or Python's timeit module |+| %%timeit | Python's timeit module |+| %%writefile | `with open("file.txt", "w") as f: f.write()` |+| %%capture | [`mo.capture_stdout()`][marimo.capture_stdout], [`mo.capture_stderr()`][marimo.capture_stderr] |+| %%html | [`mo.Html()`][marimo.Html] or [`mo.md()`][marimo.md] |+| %%latex | [`mo.md(r'$$...$$')`][marimo.md] |+### Installing packages with marimo's package managerUse marimo's package management sidebar panel to install packages to your currentdiff --git a/frontend/src/components/ui/use-toast.ts b/frontend/src/components/ui/use-toast.tsindex fb44bbd5d1..59866c78a1 100644--- a/frontend/src/components/ui/use-toast.ts+++ b/frontend/src/components/ui/use-toast.ts@@ -6,7 +6,7 @@ import type { ToastActionElement, ToastProps } from "@/components/ui/toast";const TOAST_LIMIT = 1;const TOAST_REMOVE_DELAY = 10_000; // 10 seconds-type ToasterToast = ToastProps & {+type ToasterToast = Omit<ToastProps, "title"> & {id: string;title?: React.ReactNode;description?: React.ReactNode;@@ -18,6 +18,7 @@ const actionTypes = {UPDATE_TOAST: "UPDATE_TOAST",DISMISS_TOAST: "DISMISS_TOAST",REMOVE_TOAST: "REMOVE_TOAST",+ UPSERT_TOAST: "UPSERT_TOAST",} as const;let count = 0;@@ -45,6 +46,10 @@ type Action =| {type: ActionType["REMOVE_TOAST"];toastId?: ToasterToast["id"];+ }+ | {+ type: ActionType["UPSERT_TOAST"];+ toast: ToasterToast;};interface State {@@ -121,6 +126,23 @@ export const reducer = (state: State, action: Action): State => {...state,toasts: state.toasts.filter((t) => t.id !== action.toastId),};+ case "UPSERT_TOAST": {+ const existingIndex = state.toasts.findIndex(+ (t) => t.id === action.toast.id,+ );+ if (existingIndex > -1) {+ return {+ ...state,+ toasts: state.toasts.map((t) =>+ t.id === action.toast.id ? { ...t, ...action.toast } : t,+ ),+ };+ }+ return {+ ...state,+ toasts: [action.toast, ...state.toasts].slice(0, TOAST_LIMIT),+ };+ }}};@@ -137,21 +159,35 @@ function dispatch(action: Action) {type Toast = Omit<ToasterToast, "id">;-function toast({ ...props }: Toast) {- const id = genId();+function toast({ id, ...props }: Toast & { id?: string }) {+ const toastId = id || genId();const update = (props: Toast) =>dispatch({type: "UPDATE_TOAST",- toast: { ...props, id },+ toast: { ...props, id: toastId },+ });+ const dismiss = () => dispatch({ type: "DISMISS_TOAST", toastId });+ const upsert = (props: Toast) =>+ dispatch({+ type: "UPSERT_TOAST",+ toast: {+ ...props,+ id: toastId,+ open: true,+ onOpenChange: (open) => {+ if (!open) {+ dismiss();+ }+ },+ },});- const dismiss = () => dispatch({ type: "DISMISS_TOAST", toastId: id });dispatch({type: "ADD_TOAST",toast: {...props,- id,+ id: toastId,open: true,onOpenChange: (open) => {if (!open) {@@ -162,9 +198,10 @@ function toast({ ...props }: Toast) {});return {- id: id,+ id: toastId,dismiss,update,+ upsert,};}diff --git a/frontend/src/core/codemirror/compat/__tests__/jupyter.test.ts b/frontend/src/core/codemirror/compat/__tests__/jupyter.test.tsindex 8a47046e3d..64a1da9650 100644--- a/frontend/src/core/codemirror/compat/__tests__/jupyter.test.ts+++ b/frontend/src/core/codemirror/compat/__tests__/jupyter.test.ts@@ -71,8 +71,8 @@ describe("jupyterHelpExtension", () => {// Test autorun modeview.dispatch({- changes: { from: 0, insert: "%load_ext autoreload 2" },- selection: { anchor: "%load_ext autoreload 2".length },+ changes: { from: 0, insert: "%autoreload 2" },+ selection: { anchor: "%autoreload 2".length },});expect(saveUserConfig).toHaveBeenCalledWith({@@ -83,8 +83,8 @@ describe("jupyterHelpExtension", () => {// Test lazy modeview.dispatch({- changes: { from: 0, insert: "%load_ext autoreload 1" },- selection: { anchor: "%load_ext autoreload 1".length },+ changes: { from: 0, insert: "%autoreload 1" },+ selection: { anchor: "%autoreload 1".length },});expect(saveUserConfig).toHaveBeenCalledWith({@@ -95,8 +95,8 @@ describe("jupyterHelpExtension", () => {// Test off modeview.dispatch({- changes: { from: 0, insert: "%load_ext autoreload 0" },- selection: { anchor: "%load_ext autoreload 0".length },+ changes: { from: 0, insert: "%autoreload 0" },+ selection: { anchor: "%autoreload 0".length },});expect(saveUserConfig).toHaveBeenCalledWith({diff --git a/frontend/src/core/codemirror/compat/jupyter.tsx b/frontend/src/core/codemirror/compat/jupyter.tsxindex db76bc7777..0e273cd5b2 100644--- a/frontend/src/core/codemirror/compat/jupyter.tsx+++ b/frontend/src/core/codemirror/compat/jupyter.tsx@@ -8,14 +8,40 @@ import { saveUserConfig } from "@/core/network/requests";import type { UserConfig } from "@/core/config/config-schema";import { userConfigAtom } from "@/core/config/config";import { PACKAGES_INPUT_ID } from "@/components/editor/chrome/panels/constants";+import { Kbd } from "@/components/ui/kbd";interface ReplaceCommand {match: string;onMatch: () => void;- title: string;+ title: React.ReactNode;description: React.ReactNode;}+const COMMON_MAGIC_COMMANDS = [+ // Line magics+ "%alias",+ "%cd",+ "%clear",+ "%debug",+ "%env",+ "%load",+ "%load_ext",+ "%lsmagic",+ "%matplotlib",+ "%pwd",+ "%who_ls",+ "%system",+ // Cell magics+ "%%time",+ "%%timeit",+ "%%writefile",+ "%%capture",+ "%%html",+ "%%latex",+];++let toastResponse: ReturnType<typeof toast>;+export function jupyterHelpExtension(): Extension {// Function to update the module reload mode// Updates local config and saves to server@@ -43,6 +69,7 @@ export function jupyterHelpExtension(): Extension {};const commands: ReplaceCommand[] = [+ // Package installation{match: "!pip install",onMatch: () => {@@ -83,8 +110,10 @@ export function jupyterHelpExtension(): Extension {</>),},++ // Module reload{- match: "%load_ext autoreload 2",+ match: "%autoreload 2",onMatch: async () => {await handleUpdateModuleReload("autorun");},@@ -97,7 +126,7 @@ export function jupyterHelpExtension(): Extension {),},{- match: "%load_ext autoreload 1",+ match: "%autoreload 1",onMatch: async () => {await handleUpdateModuleReload("lazy");},@@ -110,7 +139,7 @@ export function jupyterHelpExtension(): Extension {),},{- match: "%load_ext autoreload 0",+ match: "%autoreload 0",onMatch: async () => {await handleUpdateModuleReload("off");},@@ -119,6 +148,8 @@ export function jupyterHelpExtension(): Extension {<>Module reload disabled - module changes will not be detected.</>),},++ // Shell commands{match: "!ls",onMatch: () => {@@ -133,6 +164,30 @@ export function jupyterHelpExtension(): Extension {</>),},++ // Common magic commands+ ...COMMON_MAGIC_COMMANDS.map((cmd) => ({+ match: cmd,+ onMatch: () => {+ // noop+ },+ title: <Kbd className="inline">{cmd}</Kbd>,+ description: (+ <>+ Magic commands are not supported in marimo. Read more about+ replacements in the{" "}+ <a+ href="https://docs.marimo.io/guides/coming_from/jupyter/#adapting-to-the-absence-of-magic-commands"+ target="_blank"+ className="text-link underline cursor-pointer"+ rel="noopener noreferrer"+ >+ documentation+ </a>+ .+ </>+ ),+ })),];const commandMap = new Map(commands.map((cmd) => [cmd.match, cmd]));@@ -151,10 +206,17 @@ export function jupyterHelpExtension(): Extension {cmd.onMatch();// Show toast- toast({- title: cmd.title,- description: cmd.description,- });+ if (toastResponse) {+ toastResponse.upsert({+ title: cmd.title,+ description: cmd.description,+ });+ } else {+ toastResponse = toast({+ title: cmd.title,+ description: cmd.description,+ });+ }}}});