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.

improvement: add action to Clear Cell Outputs / Clear All Outputs (#2955)

Myles Scolnick97d5d352acbb
diff --git a/frontend/src/components/editor/actions/useCellActionButton.tsx b/frontend/src/components/editor/actions/useCellActionButton.tsx
index 42169b01e3..ea564ad3cc 100644
--- a/frontend/src/components/editor/actions/useCellActionButton.tsx
+++ b/frontend/src/components/editor/actions/useCellActionButton.tsx
@@ -26,6 +26,7 @@ import {
SparklesIcon,
DatabaseIcon,
Columns2Icon,
+ XCircleIcon,
} from "lucide-react";
import type { ActionButton } from "./types";
import { MultiIcon } from "@/components/icons/multi-icon";
@@ -78,6 +79,7 @@ export function useCellActionButtons({ cell }: Props) {
sendToTop,
sendToBottom,
addColumnBreakpoint,
+ clearCellOutput,
} = useCellActions();
const runCell = useRunCell(cell?.cellId);
const hasOnlyOneCell = useAtomValue(hasOnlyOneCellAtom);
@@ -232,6 +234,14 @@ export function useCellActionButtons({ cell }: Props) {
),
handle: toggleDisabled,
},
+ {
+ icon: <XCircleIcon size={13} strokeWidth={1.5} />,
+ label: "Clear output",
+ hidden: !hasOutput,
+ handle: () => {
+ clearCellOutput({ cellId });
+ },
+ },
],
// View as
diff --git a/frontend/src/components/editor/actions/useNotebookActions.tsx b/frontend/src/components/editor/actions/useNotebookActions.tsx
index 27fdad9df0..51d150842f 100644
--- a/frontend/src/components/editor/actions/useNotebookActions.tsx
+++ b/frontend/src/components/editor/actions/useNotebookActions.tsx
@@ -27,6 +27,7 @@ import {
LayoutTemplateIcon,
Files,
SettingsIcon,
+ XCircleIcon,
} from "lucide-react";
import { commandPaletteAtom } from "../controls/command-palette";
import { useCellActions, useNotebook } from "@/core/cells/cells";
@@ -79,7 +80,8 @@ export function useNotebookActions() {
const kioskMode = useAtomValue(kioskModeAtom);
const notebook = useNotebook();
- const { updateCellConfig, undoDeleteCell } = useCellActions();
+ const { updateCellConfig, undoDeleteCell, clearAllCellOutputs } =
+ useCellActions();
const restartKernel = useRestartKernel();
const copyNotebook = useCopyNotebook(filename);
const setCommandPaletteOpen = useSetAtom(commandPaletteAtom);
@@ -342,6 +344,13 @@ export function useNotebookActions() {
}
},
},
+ {
+ icon: <XCircleIcon size={14} strokeWidth={1.5} />,
+ label: "Clear all outputs",
+ handle: () => {
+ clearAllCellOutputs();
+ },
+ },
{
icon: <Undo2Icon size={14} strokeWidth={1.5} />,
label: "Undo cell deletion",
diff --git a/frontend/src/core/cells/__tests__/cells.test.ts b/frontend/src/core/cells/__tests__/cells.test.ts
index af0980c1fc..37050bfafe 100644
--- a/frontend/src/core/cells/__tests__/cells.test.ts
+++ b/frontend/src/core/cells/__tests__/cells.test.ts
@@ -1626,4 +1626,88 @@ describe("cell reducer", () => {
expect(state.cellIds.getColumns()[1].topLevelIds).toEqual(["1"]);
expect(state.cellIds.getColumns()[2].topLevelIds).toEqual(["2"]);
});
+
+ it("can clear output of a single cell", () => {
+ // Set up initial state with output
+ actions.handleCellMessage({
+ cell_id: firstCellId,
+ output: {
+ channel: "output",
+ mimetype: "text/plain",
+ data: "test output",
+ timestamp: 0,
+ },
+ console: {
+ channel: "stdout",
+ mimetype: "text/plain",
+ data: "console output",
+ timestamp: 0,
+ },
+ status: "idle",
+ stale_inputs: null,
+ timestamp: new Date(33).getTime() as Seconds,
+ });
+
+ // Verify initial state has output
+ let cell = cells[0];
+ expect(cell.output).not.toBeNull();
+ expect(cell.consoleOutputs.length).toBe(1);
+
+ // Clear output
+ actions.clearCellOutput({ cellId: firstCellId });
+
+ // Verify output is cleared
+ cell = cells[0];
+ expect(cell.output).toBeNull();
+ expect(cell.consoleOutputs).toEqual([]);
+ });
+
+ it("can clear output of all cells", () => {
+ // Create multiple cells with output
+ actions.createNewCell({ cellId: firstCellId, before: false });
+ const secondCellId = state.cellIds.atOrThrow(FIRST_COLUMN).atOrThrow(1);
+
+ // Add output to both cells
+ const outputMessage = {
+ output: {
+ channel: "output",
+ mimetype: "text/plain",
+ data: "test output",
+ timestamp: 0,
+ },
+ console: {
+ channel: "stdout",
+ mimetype: "text/plain",
+ data: "console output",
+ timestamp: 0,
+ },
+ status: "idle",
+ stale_inputs: null,
+ timestamp: new Date(33).getTime() as Seconds,
+ } as const;
+
+ actions.handleCellMessage({
+ ...outputMessage,
+ cell_id: firstCellId,
+ });
+ actions.handleCellMessage({
+ ...outputMessage,
+ cell_id: secondCellId,
+ });
+
+ // Verify initial state has output
+ expect(state.cellRuntime[firstCellId].output).not.toBeNull();
+ expect(state.cellRuntime[firstCellId].consoleOutputs.length).toBe(1);
+ expect(state.cellRuntime[secondCellId].output).not.toBeNull();
+ expect(state.cellRuntime[secondCellId].consoleOutputs.length).toBe(1);
+
+ // Clear all outputs
+ actions.clearAllCellOutputs();
+
+ // Verify all outputs are cleared
+ expect(state.cellRuntime[firstCellId].output).toBeNull();
+ expect(state.cellRuntime[firstCellId].consoleOutputs).toEqual([]);
+ expect(state.cellRuntime[secondCellId].output).toBeNull();
+ expect(state.cellRuntime[secondCellId].consoleOutputs).toEqual([]);
+ });
});
diff --git a/frontend/src/core/cells/cells.ts b/frontend/src/core/cells/cells.ts
index 0aa4a2fc2e..9b01ecd7eb 100644
--- a/frontend/src/core/cells/cells.ts
+++ b/frontend/src/core/cells/cells.ts
@@ -1057,6 +1057,28 @@ const {
},
};
},
+ clearCellOutput: (state, action: { cellId: CellId }) => {
+ const { cellId } = action;
+ return updateCellRuntimeState(state, cellId, (cell) => ({
+ ...cell,
+ output: null,
+ consoleOutputs: [],
+ }));
+ },
+ clearAllCellOutputs: (state) => {
+ const newCellRuntime = { ...state.cellRuntime };
+ for (const cellId of state.cellIds.inOrderIds) {
+ newCellRuntime[cellId] = {
+ ...newCellRuntime[cellId],
+ output: null,
+ consoleOutputs: [],
+ };
+ }
+ return {
+ ...state,
+ cellRuntime: newCellRuntime,
+ };
+ },
});
// Helper function to update a cell in the array