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: add cell actions and input/output variabels to dag viewer panel (#1328)
Myles Scolnicka55c9eae5b1f
diff --git a/frontend/src/components/dependency-graph/panels.tsx b/frontend/src/components/dependency-graph/panels.tsxindex bbc38c2965..aad374b69e 100644--- a/frontend/src/components/dependency-graph/panels.tsx+++ b/frontend/src/components/dependency-graph/panels.tsx@@ -11,16 +11,17 @@ import {WorkflowIcon,SquareFunction,SettingsIcon,+ MoreVerticalIcon,} from "lucide-react";import { GraphLayoutView, GraphSelection, GraphSettings } from "./types";-import { CellId } from "@/core/cells/ids";import { CellLink } from "../editor/links/cell-link";import { CellLinkList } from "../editor/links/cell-link-list";import { VariableName } from "../variables/common";-import { Variables } from "@/core/variables/types";+import { Variable, Variables } from "@/core/variables/types";import { Popover, PopoverContent, PopoverTrigger } from "../ui/popover";import { Checkbox } from "../ui/checkbox";import { Label } from "../ui/label";+import { ConnectionCellActionsDropdown } from "../editor/cell/cell-actions";interface Props {view: GraphLayoutView;@@ -114,30 +115,67 @@ export const GraphSelectionPanel: React.FC<{const renderSelection = () => {if (selection.type === "node") {- const inputs = edges.flatMap((edge) =>- edge.target === selection.id ? [edge.source as CellId] : [],+ const variablesUsed = Object.values(variables).filter((variable) =>+ variable.usedBy.includes(selection.id),);- const outputs = edges.flatMap((edge) =>- edge.source === selection.id ? [edge.target as CellId] : [],+ const variablesDeclared = Object.values(variables).filter((variable) =>+ variable.declaredBy.includes(selection.id),);++ const renderVariables = (variables: Variable[]) => (+ <>+ {variables.length === 0 && (+ <div className="text-muted-foreground text-sm text-center">--</div>+ )}+ <div className="grid grid-cols-5 gap-3 items-center text-sm py-1 flex-1 empty:hidden">+ {variables.map((variable) => (+ <React.Fragment key={variable.name}>+ <VariableName+ declaredBy={variable.declaredBy}+ name={variable.name}+ />+ <div className="truncate col-span-2" title={variable.value}>+ {variable.value}+ <span className="ml-1 truncate text-foreground/60 font-mono">+ ({variable.dataType})+ </span>+ </div>+ <div className="truncate col-span-2 gap-1 items-center">+ <CellLinkList maxCount={3} cellIds={variable.usedBy} />+ </div>+ </React.Fragment>+ ))}+ </div>+ </>+ );+return (<><div className="font-bold py-2 flex items-center gap-2 border-b px-3"><SquareFunction className="w-5 h-5" /><CellLink cellId={selection.id} />+ <div className="flex-1" />+ <ConnectionCellActionsDropdown cellId={selection.id}>+ <Button variant="ghost" size="icon">+ <MoreVerticalIcon className="w-4 h-4" />+ </Button>+ </ConnectionCellActionsDropdown></div>- <div className="text-sm flex flex-col p-3 flex-1 justify-center">- <div className="flex items-center gap-2">- <span title="Inputs">- <ArrowRightToLineIcon className="w-4 h-4 mr-2" />+ <div className="text-sm flex flex-col py-3 pl-2 pr-4 flex-1 justify-center">+ <div className="flex flex-col gap-2">+ <span className="flex items-center gap-2 font-semibold">+ <ArrowRightFromLineIcon className="w-4 h-4" />+ Outputs</span>- <CellLinkList maxCount={3} cellIds={inputs} />+ {renderVariables(variablesDeclared)}</div>- <div className="flex items-center gap-2">- <span title="Outputs">- <ArrowRightFromLineIcon className="w-4 h-4 mr-2" />+ <hr className="border-divider my-3" />+ <div className="flex flex-col gap-2">+ <span className="flex items-center gap-2 font-semibold">+ <ArrowRightToLineIcon className="w-4 h-4" />+ Inputs</span>- <CellLinkList maxCount={3} cellIds={outputs} />+ {renderVariables(variablesUsed)}</div></div></>@@ -145,7 +183,7 @@ export const GraphSelectionPanel: React.FC<{}if (selection.type === "edge") {- const variableUsed = Object.values(variables).filter(+ const edgeVariables = Object.values(variables).filter((variable) =>variable.declaredBy.includes(selection.source) &&variable.usedBy.includes(selection.target),@@ -159,19 +197,16 @@ export const GraphSelectionPanel: React.FC<{<CellLink cellId={selection.target} /></div><div className="grid grid-cols-4 gap-3 max-w-[350px] items-center text-sm p-3 flex-1">- {variableUsed.map((variable) => (+ {edgeVariables.map((variable) => (<React.Fragment key={variable.name}><VariableNamedeclaredBy={variable.declaredBy}name={variable.name}/>- <div className="text-ellipsis overflow-hidden whitespace-nowrap text-foreground/60 font-mono">+ <div className="truncate text-foreground/60 font-mono">{variable.dataType}</div>- <div- className="text-ellipsis overflow-hidden whitespace-nowrap col-span-2"- title={variable.value}- >+ <div className="truncate col-span-2" title={variable.value}>{variable.value}</div></React.Fragment>@@ -184,7 +219,7 @@ export const GraphSelectionPanel: React.FC<{return (<Panel position="bottom-left">- <div className="min-h-[100px] shadow-md rounded-md border border-primary/40 my-4 min-w-[200px] bg-[var(--slate-1)] text-muted-foreground/80 flex flex-col">+ <div className="min-h-[100px] shadow-md rounded-md border max-w-[550px] border-primary/40 my-4 min-w-[240px] bg-[var(--slate-1)] text-muted-foreground/80 flex flex-col">{renderSelection()}</div></Panel>diff --git a/frontend/src/components/editor/cell/cell-actions.tsx b/frontend/src/components/editor/cell/cell-actions.tsxindex 2fe01c5b3c..4b9f77672a 100644--- a/frontend/src/components/editor/cell/cell-actions.tsx+++ b/frontend/src/components/editor/cell/cell-actions.tsx@@ -1,5 +1,11 @@/* Copyright 2024 Marimo. All rights reserved. */-import { Fragment, useImperativeHandle, useState } from "react";+import {+ Fragment,+ PropsWithChildren,+ useImperativeHandle,+ useMemo,+ useState,+} from "react";import {Command,@@ -26,9 +32,13 @@ import {useCellActionButtons,} from "../actions/useCellActionButton";import { useRestoreFocus } from "@/components/ui/use-restore-focus";+import { useAtomValue } from "jotai";+import { cellFocusDetailsAtom } from "@/core/cells/focus";+import { CellId } from "@/core/cells/ids";interface Props extends CellActionButtonProps {children: React.ReactNode;+ showTooltip?: boolean;}export interface CellActionsDropdownHandle {@@ -36,7 +46,7 @@ export interface CellActionsDropdownHandle {}const CellActionsDropdownInternal = (- { children, ...props }: Props,+ { children, showTooltip = true, ...props }: Props,ref: React.Ref<CellActionsDropdownHandle>,) => {const [open, setOpen] = useState(false);@@ -49,23 +59,76 @@ const CellActionsDropdownInternal = (toggle: () => setOpen((prev) => !prev),}));+ const content = (+ <PopoverContent className="w-[300px] p-0 pt-1" {...restoreFocus}>+ <Command>+ <CommandInput placeholder="Search actions..." className="h-6 m-1" />+ <CommandEmpty>No results</CommandEmpty>+ {actions.map((group, i) => (+ <Fragment key={i}>+ <CommandGroup key={i}>+ {group.map((action) => {+ return (+ <CommandItem+ key={action.label}+ onSelect={() => {+ if (action.disableClick) {+ return;+ }+ action.handle();+ setOpen(false);+ }}+ variant={action.variant}+ >+ <div className="flex items-center flex-1">+ {action.icon && (+ <div className="mr-2 w-5">{action.icon}</div>+ )}+ <div className="flex-1">{action.label}</div>+ <div className="flex-shrink-0 text-sm">+ {action.hotkey && renderMinimalShortcut(action.hotkey)}+ {action.rightElement}+ </div>+ </div>+ </CommandItem>+ );+ })}+ </CommandGroup>+ {i < actions.length - 1 && <CommandSeparator />}+ </Fragment>+ ))}+ </Command>+ </PopoverContent>+ );++ if (!showTooltip) {+ return (+ <Popover open={open} onOpenChange={setOpen}>+ <PopoverTrigger asChild={true}>{children}</PopoverTrigger>+ {content}+ </Popover>+ );+ }++ const tooltipContent = (+ <TooltipContent className="w-full bg-card" tabIndex={-1}>+ <div className="text-foreground-muted flex flex-col text-center">+ <span>+ <span className="text-foreground font-semibold">Drag </span>to move+ cell+ </span>+ <span>+ <span className="text-foreground font-semibold">Click </span>to open+ menu+ </span>+ </div>+ </TooltipContent>+ );+return (<Popover open={open} onOpenChange={setOpen}><TooltipRoot delayDuration={400} disableHoverableContent={true}>- {!open && (- <TooltipContent className="w-full bg-card" tabIndex={-1}>- <div className="text-foreground-muted flex flex-col text-center">- <span>- <span className="text-foreground font-semibold">Drag </span>to- move cell- </span>- <span>- <span className="text-foreground font-semibold">Click </span>to- open menu- </span>- </div>- </TooltipContent>- )}+ {!open && tooltipContent}{/* This creates a warning in React due to nested <button> elements.Adding asChild could fix this, but it also changes the styling (is hidden) of the button whenthe Popover is open. */}@@ -73,46 +136,7 @@ const CellActionsDropdownInternal = (<PopoverTrigger className="flex">{children}</PopoverTrigger></TooltipTrigger></TooltipRoot>- <PopoverContent className="w-[300px] p-0 pt-1" {...restoreFocus}>- <Command>- <CommandInput placeholder="Search actions..." className="h-6 m-1" />- <CommandEmpty>No results</CommandEmpty>- {actions.map((group, i) => (- <Fragment key={i}>- <CommandGroup key={i}>- {group.map((action) => {- return (- <CommandItem- key={action.label}- onSelect={() => {- if (action.disableClick) {- return;- }- action.handle();- setOpen(false);- }}- variant={action.variant}- >- <div className="flex items-center flex-1">- {action.icon && (- <div className="mr-2 w-5">{action.icon}</div>- )}- <div className="flex-1">{action.label}</div>- <div className="flex-shrink-0 text-sm">- {action.hotkey &&- renderMinimalShortcut(action.hotkey)}- {action.rightElement}- </div>- </div>- </CommandItem>- );- })}- </CommandGroup>- {i < actions.length - 1 && <CommandSeparator />}- </Fragment>- ))}- </Command>- </PopoverContent>+ {content}</Popover>);};@@ -120,3 +144,22 @@ const CellActionsDropdownInternal = (export const CellActionsDropdown = React.memo(React.forwardRef(CellActionsDropdownInternal),);++export const ConnectionCellActionsDropdown = React.memo(+ ({ children, cellId }: PropsWithChildren<{ cellId: CellId }>) => {+ const state = useAtomValue(+ useMemo(() => cellFocusDetailsAtom(cellId), [cellId]),+ );++ if (!state) {+ return null;+ }++ return (+ <CellActionsDropdown showTooltip={false} {...state}>+ {children}+ </CellActionsDropdown>+ );+ },+);+ConnectionCellActionsDropdown.displayName = "ConnectionCellActionsDropdown";diff --git a/frontend/src/components/editor/chrome/wrapper/footer.tsx b/frontend/src/components/editor/chrome/wrapper/footer.tsxindex e1a284794b..e00098526b 100644--- a/frontend/src/components/editor/chrome/wrapper/footer.tsx+++ b/frontend/src/components/editor/chrome/wrapper/footer.tsx@@ -102,7 +102,7 @@ const FooterItem: React.FC<>> = ({ children, tooltip, selected, className, ...rest }) => {return (- <Tooltip content={tooltip} side="top">+ <Tooltip content={tooltip} side="top" delayDuration={200}><divclassName={cn("h-full flex items-center p-2 text-sm mx-[1px] shadow-inset font-mono cursor-pointer rounded",diff --git a/frontend/src/components/editor/chrome/wrapper/machine-stats.tsx b/frontend/src/components/editor/chrome/wrapper/machine-stats.tsxindex 4c388eceb0..2ccb76b370 100644--- a/frontend/src/components/editor/chrome/wrapper/machine-stats.tsx+++ b/frontend/src/components/editor/chrome/wrapper/machine-stats.tsx@@ -49,6 +49,7 @@ const MemoryUsageBar: React.FC<{ memory: UsageResponse["memory"] }> = ({const roundedPercent = Math.round(percent);return (<Tooltip+ delayDuration={200}content={<span><b>Memory:</b> {asGB(used)} / {asGB(total)} GB ({roundedPercent}%)@@ -68,6 +69,7 @@ const CPUBar: React.FC<{ cpu: UsageResponse["cpu"] }> = ({ cpu }) => {const roundedPercent = Math.round(percent);return (<Tooltip+ delayDuration={200}content={<span><b>CPU:</b> {roundedPercent}%diff --git a/frontend/src/components/editor/links/cell-link-list.tsx b/frontend/src/components/editor/links/cell-link-list.tsxindex cef63e99e1..489b7ab14b 100644--- a/frontend/src/components/editor/links/cell-link-list.tsx+++ b/frontend/src/components/editor/links/cell-link-list.tsx@@ -32,7 +32,7 @@ export const CellLinkList: React.FC<Props> = ({return (<>{sortedCellIds.slice(0, maxCount).map((cellId, idx) => (- <span className="flex" key={cellId}>+ <span className="truncate" key={cellId}><CellLinkvariant="focus"key={cellId}@@ -46,9 +46,9 @@ export const CellLinkList: React.FC<Props> = ({{cellIds.length > maxCount && (<Popover><PopoverTrigger asChild={true}>- <div className="whitespace-nowrap text-muted-foreground text-xs hover:underline cursor-pointer">+ <span className="whitespace-nowrap text-muted-foreground text-xs hover:underline cursor-pointer">+{cellIds.length - maxCount} more- </div>+ </span></PopoverTrigger><PopoverContent className="w-auto"><div className="flex flex-col gap-1 py-1">diff --git a/frontend/src/components/ui/button.tsx b/frontend/src/components/ui/button.tsxindex 64ee0af736..0b53d5314d 100644--- a/frontend/src/components/ui/button.tsx+++ b/frontend/src/components/ui/button.tsx@@ -59,7 +59,8 @@ const buttonVariants = cva(),text: cn("opacity-80 hover:opacity-100", "active:opacity-100"),ghost: cn(- "hover:bg-accent hover:text-accent-foreground hover:shadow-smSolid border border-transparent",+ "border border-transparent",+ "hover:bg-accent hover:text-accent-foreground hover:shadow-xsSolid",activeCommon,"active:text-accent-foreground",),diff --git a/frontend/src/core/cells/focus.ts b/frontend/src/core/cells/focus.tsindex cda6dcf1c6..00aba79afe 100644--- a/frontend/src/core/cells/focus.ts+++ b/frontend/src/core/cells/focus.ts@@ -1,7 +1,7 @@/* Copyright 2024 Marimo. All rights reserved. */import { atom } from "jotai";import { CellId } from "./ids";-import { notebookAtom } from "./cells";+import { NotebookState, notebookAtom } from "./cells";import { EditorView } from "@codemirror/view";import { CellConfig, CellStatus } from "./types";@@ -19,14 +19,25 @@ export const lastFocusedCellAtom = atom<{hasOutput: boolean;} | null>((get) => {const cellId = get(lastFocusedCellIdAtom);- const { cellData, cellHandles, cellRuntime } = get(notebookAtom);if (!cellId) {return null;}++ return cellFocusDetails(cellId, get(notebookAtom));+});++export function cellFocusDetailsAtom(cellId: CellId) {+ return atom((get) => {+ return cellFocusDetails(cellId, get(notebookAtom));+ });+}++function cellFocusDetails(cellId: CellId, notebookState: NotebookState) {+ const { cellData, cellHandles, cellRuntime } = notebookState;const data = cellData[cellId];const runtime = cellRuntime[cellId];const handle = cellHandles[cellId].current;- if (!data || !runtime || !handle) {+ if (!data || !handle) {return null;}const getEditorView = () => handle.editorView;@@ -35,8 +46,8 @@ export const lastFocusedCellAtom = atom<{cellId,name: data.name,config: data.config,- status: runtime.status,+ status: runtime ? runtime.status : "idle",getEditorView: getEditorView,- hasOutput: runtime.output !== null,+ hasOutput: runtime?.output != null,};-});+}