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
show notebook actions in command pallete (#119)
Myles Scolnick636a2e43f957
diff --git a/frontend/src/core/hotkeys/hotkeys.ts b/frontend/src/core/hotkeys/hotkeys.tsindex 3a20d29c59..c37795c641 100644--- a/frontend/src/core/hotkeys/hotkeys.ts+++ b/frontend/src/core/hotkeys/hotkeys.ts@@ -232,6 +232,10 @@ const DEFAULT_HOT_KEY = {export type HotkeyAction = keyof typeof DEFAULT_HOT_KEY;+export function isHotkeyAction(x: string): x is HotkeyAction {+ return x in DEFAULT_HOT_KEY;+}+export interface IHotkeyProvider {getHotkey(action: HotkeyAction): ResolvedHotkey;}diff --git a/frontend/src/editor/CommandPallette.tsx b/frontend/src/editor/CommandPallette.tsxindex eb0e06aad9..e637407016 100644--- a/frontend/src/editor/CommandPallette.tsx+++ b/frontend/src/editor/CommandPallette.tsx@@ -14,15 +14,27 @@ import { useRegisteredActions } from "../core/state/actions";import { useRecentCommands } from "../hooks/useRecentCommands";import { Kbd } from "../components/ui/kbd";import { prettyPrintHotkey } from "../components/shortcuts/renderShortcut";-import { HOTKEYS, HotkeyAction } from "@/core/hotkeys/hotkeys";+import { HOTKEYS, HotkeyAction, isHotkeyAction } from "@/core/hotkeys/hotkeys";import { atom, useAtom } from "jotai";+import { useNotebookActions } from "./actions/useNotebookActions";+import { Objects } from "@/utils/objects";export const commandPalletteAtom = atom(false);export const CommandPallette = () => {const [open, setOpen] = useAtom(commandPalletteAtom);const registeredActions = useRegisteredActions();+ const notebookActions = useNotebookActions({});+ const notebookActionsWithoutHotkeys = notebookActions.filter(+ (action) => !action.hotkey+ );+ const keyedNotebookActions = Objects.keyBy(+ notebookActionsWithoutHotkeys,+ (action) => action.label+ );+const { recentCommands, addRecentCommand } = useRecentCommands();+ const recentCommandsSet = new Set(recentCommands);React.useEffect(() => {const down = (e: KeyboardEvent) => {@@ -35,14 +47,6 @@ export const CommandPallette = () => {return () => document.removeEventListener("keydown", down);}, [setOpen]);- const renderShortcutCommandItemIfNotRecent = (shortcut: HotkeyAction) => {- const isRecent = recentCommands.includes(shortcut);- if (isRecent) {- return null;- }- return renderShortcutCommandItem(shortcut);- };-const renderShortcutCommandItem = (shortcut: HotkeyAction) => {const action = registeredActions[shortcut];if (!action) {@@ -75,6 +79,24 @@ export const CommandPallette = () => {);};+ const renderCommandItem = (label: string, handle: () => void) => {+ return (+ <CommandItem+ onSelect={() => {+ addRecentCommand(label);+ setOpen(false);+ requestAnimationFrame(() => {+ handle();+ });+ }}+ key={label}+ value={label}+ >+ <span>{label}</span>+ </CommandItem>+ );+ };+return (<CommandDialog open={open} onOpenChange={setOpen}><CommandInput placeholder="Type to search..." />@@ -83,17 +105,35 @@ export const CommandPallette = () => {{recentCommands.length > 0 && (<><CommandGroup heading="Recently Used">- {recentCommands.map((shortcut) =>- renderShortcutCommandItem(shortcut)- )}+ {recentCommands.map((shortcut) => {+ // Hotkey+ if (isHotkeyAction(shortcut)) {+ return renderShortcutCommandItem(shortcut);+ }+ // Other action+ const action = keyedNotebookActions[shortcut];+ if (action) {+ return renderCommandItem(action.label, action.handle);+ }+ return null;+ })}</CommandGroup><CommandSeparator /></>)}<CommandGroup heading="Commands">- {HOTKEYS.iterate().map((shortcut) =>- renderShortcutCommandItemIfNotRecent(shortcut)- )}+ {HOTKEYS.iterate().map((shortcut) => {+ if (recentCommandsSet.has(shortcut)) {+ return null; // Don't show recent commands in the main list+ }+ return renderShortcutCommandItem(shortcut);+ })}+ {notebookActionsWithoutHotkeys.map((action) => {+ if (recentCommandsSet.has(action.label)) {+ return null; // Don't show recent commands in the main list+ }+ return renderCommandItem(action.label, action.handle);+ })}</CommandGroup></CommandList></CommandDialog>diff --git a/frontend/src/hooks/useRecentCommands.ts b/frontend/src/hooks/useRecentCommands.tsindex b6b6f1714a..f6b47baeda 100644--- a/frontend/src/hooks/useRecentCommands.ts+++ b/frontend/src/hooks/useRecentCommands.ts@@ -3,16 +3,17 @@ import { HotkeyAction } from "@/core/hotkeys/hotkeys";import { useLocalStorage } from "./useLocalStorage";const MAX_RECENT_COMMANDS = 3;+type RecentCommandId = HotkeyAction | string;export function useRecentCommands() {- const [commands, setCommands] = useLocalStorage<HotkeyAction[]>(+ const [commands, setCommands] = useLocalStorage<RecentCommandId[]>("marimo:commands",[]);return {recentCommands: commands,- addRecentCommand: (command: HotkeyAction) => {+ addRecentCommand: (command: RecentCommandId) => {const uniqueCommands = unique([command, ...commands]);setCommands(uniqueCommands.slice(0, MAX_RECENT_COMMANDS));},diff --git a/frontend/src/utils/objects.ts b/frontend/src/utils/objects.tsindex 164cf1991d..6278a3a5bf 100644--- a/frontend/src/utils/objects.ts+++ b/frontend/src/utils/objects.ts@@ -26,6 +26,23 @@ export const Objects = {keys<K extends string | number>(obj: Record<K, unknown>): K[] {return Object.keys(obj) as K[];},+ /**+ * Type-safe keyBy+ */+ keyBy<T, K extends string | number>(+ items: T[],+ toKey: (item: T) => K | undefined+ ): Record<K, T> {+ const result: Record<K, T> = {} as Record<K, T>;+ for (const item of items) {+ const key = toKey(item);+ if (key === undefined) {+ continue;+ }+ result[key] = item;+ }+ return result;+ },/*** Type-safe groupBy*/