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.

fix: edge-case for not deleting the last cell (#1239)

Myles Scolnick84da747004b6
diff --git a/frontend/src/components/editor/actions/useCellActionButton.tsx b/frontend/src/components/editor/actions/useCellActionButton.tsx
index ba051537aa..b467a1067a 100644
--- a/frontend/src/components/editor/actions/useCellActionButton.tsx
+++ b/frontend/src/components/editor/actions/useCellActionButton.tsx
@@ -7,7 +7,7 @@ import {
getEditorViewMode,
toggleMarkdown,
} from "@/core/codemirror/format";
-import { useCellActions } from "@/core/cells/cells";
+import { hasOnlyOneCellAtom, useCellActions } from "@/core/cells/cells";
import {
ImageIcon,
Code2Icon,
@@ -69,6 +69,8 @@ export function useCellActionButtons({ cell }: Props) {
sendToBottom,
} = useCellActions();
const runCell = useRunCell(cell?.cellId);
+ const hasOnlyOneCell = useAtomValue(hasOnlyOneCellAtom);
+ const canDelete = !hasOnlyOneCell;
const deleteCell = useDeleteCellCallback();
const { openModal } = useImperativeModal();
const setAiCompletionCell = useSetAtom(aiCompletionCellAtom);
@@ -290,6 +292,7 @@ export function useCellActionButtons({ cell }: Props) {
[
{
label: "Delete",
+ hidden: !canDelete,
variant: "danger",
icon: <Trash2Icon size={13} strokeWidth={1.5} />,
handle: () => {
diff --git a/frontend/src/components/editor/cell/useDeleteCell.tsx b/frontend/src/components/editor/cell/useDeleteCell.tsx
index 46baf408ab..550475ce0d 100644
--- a/frontend/src/components/editor/cell/useDeleteCell.tsx
+++ b/frontend/src/components/editor/cell/useDeleteCell.tsx
@@ -1,7 +1,7 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { Button } from "@/components/ui/button";
import { toast } from "@/components/ui/use-toast";
-import { useCellActions } from "@/core/cells/cells";
+import { getCells, useCellActions } from "@/core/cells/cells";
import { CellId } from "@/core/cells/ids";
import { sendDeleteCell } from "@/core/network/requests";
import useEvent from "react-use-event-hook";
@@ -10,6 +10,12 @@ export function useDeleteCellCallback() {
const { deleteCell, undoDeleteCell } = useCellActions();
return useEvent((opts: { cellId: CellId }) => {
+ const cells = getCells();
+ // Can't delete the last cell
+ if (cells.length === 1) {
+ return;
+ }
+
const { cellId } = opts;
// Optimistic update
deleteCell({ cellId });
diff --git a/frontend/src/core/cells/cells.ts b/frontend/src/core/cells/cells.ts
index 540aacbfc2..7dd3248a9f 100644
--- a/frontend/src/core/cells/cells.ts
+++ b/frontend/src/core/cells/cells.ts
@@ -654,6 +654,8 @@ export {
const cellIdsAtom = atom((get) => get(notebookAtom).cellIds);
+export const hasOnlyOneCellAtom = atom((get) => get(cellIdsAtom).length === 1);
+
const cellErrorsAtom = atom((get) => {
const { cellIds, cellRuntime, cellData } = get(notebookAtom);
const errors = cellIds