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.

Add outputs e2e test (#165)

Myles Scolnicka74444ea4437
diff --git a/frontend/e2e-tests/output.spec.ts b/frontend/e2e-tests/output.spec.ts
new file mode 100644
index 0000000000..6ee167b58c
--- /dev/null
+++ b/frontend/e2e-tests/output.spec.ts
@@ -0,0 +1,26 @@
+/* Copyright 2023 Marimo. All rights reserved. */
+import { test, expect } from "@playwright/test";
+import { getAppUrl } from "../playwright.config";
+import { takeScreenshot } from "./helper";
+
+test("it can clear and append output", async ({ page }) => {
+ const appUrl = getAppUrl("output.py//run");
+ await page.goto(appUrl);
+
+ // Test that Loading replaced exists at least once
+ await expect(page.getByText("Loading replace")).toBeVisible();
+ // Now wait for Replaced to be visible
+ await expect(page.getByText("Replaced!")).toBeVisible();
+ // Test that Loading replaced does not exist
+ await expect(page.getByText("Loading replace")).not.toBeVisible();
+
+ // Test the end state of the output
+ await expect(page.getByText("Appended!")).toBeVisible();
+ await expect(page.getByText("Loading 0/5")).toBeVisible();
+ await expect(page.getByText("Loading 4/5")).toBeVisible();
+
+ // Test that Cleared does not exist
+ await expect(page.getByText("Cleared!")).not.toBeVisible();
+
+ await takeScreenshot(page, __filename);
+});
diff --git a/frontend/e2e-tests/py/output.py b/frontend/e2e-tests/py/output.py
new file mode 100644
index 0000000000..3d480abb36
--- /dev/null
+++ b/frontend/e2e-tests/py/output.py
@@ -0,0 +1,59 @@
+import marimo
+
+__generated_with = "0.1.19"
+app = marimo.App()
+
+
+def __():
+ import marimo as mo
+ import time
+ return mo, time
+
+
+def __(mo, time):
+ def loop_replace():
+ for i in range(5):
+ mo.output.replace(mo.md(f"Loading replace {i}/5"))
+ time.sleep(.1) # This is long enough to see the replace
+
+ def loop_append():
+ for i in range(5):
+ mo.output.append(mo.md(f"Loading {i}/5"))
+ time.sleep(.01) # This should be shorter than the replace
+ return loop_append, loop_replace
+
+
+def __(loop_replace, mo):
+ loop_replace()
+ mo.md("Replaced!")
+ return
+
+
+def __(loop_append, mo):
+ loop_append()
+ mo.output.append(mo.md("Appended!"))
+ return
+
+
+def __(loop_append, mo):
+ loop_append()
+ mo.output.append(mo.md("Cleared!"))
+ mo.output.clear()
+ return
+
+
+def __(loop_append, mo):
+ loop_append()
+ mo.output.append(mo.md("Cleared!"))
+ mo.output.replace(None)
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/frontend/playwright.config.ts b/frontend/playwright.config.ts
index f13cee760c..f96775d530 100644
--- a/frontend/playwright.config.ts
+++ b/frontend/playwright.config.ts
@@ -33,6 +33,7 @@ const appToOptions = {
"bugs.py": { port: port(), command: "edit" },
"layout_grid.py//edit": { port: port(), command: "edit" },
"layout_grid.py//run": { port: port(), command: "run" },
+ "output.py//run": { port: port(), command: "run" },
} satisfies Record<string, ServerOptions>;
export type ApplicationNames = keyof typeof appToOptions;
diff --git a/frontend/src/editor/Cell.tsx b/frontend/src/editor/Cell.tsx
index c3edd81f29..0e740354d5 100644
--- a/frontend/src/editor/Cell.tsx
+++ b/frontend/src/editor/Cell.tsx
@@ -40,6 +40,7 @@ import { Theme } from "../theme/useTheme";
import { CellActionsDropdown } from "./cell/cell-actions";
import { CellActionsContextMenu } from "./cell/cell-context-menu";
import { AppMode } from "@/core/mode";
+import useEvent from "react-use-event-hook";
/**
* Imperative interface of the cell.
@@ -141,7 +142,6 @@ const CellComponent = (
const editorView = useRef<EditorView | null>(null);
// DOM node where the editorView will be mounted
const editorViewParentRef = useRef<HTMLDivElement>(null);
- const runningOrQueuedRef = useRef<boolean | null>(null);
const needsRun = edited || interrupted;
const loading = status === "running" || status === "queued";
@@ -191,18 +191,25 @@ const CellComponent = (
[editorView, prepareToRunEffects]
);
- // Hack to provide the value of `running` to Code Mirror's EditorView.
- useEffect(() => {
- runningOrQueuedRef.current = loading;
- }, [loading]);
+ const handleRun = useEvent(() => {
+ if (loading) {
+ return;
+ }
- const onRun = useCallback(() => {
- if (!runningOrQueuedRef.current) {
- const code = prepareToRunEffects();
- registerRunStart();
- sendRun(cellId, code);
+ const code = prepareToRunEffects();
+ registerRunStart();
+ sendRun(cellId, code);
+ });
+
+ const handleDelete = useEvent(() => {
+ // Cannot delete running cells, since we're waiting for their output.
+ if (loading) {
+ return false;
}
- }, [cellId, registerRunStart, prepareToRunEffects]);
+
+ deleteCell({ cellId });
+ return true;
+ });
const createBelow = useCallback(
() => createNewCell({ cellId, before: false }),
@@ -234,15 +241,6 @@ const CellComponent = (
return;
}
- const deleteCellIfNotRunning = () => {
- // Cannot delete running cells, since we're waiting for their output.
- if (!runningOrQueuedRef.current) {
- deleteCell({ cellId });
- return true;
- }
- return false;
- };
-
const extensions = setupCodeMirror({
cellId,
showPlaceholder,
@@ -250,8 +248,8 @@ const CellComponent = (
updateCellCode,
},
cellMovementCallbacks: {
- onRun,
- deleteCell: deleteCellIfNotRunning,
+ onRun: handleRun,
+ deleteCell: handleDelete,
createAbove,
createBelow,
moveUp,
@@ -330,14 +328,14 @@ const CellComponent = (
showPlaceholder,
createAbove,
createBelow,
- deleteCell,
focusUp,
focusDown,
moveUp,
moveDown,
moveToNextCell,
updateCellCode,
- onRun,
+ handleDelete,
+ handleRun,
serializedEditorState,
]);
@@ -490,7 +488,7 @@ const CellComponent = (
<div className="flex align-bottom">
<RunButton
edited={edited}
- onClick={appClosed ? Functions.NOOP : onRun}
+ onClick={appClosed ? Functions.NOOP : handleRun}
appClosed={appClosed}
status={status}
config={cellConfig}