Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions frontend/src/components/editor/actions/useNotebookActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
SettingsIcon,
XCircleIcon,
FilePlus2Icon,
FastForwardIcon,
} from "lucide-react";
import { commandPaletteAtom } from "../controls/command-palette";
import {
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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",
Expand Down
8 changes: 7 additions & 1 deletion frontend/src/components/editor/cell/useRunCells.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
*/
Expand Down
12 changes: 9 additions & 3 deletions frontend/src/components/shortcuts/renderShortcut.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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)}>
Expand Down
9 changes: 8 additions & 1 deletion frontend/src/core/edit-app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand All @@ -104,6 +108,9 @@ export const EditApp: React.FC<AppProps> = ({ userConfig, appConfig }) => {
useHotkey("global.hideCode", () => {
togglePresenting();
});
useHotkey("global.runAll", () => {
runAllCells();
});

const editableCellsArray = (
<CellArray
Expand Down
14 changes: 14 additions & 0 deletions frontend/src/core/hotkeys/hotkeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
/**
Expand All @@ -11,6 +13,7 @@ interface Hotkey {
group: HotkeyGroup | undefined;
key:
| string
| typeof NOT_SET
| {
main: string;
/** macOS specific override */
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 9 additions & 1 deletion frontend/src/core/hotkeys/shortcuts.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
Expand Down
Loading