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: clear previous stop's output when running again (#174)

Myles Scolnick08bf159ad7a9
diff --git a/frontend/src/core/state/__tests__/__snapshots__/cells.test.ts.snap b/frontend/src/core/state/__tests__/__snapshots__/cells.test.ts.snap
index 999b32ce7e..09d19db479 100644
--- a/frontend/src/core/state/__tests__/__snapshots__/cells.test.ts.snap
+++ b/frontend/src/core/state/__tests__/__snapshots__/cells.test.ts.snap
@@ -1,6 +1,6 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
-exports[`cell reducer > can run cell a stale cell 1`] = `
+exports[`cell reducer > can run a stale cell 1`] = `
{
"code": "mo.slider()",
"config": {},
@@ -23,7 +23,7 @@ exports[`cell reducer > can run cell a stale cell 1`] = `
}
`;
-exports[`cell reducer > can run cell a stale cell 2`] = `
+exports[`cell reducer > can run a stale cell 2`] = `
{
"code": "mo.slider()",
"config": {},
@@ -46,6 +46,143 @@ exports[`cell reducer > can run cell a stale cell 2`] = `
}
`;
+exports[`cell reducer > can run a stopped cell 1`] = `
+{
+ "code": "mo.md('This has an ancestor that was stopped')",
+ "config": {},
+ "consoleOutputs": [],
+ "edited": false,
+ "errored": false,
+ "interrupted": false,
+ "key": "0",
+ "lastCodeRun": "mo.md('This has an ancestor that was stopped')",
+ "name": "__",
+ "output": null,
+ "ref": {
+ "current": null,
+ },
+ "runElapsedTimeMs": null,
+ "runStartTimestamp": null,
+ "serializedEditorState": null,
+ "status": "queued",
+ "stopped": false,
+}
+`;
+
+exports[`cell reducer > can run a stopped cell 2`] = `
+{
+ "code": "mo.md('This has an ancestor that was stopped')",
+ "config": {},
+ "consoleOutputs": [],
+ "edited": false,
+ "errored": false,
+ "interrupted": false,
+ "key": "0",
+ "lastCodeRun": "mo.md('This has an ancestor that was stopped')",
+ "name": "__",
+ "output": null,
+ "ref": {
+ "current": null,
+ },
+ "runElapsedTimeMs": null,
+ "runStartTimestamp": null,
+ "serializedEditorState": null,
+ "status": "idle",
+ "stopped": false,
+}
+`;
+
+exports[`cell reducer > can run a stopped cell 3`] = `
+{
+ "code": "mo.md('This has an ancestor that was stopped')",
+ "config": {},
+ "consoleOutputs": [],
+ "edited": false,
+ "errored": false,
+ "interrupted": false,
+ "key": "0",
+ "lastCodeRun": "mo.md('This has an ancestor that was stopped')",
+ "name": "__",
+ "output": {
+ "channel": "marimo-error",
+ "data": [
+ {
+ "msg": "This cell wasn't run because an ancestor was stopped with \`mo.stop\`: ",
+ "raising_cell": "2",
+ "type": "ancestor-stopped",
+ },
+ ],
+ "mimetype": "application/vnd.marimo+error",
+ "timestamp": 20,
+ },
+ "ref": {
+ "current": null,
+ },
+ "runElapsedTimeMs": null,
+ "runStartTimestamp": null,
+ "serializedEditorState": null,
+ "status": "idle",
+ "stopped": true,
+}
+`;
+
+exports[`cell reducer > can run a stopped cell 4`] = `
+{
+ "code": "mo.md('This has an ancestor that was stopped')",
+ "config": {},
+ "consoleOutputs": [],
+ "edited": false,
+ "errored": false,
+ "interrupted": false,
+ "key": "0",
+ "lastCodeRun": "mo.md('This has an ancestor that was stopped')",
+ "name": "__",
+ "output": {
+ "channel": "marimo-error",
+ "data": [
+ {
+ "msg": "This cell wasn't run because an ancestor was stopped with \`mo.stop\`: ",
+ "raising_cell": "2",
+ "type": "ancestor-stopped",
+ },
+ ],
+ "mimetype": "application/vnd.marimo+error",
+ "timestamp": 20,
+ },
+ "ref": {
+ "current": null,
+ },
+ "runElapsedTimeMs": null,
+ "runStartTimestamp": null,
+ "serializedEditorState": null,
+ "status": "queued",
+ "stopped": true,
+}
+`;
+
+exports[`cell reducer > can run a stopped cell 5`] = `
+{
+ "code": "mo.md('This has an ancestor that was stopped')",
+ "config": {},
+ "consoleOutputs": [],
+ "edited": false,
+ "errored": false,
+ "interrupted": false,
+ "key": "0",
+ "lastCodeRun": "mo.md('This has an ancestor that was stopped')",
+ "name": "__",
+ "output": null,
+ "ref": {
+ "current": null,
+ },
+ "runElapsedTimeMs": null,
+ "runStartTimestamp": 40,
+ "serializedEditorState": null,
+ "status": "running",
+ "stopped": false,
+}
+`;
+
exports[`cell reducer > can run cell and receive cell messages 1`] = `
{
"code": "",
diff --git a/frontend/src/core/state/__tests__/cells.test.ts b/frontend/src/core/state/__tests__/cells.test.ts
index 8a30b5226d..561f44c9b6 100644
--- a/frontend/src/core/state/__tests__/cells.test.ts
+++ b/frontend/src/core/state/__tests__/cells.test.ts
@@ -472,7 +472,7 @@ describe("cell reducer", () => {
expect(cell).toMatchSnapshot(); // snapshot everything as a catch all
});
- it("can run cell a stale cell", () => {
+ it("can run a stale cell", () => {
// Update code of first cell
actions.updateCellCode({
cellId: firstCellId,
@@ -612,4 +612,117 @@ describe("cell reducer", () => {
cell = state.present[0];
expect(cell.config.disabled).toBe(false);
});
+
+ it("can run a stopped cell", () => {
+ // Update code of first cell
+ actions.updateCellCode({
+ cellId: firstCellId,
+ code: "mo.md('This has an ancestor that was stopped')",
+ formattingChange: false,
+ });
+
+ // Prepare for run
+ actions.prepareForRun({
+ cellId: firstCellId,
+ });
+
+ // Receive queued messages
+ actions.handleCellMessage({
+ cellId: firstCellId,
+ message: {
+ cell_id: firstCellId,
+ output: null,
+ console: null,
+ status: "queued",
+ timestamp: new Date(10).getTime(),
+ },
+ });
+ let cell = state.present[0];
+ expect(cell.status).toBe("queued");
+ expect(cell.lastCodeRun).toBe(
+ "mo.md('This has an ancestor that was stopped')"
+ );
+ expect(cell.edited).toBe(false);
+ expect(cell.runElapsedTimeMs).toBe(null);
+ expect(cell.runStartTimestamp).toBe(null);
+ expect(cell).toMatchSnapshot(); // snapshot everything as a catch all
+
+ // Receive idle message
+ actions.handleCellMessage({
+ cellId: firstCellId,
+ message: {
+ cell_id: firstCellId,
+ output: null,
+ console: null,
+ status: "idle",
+ timestamp: new Date(20).getTime(),
+ },
+ });
+ cell = state.present[0];
+ expect(cell).toMatchSnapshot(); // snapshot everything as a catch all
+
+ // Receive stop output
+ actions.handleCellMessage({
+ cellId: firstCellId,
+ message: {
+ cell_id: firstCellId,
+ output: {
+ channel: "marimo-error",
+ mimetype: "application/vnd.marimo+error",
+ data: [
+ {
+ msg: "This cell wasn't run because an ancestor was stopped with `mo.stop`: ",
+ raising_cell: "2" as CellId,
+ type: "ancestor-stopped",
+ },
+ ],
+ timestamp: new Date(20).getTime(),
+ },
+ console: null,
+ status: "idle",
+ timestamp: new Date(20).getTime(),
+ },
+ });
+ cell = state.present[0];
+ expect(cell.status).toBe("idle");
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ expect((cell.output?.data as any)[0].msg).toBe(
+ "This cell wasn't run because an ancestor was stopped with `mo.stop`: "
+ );
+ expect(cell.stopped).toBe(true);
+ expect(cell).toMatchSnapshot(); // snapshot everything as a catch all
+
+ // Receive queued message
+ actions.handleCellMessage({
+ cellId: firstCellId,
+ message: {
+ cell_id: firstCellId,
+ output: null,
+ console: null,
+ status: "queued",
+ timestamp: new Date(30).getTime(),
+ },
+ });
+ cell = state.present[0];
+ expect(cell.status).toBe("queued");
+ expect(cell.stopped).toBe(true);
+ expect(cell).toMatchSnapshot(); // snapshot everything as a catch all
+
+ // Receive running message
+ actions.handleCellMessage({
+ cellId: firstCellId,
+ message: {
+ cell_id: firstCellId,
+ output: null,
+ console: null,
+ status: "running",
+ timestamp: new Date(40).getTime(),
+ },
+ });
+ cell = state.present[0];
+ expect(cell.status).toBe("running");
+ expect(cell.stopped).toBe(false);
+ expect(cell.output).toBe(null);
+ expect(cell).toMatchSnapshot(); // snapshot everything as a catch all
+ });
});
diff --git a/frontend/src/core/state/cell.ts b/frontend/src/core/state/cell.ts
index ba16b0ac97..4221dcef5c 100644
--- a/frontend/src/core/state/cell.ts
+++ b/frontend/src/core/state/cell.ts
@@ -14,7 +14,6 @@ export function transitionCell(
// implies a status transition
switch (message.status) {
case "queued":
- nextCell.stopped = false;
nextCell.interrupted = false;
nextCell.errored = false;
// We intentionally don't update lastCodeRun, since the kernel queues
@@ -22,6 +21,11 @@ export function transitionCell(
// the cell's current code if the user modified it.
break;
case "running":
+ // If was previously stopped, clear the outputs
+ if (cell.stopped) {
+ nextCell.output = null;
+ }
+ nextCell.stopped = false;
nextCell.runStartTimestamp = message.timestamp;
break;
case "idle":
@@ -42,8 +46,8 @@ export function transitionCell(
default:
logNever(message.status);
}
- nextCell.output = message.output ?? cell.output;
- nextCell.status = message.status ?? cell.status;
+ nextCell.output = message.output ?? nextCell.output;
+ nextCell.status = message.status ?? nextCell.status;
// Handle errors: marimo includes an error output when a cell is interrupted
// or errored