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.

improve: scratchpad hint on multiple definition error (#4647)

Akshay Agrawal9c9ff4940840
diff --git a/frontend/src/components/datasources/__tests__/install-package-button.test.tsx b/frontend/src/components/datasources/__tests__/install-package-button.test.tsx
index e4c1c0c26c..5402c46999 100644
--- a/frontend/src/components/datasources/__tests__/install-package-button.test.tsx
+++ b/frontend/src/components/datasources/__tests__/install-package-button.test.tsx
@@ -3,10 +3,10 @@ import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, fireEvent } from "@testing-library/react";
import { InstallPackageButton } from "../install-package-button";
-const mockOpenApplication = vi.fn();
+const mockToggleApplication = vi.fn();
vi.mock("@/components/editor/chrome/state", () => ({
useChromeActions: () => ({
- openApplication: mockOpenApplication,
+ toggleApplication: mockToggleApplication,
}),
}));
@@ -62,6 +62,6 @@ describe("InstallPackageButton", () => {
expect(mockSetPackagesToInstall).toHaveBeenCalledWith("altair");
- expect(mockOpenApplication).toHaveBeenCalledWith("packages");
+ expect(mockToggleApplication).toHaveBeenCalledWith("packages");
});
});
diff --git a/frontend/src/components/datasources/install-package-button.tsx b/frontend/src/components/datasources/install-package-button.tsx
index 570082f143..2d59432038 100644
--- a/frontend/src/components/datasources/install-package-button.tsx
+++ b/frontend/src/components/datasources/install-package-button.tsx
@@ -30,7 +30,7 @@ export const InstallPackageButton: React.FC<InstallPackageButtonProps> = ({
setPackagesToInstall(packagesString);
// Open the packages panel
- chromeActions.openApplication("packages");
+ chromeActions.toggleApplication("packages");
};
return (
diff --git a/frontend/src/components/editor/actions/useNotebookActions.tsx b/frontend/src/components/editor/actions/useNotebookActions.tsx
index e71ee0d96a..437ffaf95f 100644
--- a/frontend/src/components/editor/actions/useNotebookActions.tsx
+++ b/frontend/src/components/editor/actions/useNotebookActions.tsx
@@ -94,7 +94,7 @@ const NOOP_HANDLER = (event?: Event) => {
export function useNotebookActions() {
const filename = useFilename();
const { openModal, closeModal } = useImperativeModal();
- const { openApplication } = useChromeActions();
+ const { toggleApplication } = useChromeActions();
const { selectedPanel } = useChromeState();
const [viewState] = useAtom(viewStateAtom);
const kioskMode = useAtomValue(kioskModeAtom);
@@ -272,7 +272,7 @@ export function useNotebookActions() {
label: startCase(type),
rightElement: renderCheckboxElement(selectedPanel === type),
icon: <Icon size={14} strokeWidth={1.5} />,
- handle: () => openApplication(type),
+ handle: () => toggleApplication(type),
};
}),
},
diff --git a/frontend/src/components/editor/chrome/state.ts b/frontend/src/components/editor/chrome/state.ts
index 6c33cc80ee..a6f9569ebe 100644
--- a/frontend/src/components/editor/chrome/state.ts
+++ b/frontend/src/components/editor/chrome/state.ts
@@ -41,6 +41,11 @@ const {
() => storage.get(),
{
openApplication: (state, selectedPanel: PanelType) => ({
+ ...state,
+ selectedPanel,
+ isSidebarOpen: true,
+ }),
+ toggleApplication: (state, selectedPanel: PanelType) => ({
...state,
selectedPanel,
// If it was closed, open it
diff --git a/frontend/src/components/editor/chrome/wrapper/footer.tsx b/frontend/src/components/editor/chrome/wrapper/footer.tsx
index eb9889e95c..eed46d62d3 100644
--- a/frontend/src/components/editor/chrome/wrapper/footer.tsx
+++ b/frontend/src/components/editor/chrome/wrapper/footer.tsx
@@ -32,7 +32,7 @@ import { isWasm } from "@/core/wasm/utils";
export const Footer: React.FC = () => {
const { selectedPanel, isTerminalOpen } = useChromeState();
- const { openApplication, toggleTerminal } = useChromeActions();
+ const { toggleApplication, toggleTerminal } = useChromeActions();
const [config, setConfig] = useResolvedMarimoConfig();
const errorCount = useAtomValue(cellErrorCount);
@@ -52,7 +52,7 @@ export const Footer: React.FC = () => {
<FooterItem
tooltip={errorPanel.tooltip}
selected={selectedPanel === errorPanel.type}
- onClick={() => openApplication(errorPanel.type)}
+ onClick={() => toggleApplication(errorPanel.type)}
>
{renderIcon(errorPanel, errorCount > 0 ? "text-destructive" : "")}
<span className="ml-1 font-mono mt-[0.125rem]">{errorCount}</span>
diff --git a/frontend/src/components/editor/chrome/wrapper/sidebar.tsx b/frontend/src/components/editor/chrome/wrapper/sidebar.tsx
index f54f897613..c6076144d4 100644
--- a/frontend/src/components/editor/chrome/wrapper/sidebar.tsx
+++ b/frontend/src/components/editor/chrome/wrapper/sidebar.tsx
@@ -12,7 +12,7 @@ import { useAtomValue } from "jotai";
export const Sidebar: React.FC = () => {
const { selectedPanel } = useChromeState();
- const { openApplication } = useChromeActions();
+ const { toggleApplication } = useChromeActions();
const renderIcon = ({ Icon }: PanelDescriptor, className?: string) => {
return <Icon className={cn("h-5 w-5", className)} />;
@@ -29,7 +29,7 @@ export const Sidebar: React.FC = () => {
key={p.type}
tooltip={p.tooltip}
selected={selectedPanel === p.type}
- onClick={() => openApplication(p.type)}
+ onClick={() => toggleApplication(p.type)}
>
{renderIcon(p)}
</SidebarItem>
diff --git a/frontend/src/components/editor/output/MarimoErrorOutput.tsx b/frontend/src/components/editor/output/MarimoErrorOutput.tsx
index d16e5ad0a9..61cc46a2eb 100644
--- a/frontend/src/components/editor/output/MarimoErrorOutput.tsx
+++ b/frontend/src/components/editor/output/MarimoErrorOutput.tsx
@@ -11,12 +11,14 @@ import {
AccordionItem,
AccordionTrigger,
} from "@/components/ui/accordion";
+import { Button } from "@/components/ui/button";
import { Fragment } from "react";
import { CellLinkError } from "../links/cell-link";
import type { CellId } from "@/core/cells/ids";
import { AutoFixButton } from "../errors/auto-fix";
-import { SquareArrowOutUpRightIcon } from "lucide-react";
+import { NotebookPenIcon, SquareArrowOutUpRightIcon } from "lucide-react";
import { ExternalLink } from "@/components/ui/links";
+import { useChromeActions } from "../chrome/state";
const Tip = (props: {
title?: string;
@@ -51,6 +53,8 @@ export const MarimoErrorOutput = ({
cellId,
className,
}: Props): JSX.Element => {
+ const chromeActions = useChromeActions();
+
let titleContents = "This cell wasn't run because it has errors";
let alertVariant: "destructive" | "default" = "destructive";
let titleColor = "text-error";
@@ -129,6 +133,10 @@ export const MarimoErrorOutput = ({
(e): e is Extract<MarimoError, { type: "unknown" }> => e.type === "unknown",
);
+ const openScratchpad = () => {
+ chromeActions.openApplication("scratchpad");
+ };
+
const renderMessages = () => {
const messages: JSX.Element[] = [];
@@ -270,9 +278,11 @@ export const MarimoErrorOutput = ({
</ul>
</Fragment>
))}
+
{cellId && (
<AutoFixButton errors={multipleDefsErrors} cellId={cellId} />
)}
+
<Tip title="Why can't I redefine variables?">
<p className="pb-2">
marimo requires that each variable is defined in just one cell.
@@ -295,6 +305,21 @@ export const MarimoErrorOutput = ({
.
</p>
</Tip>
+
+ <Tip title="Need a scratchpad?">
+ <div className="flex flex-row gap-2 items-center">
+ <Button
+ size="xs"
+ variant="link"
+ className="my-2 font-normal mx-0 px-0"
+ onClick={openScratchpad}
+ >
+ <NotebookPenIcon className="h-3" />
+ <span>Try the scratchpad</span>
+ </Button>
+ <span>to experiment without restrictions on variable names.</span>
+ </div>
+ </Tip>
</li>,
);
}
diff --git a/frontend/src/components/scratchpad/scratchpad.tsx b/frontend/src/components/scratchpad/scratchpad.tsx
index 472ac29d8c..5a82ed9eb6 100644
--- a/frontend/src/components/scratchpad/scratchpad.tsx
+++ b/frontend/src/components/scratchpad/scratchpad.tsx
@@ -209,6 +209,12 @@ export const ScratchPad: React.FC = () => {
className="flex flex-col h-full overflow-hidden divide-y"
id={HTMLCellId.create(cellId)}
>
+ <p className="mx-2 my-2 text-muted-foreground text-sm">
+ Use this scratchpad cell to experiment with code without restrictions on
+ variable names. Scratchpad code is ephemeral: variables defined in the
+ scratchpad aren't saved to notebook memory, and the code is not saved in
+ the notebook file.
+ </p>
<div className="flex items-center flex-shrink-0">
<Tooltip content={renderShortcut("cell.run")}>
<Button