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.

feat: re-run all cells action (useful for external queries/dashboards) (#3448)

Myles Scolnick31dc39b45c1a
diff --git a/frontend/src/components/editor/actions/useNotebookActions.tsx b/frontend/src/components/editor/actions/useNotebookActions.tsx
index aeaf79572d..bed35d2288 100644
--- a/frontend/src/components/editor/actions/useNotebookActions.tsx
+++ b/frontend/src/components/editor/actions/useNotebookActions.tsx
@@ -29,6 +29,7 @@ import {
SettingsIcon,
XCircleIcon,
FilePlus2Icon,
+ FastForwardIcon,
} from "lucide-react";
import { commandPaletteAtom } from "../controls/command-palette";
import {
@@ -69,6 +70,7 @@ import { settingDialogAtom } from "@/components/app-config/app-config-button";
import { renderShortcut } from "@/components/shortcuts/renderShortcut";
import { copyToClipboard } from "@/utils/copy";
import { newNotebookURL } from "@/utils/urls";
+import { useRunAllCells } from "../cell/useRunCells";
const NOOP_HANDLER = (event?: Event) => {
event?.preventDefault();
@@ -86,6 +88,7 @@ export function useNotebookActions() {
const { updateCellConfig, undoDeleteCell, clearAllCellOutputs } =
useCellActions();
const restartKernel = useRestartKernel();
+ const runAllCells = useRunAllCells();
const copyNotebook = useCopyNotebook(filename);
const setCommandPaletteOpen = useSetAtom(commandPaletteAtom);
const setSettingsDialogOpen = useSetAtom(settingDialogAtom);
@@ -357,6 +360,14 @@ export function useNotebookActions() {
clearAllCellOutputs();
},
},
+ {
+ icon: <FastForwardIcon size={14} strokeWidth={1.5} />,
+ label: "Re-run all cells",
+ hotkey: "global.runAll",
+ handle: async () => {
+ runAllCells();
+ },
+ },
{
icon: <Undo2Icon size={14} strokeWidth={1.5} />,
label: "Undo cell deletion",
diff --git a/frontend/src/components/editor/cell/useRunCells.ts b/frontend/src/components/editor/cell/useRunCells.ts
index 8693bb2839..f8856c9173 100644
--- a/frontend/src/components/editor/cell/useRunCells.ts
+++ b/frontend/src/components/editor/cell/useRunCells.ts
@@ -5,7 +5,7 @@ import { getNotebook, useCellActions } from "@/core/cells/cells";
import useEvent from "react-use-event-hook";
import { getEditorCodeAsPython } from "@/core/codemirror/language/utils";
import { Logger } from "@/utils/Logger";
-import { staleCellIds } from "@/core/cells/utils";
+import { enabledCellIds, staleCellIds } from "@/core/cells/utils";
/**
* Creates a function that runs all cells that have been edited or interrupted.
@@ -30,6 +30,12 @@ export function useRunCell(cellId: CellId | undefined) {
return runCell;
}
+export function useRunAllCells() {
+ const runCells = useRunCells();
+ const runAllCells = useEvent(() => runCells(enabledCellIds(getNotebook())));
+ return runAllCells;
+}
+
/**
* Creates a function that runs the given cells.
*/
diff --git a/frontend/src/components/shortcuts/renderShortcut.tsx b/frontend/src/components/shortcuts/renderShortcut.tsx
index 3e6e1757ab..f39b74dcfc 100644
--- a/frontend/src/components/shortcuts/renderShortcut.tsx
+++ b/frontend/src/components/shortcuts/renderShortcut.tsx
@@ -1,5 +1,5 @@
/* Copyright 2024 Marimo. All rights reserved. */
-import type { HotkeyAction } from "@/core/hotkeys/hotkeys";
+import { type HotkeyAction, NOT_SET } from "@/core/hotkeys/hotkeys";
import { isPlatformMac } from "@/core/hotkeys/shortcuts";
import { Kbd } from "../ui/kbd";
import { DropdownMenuShortcut } from "../ui/dropdown-menu";
@@ -29,8 +29,11 @@ const Shortcut: React.FC<{ shortcut: HotkeyAction; includeName?: boolean }> = ({
export const KeyboardHotkeys: React.FC<{
className?: string;
- shortcut: string;
+ shortcut: string | typeof NOT_SET;
}> = ({ shortcut, className }) => {
+ if (shortcut === NOT_SET || shortcut === "") {
+ return <span />;
+ }
const keys = shortcut.split("-");
return (
@@ -71,8 +74,11 @@ export const MinimalShortcut: React.FC<{
export const MinimalHotkeys: React.FC<{
className?: string;
- shortcut: string;
+ shortcut: string | typeof NOT_SET;
}> = ({ shortcut, className }) => {
+ if (shortcut === NOT_SET || shortcut === "") {
+ return <span />;
+ }
const keys = shortcut.split("-");
return (
<DropdownMenuShortcut className={cn("flex gap-1 items-center", className)}>
diff --git a/frontend/src/core/edit-app.tsx b/frontend/src/core/edit-app.tsx
index b36a37b3b6..8adc0539c2 100644
--- a/frontend/src/core/edit-app.tsx
+++ b/frontend/src/core/edit-app.tsx
@@ -22,7 +22,10 @@ import { CellArray } from "../components/editor/renderers/CellArray";
import { RuntimeState } from "./kernel/RuntimeState";
import { CellsRenderer } from "../components/editor/renderers/cells-renderer";
import { useAtomValue, useSetAtom } from "jotai";
-import { useRunStaleCells } from "../components/editor/cell/useRunCells";
+import {
+ useRunAllCells,
+ useRunStaleCells,
+} from "../components/editor/cell/useRunCells";
import { cn } from "@/utils/cn";
import { useFilename } from "./saving/filename";
import { getSessionId } from "./kernel/session";
@@ -92,6 +95,7 @@ export const EditApp: React.FC<AppProps> = ({ userConfig, appConfig }) => {
}, [appConfig.width, previousWidth, mergeAllColumns, numColumns]);
const runStaleCells = useRunStaleCells();
+ const runAllCells = useRunAllCells();
const togglePresenting = useTogglePresenting();
// HOTKEYS
@@ -104,6 +108,9 @@ export const EditApp: React.FC<AppProps> = ({ userConfig, appConfig }) => {
useHotkey("global.hideCode", () => {
togglePresenting();
});
+ useHotkey("global.runAll", () => {
+ runAllCells();
+ });
const editableCellsArray = (
<CellArray
diff --git a/frontend/src/core/hotkeys/hotkeys.ts b/frontend/src/core/hotkeys/hotkeys.ts
index 5aefdf54d8..aac24ef105 100644
--- a/frontend/src/core/hotkeys/hotkeys.ts
+++ b/frontend/src/core/hotkeys/hotkeys.ts
@@ -2,6 +2,8 @@
import { isPlatformMac } from "@/core/hotkeys/shortcuts";
import { Objects } from "@/utils/objects";
+export const NOT_SET: unique symbol = Symbol("NOT_SET");
+
interface Hotkey {
name: string;
/**
@@ -11,6 +13,7 @@ interface Hotkey {
group: HotkeyGroup | undefined;
key:
| string
+ | typeof NOT_SET
| {
main: string;
/** macOS specific override */
@@ -117,6 +120,11 @@ const DEFAULT_HOT_KEY = {
group: "Running Cells",
key: "Mod-Shift-Enter",
},
+ "global.runAll": {
+ name: "Re-run all cells",
+ group: "Running Cells",
+ key: NOT_SET,
+ },
// Editing Cells
"cell.format": {
@@ -381,6 +389,12 @@ export class HotkeyProvider implements IHotkeyProvider {
key: key.replace("Mod", this.mod),
};
}
+ if (key === NOT_SET) {
+ return {
+ name,
+ key: "",
+ };
+ }
const platformKey = key[this.platform] || key.main;
return {
name,
diff --git a/frontend/src/core/hotkeys/shortcuts.ts b/frontend/src/core/hotkeys/shortcuts.ts
index df6def122f..b247667266 100644
--- a/frontend/src/core/hotkeys/shortcuts.ts
+++ b/frontend/src/core/hotkeys/shortcuts.ts
@@ -1,6 +1,7 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { Logger } from "@/utils/Logger";
+import { NOT_SET } from "./hotkeys";
/**
* Check if the current platform is Mac
@@ -76,7 +77,14 @@ function normalizeKey(key: string): string {
return specialKeys[key.toLowerCase()] || key.toLowerCase();
}
-export function parseShortcut(shortcut: string): (e: KeyboardEvent) => boolean {
+export function parseShortcut(
+ shortcut: string | typeof NOT_SET,
+): (e: KeyboardEvent) => boolean {
+ // Handle empty shortcut, e.g. not set
+ if (shortcut === NOT_SET || shortcut === "") {
+ return () => false;
+ }
+
const separator = shortcut.includes("+") ? "+" : "-";
const keys = shortcut.split(separator).map(normalizeKey);
return (e: KeyboardEvent) => areKeysPressed(keys, e);