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
minimal create cell buttons (#7212)
Shahmir Varqha942938851ae5
diff --git a/frontend/e2e-tests/py/bad_button.py b/frontend/e2e-tests/py/bad_button.pyindex bcf1d3d171..3ea54faf54 100644--- a/frontend/e2e-tests/py/bad_button.py+++ b/frontend/e2e-tests/py/bad_button.py@@ -2,27 +2,28 @@# [tool.marimo.runtime]# auto_instantiate = true# ///+import marimo-__generated_with = "0.0.1"+__generated_with = "0.17.6"app = marimo.App()@app.cell-def __():+def _():import marimo as mo- return mo,+ return (mo,)@app.cell-def __(mo):- b = mo.ui.button(value=None, label='Bad button', on_click=lambda v: v + 1)+def _(mo):+ b = mo.ui.button(value=None, label="Bad button", on_click=lambda v: v + 1)b- return b,+ return (b,)@app.cell-def __(b):+def _(b):b.valuereturndiff --git a/frontend/src/components/editor/cell/CreateCellButton.tsx b/frontend/src/components/editor/cell/CreateCellButton.tsxindex 198d0dbd49..b6a0102c7c 100644--- a/frontend/src/components/editor/cell/CreateCellButton.tsx+++ b/frontend/src/components/editor/cell/CreateCellButton.tsx@@ -2,11 +2,11 @@import { DatabaseIcon, DiamondPlusIcon, PlusIcon } from "lucide-react";import { Button } from "@/components/editor/inputs/Inputs";import {- ContextMenu,- ContextMenuContent,- ContextMenuItem,- ContextMenuTrigger,-} from "@/components/ui/context-menu";+ DropdownMenu,+ DropdownMenuContent,+ DropdownMenuItem,+ DropdownMenuTrigger,+} from "@/components/ui/dropdown-menu";import { maybeAddMarimoImport } from "@/core/cells/add-missing-import";import { useCellActions } from "@/core/cells/cells";import { LanguageAdapters } from "@/core/codemirror/language/LanguageAdapters";@@ -29,115 +29,85 @@ export const CreateCellButton = ({tooltipContent: React.ReactNode;onClick: ((opts: { code: string; hideCode?: boolean }) => void) | undefined;}) => {+ const { createNewCell, addSetupCellIfDoesntExist } = useCellActions();+const baseTooltipContent =getConnectionTooltip(connectionState) || tooltipContent;const finalTooltipContent = isAppInteractionDisabled(connectionState) ? (baseTooltipContent) : (- <div className="flex flex-col gap-4">- <div>{baseTooltipContent}</div>- <div className="text-xs text-muted-foreground font-medium pt-1 -mt-2 border-t border-border">- Right-click for cell types- </div>- </div>+ <div>{baseTooltipContent}</div>);+ const addPythonCell = () => {+ onClick?.({ code: "" });+ };++ // NB: When adding the marimo import for markdown and SQL, we run it+ // automatically regardless of whether autoinstantiate or lazy execution is+ // enabled; the user experience is confusing otherwise (how does the user+ // know they need to run import marimo as mo. first?).+ const addMarkdownCell = () => {+ maybeAddMarimoImport({ autoInstantiate: true, createNewCell });+ onClick?.({+ code: LanguageAdapters.markdown.defaultCode,+ hideCode: true,+ });+ };++ const addSQLCell = () => {+ maybeAddMarimoImport({ autoInstantiate: true, createNewCell });+ onClick?.({ code: LanguageAdapters.sql.defaultCode });+ };++ const addSetupCell = () => {+ addSetupCellIfDoesntExist({});+ };++ const renderIcon = (icon: React.ReactNode) => {+ return <div className="mr-3 text-muted-foreground">{icon}</div>;+ };+return (- <CreateCellButtonContextMenu onClick={onClick}>- <Tooltip content={finalTooltipContent}>+ <DropdownMenu>+ <DropdownMenuTrigger asChild={true}><Button- onClick={() => onClick?.({ code: "" })}className={cn(- "shoulder-button hover-action",+ "shoulder-button hover-action border-none shadow-none! bg-transparent! focus-visible:outline-none",isAppInteractionDisabled(connectionState) && " inactive-button",)}onMouseDown={Events.preventFocus}- shape="circle"size="small"color="hint-green"data-testid="create-cell-button">- <PlusIcon strokeWidth={1.8} />+ <Tooltip content={finalTooltipContent}>+ <PlusIcon+ strokeWidth={3.2}+ size={16}+ className="opacity-60 hover:opacity-90"+ />+ </Tooltip></Button>- </Tooltip>- </CreateCellButtonContextMenu>- );-};--const CreateCellButtonContextMenu = (props: {- onClick: ((opts: { code: string; hideCode?: boolean }) => void) | undefined;- children: React.ReactNode;-}) => {- const { children, onClick } = props;- const { createNewCell, addSetupCellIfDoesntExist } = useCellActions();-- if (!onClick) {- return children;- }-- // NB: When adding the marimo import for markdown and SQL, we run it- // automatically regardless of whether autoinstantiate or lazy execution is- // enabled; the user experience is confusing otherwise (how does the user- // know they need to run import marimo as mo. first?).- return (- <ContextMenu>- <ContextMenuTrigger>{children}</ContextMenuTrigger>- <ContextMenuContent>- <ContextMenuItem- key="python"- onSelect={(evt) => {- evt.stopPropagation();- onClick({ code: "" });- }}- >- <div className="mr-3 text-muted-foreground">- <PythonIcon />- </div>+ </DropdownMenuTrigger>+ <DropdownMenuContent>+ <DropdownMenuItem onClick={addPythonCell}>+ {renderIcon(<PythonIcon />)}Python cell- </ContextMenuItem>-- <ContextMenuItem- key="markdown"- onSelect={(evt) => {- evt.stopPropagation();- maybeAddMarimoImport({ autoInstantiate: true, createNewCell });- onClick({- code: LanguageAdapters.markdown.defaultCode,- hideCode: true,- });- }}- >- <div className="mr-3 text-muted-foreground">- <MarkdownIcon />- </div>+ </DropdownMenuItem>+ <DropdownMenuItem onClick={addMarkdownCell}>+ {renderIcon(<MarkdownIcon />)}Markdown cell- </ContextMenuItem>- <ContextMenuItem- key="sql"- onSelect={(evt) => {- evt.stopPropagation();- maybeAddMarimoImport({ autoInstantiate: true, createNewCell });- onClick({ code: LanguageAdapters.sql.defaultCode });- }}- >- <div className="mr-3 text-muted-foreground">- <DatabaseIcon size={13} strokeWidth={1.5} />- </div>+ </DropdownMenuItem>+ <DropdownMenuItem onClick={addSQLCell}>+ {renderIcon(<DatabaseIcon size={13} strokeWidth={1.5} />)}SQL cell- </ContextMenuItem>- <ContextMenuItem- key="setup"- onSelect={(evt) => {- evt.stopPropagation();- addSetupCellIfDoesntExist({});- }}- >- <div className="mr-3 text-muted-foreground">- <DiamondPlusIcon size={13} strokeWidth={1.5} />- </div>+ </DropdownMenuItem>+ <DropdownMenuItem onClick={addSetupCell}>+ {renderIcon(<DiamondPlusIcon size={13} strokeWidth={1.5} />)}Setup cell- </ContextMenuItem>- </ContextMenuContent>- </ContextMenu>+ </DropdownMenuItem>+ </DropdownMenuContent>+ </DropdownMenu>);};diff --git a/frontend/src/components/editor/notebook-cell.tsx b/frontend/src/components/editor/notebook-cell.tsxindex 290479f737..4bbdbd7e97 100644--- a/frontend/src/components/editor/notebook-cell.tsx+++ b/frontend/src/components/editor/notebook-cell.tsx@@ -489,7 +489,7 @@ const EditableCellComponent = ({const outputArea = hasOutput && (<div className="relative" onDoubleClick={showHiddenCodeIfMarkdown}>- <div className="absolute top-5 -left-8 z-10 print:hidden">+ <div className="absolute top-5 -left-8 z-20 print:hidden"><CollapseToggleisCollapsed={isCollapsed}onClick={() => {@@ -580,6 +580,7 @@ const EditableCellComponent = ({ref={cellContainerRef}{...cellDomProps(cellId, cellData.name)}>+ <CellLeftSideActions cellId={cellId} actions={actions} />{cellOutput === "above" && outputArea}<div className={cn("tray")} data-hidden={isMarkdownCodeHidden}><StagedAICellBackground cellId={cellId} />@@ -598,17 +599,6 @@ const EditableCellComponent = ({onRun={runCell}/></div>- <CellLeftSideActions- cellId={cellId}- className={cn(- isMarkdownCodeHidden && hasOutputAbove && "-top-7",- isMarkdownCodeHidden && hasOutputBelow && "-bottom-8",- isMarkdownCodeHidden &&- isCellButtonsInline &&- "-left-[3.8rem]",- )}- actions={actions}- /><CellEditortheme={theme}showPlaceholder={showPlaceholder}@@ -840,21 +830,26 @@ const CellLeftSideActions = memo(return (<divclassName={cn(- "absolute flex flex-col gap-[2px] justify-center h-full left-[-34px] z-20",+ "absolute flex flex-col justify-center h-full left-[-26px] z-20 border-b-0!",className,)}>- <CreateCellButton- tooltipContent={renderShortcut("cell.createAbove")}- connectionState={connection.state}- onClick={isConnected ? createAbove : undefined}- />- <div className="flex-1" />- <CreateCellButton- tooltipContent={renderShortcut("cell.createBelow")}- connectionState={connection.state}- onClick={isConnected ? createBelow : undefined}- />+ <div className="-mt-1">+ <CreateCellButton+ tooltipContent={renderShortcut("cell.createAbove")}+ connectionState={connection.state}+ onClick={isConnected ? createAbove : undefined}+ />+ </div>+ <div className="flex-1 pointer-events-none w-3" />+ {/* <div className="flex-1 pointer-events-none bg-border w-px mx-auto hover-action opacity-70" /> */}+ <div className="-mb-2">+ <CreateCellButton+ tooltipContent={renderShortcut("cell.createBelow")}+ connectionState={connection.state}+ onClick={isConnected ? createBelow : undefined}+ />+ </div></div>);},