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: debounce clearing of console outputs to prevent flickering (#8621)

Myles Scolnick8bec3b1e6799
diff --git a/frontend/src/components/editor/output/console/ConsoleOutput.tsx b/frontend/src/components/editor/output/console/ConsoleOutput.tsx
index 61c07b6138..59f6a60685 100644
--- a/frontend/src/components/editor/output/console/ConsoleOutput.tsx
+++ b/frontend/src/components/editor/output/console/ConsoleOutput.tsx
@@ -6,7 +6,7 @@ import {
ChevronsUpDownIcon,
WrapTextIcon,
} from "lucide-react";
-import React, { useLayoutEffect } from "react";
+import React, { useEffect, useLayoutEffect, useRef, useState } from "react";
import { ToggleButton } from "react-aria-components";
import { DebuggerControls } from "@/components/debugger/debugger-code";
import { CopyClipboardIcon } from "@/components/icons/copy-icon";
@@ -33,6 +33,52 @@ import { useWrapText } from "../useWrapText";
import { processOutput } from "./process-output";
import { RenderTextWithLinks } from "./text-rendering";
+/**
+ * Delay in ms before clearing console outputs.
+ * This prevents flickering when a cell re-runs and outputs are briefly cleared
+ * before new outputs arrive (e.g., plt.show() with a slider).
+ */
+export const CONSOLE_CLEAR_DEBOUNCE_MS = 200;
+
+/**
+ * Debounces the clearing of console outputs.
+ * - Non-empty updates are applied immediately.
+ * - Transitions to empty are delayed by CONSOLE_CLEAR_DEBOUNCE_MS,
+ * giving new outputs a chance to arrive and replace the old ones
+ * without a visible flicker.
+ */
+function useDebouncedConsoleOutputs<T>(outputs: T[]): T[] {
+ const [debouncedOutputs, setDebouncedOutputs] = useState(outputs);
+ const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
+
+ // Non-empty outputs: apply immediately and cancel any pending clear
+ if (outputs.length > 0 && debouncedOutputs !== outputs) {
+ if (timerRef.current !== null) {
+ clearTimeout(timerRef.current);
+ timerRef.current = null;
+ }
+ setDebouncedOutputs(outputs);
+ }
+
+ // Empty outputs: delay the clear so new outputs can arrive first
+ useEffect(() => {
+ if (outputs.length === 0 && timerRef.current === null) {
+ timerRef.current = setTimeout(() => {
+ timerRef.current = null;
+ setDebouncedOutputs([]);
+ }, CONSOLE_CLEAR_DEBOUNCE_MS);
+ }
+ return () => {
+ if (timerRef.current !== null) {
+ clearTimeout(timerRef.current);
+ timerRef.current = null;
+ }
+ };
+ }, [outputs]);
+
+ return debouncedOutputs;
+}
+
interface Props {
cellId: CellId;
cellName: string;
@@ -63,7 +109,7 @@ const ConsoleOutputInternal = (props: Props): React.ReactNode => {
setValue: setStdinValue,
});
const {
- consoleOutputs,
+ consoleOutputs: rawConsoleOutputs,
stale,
cellName,
cellId,
@@ -73,6 +119,9 @@ const ConsoleOutputInternal = (props: Props): React.ReactNode => {
className,
} = props;
+ // Debounce clearing to prevent flickering when cells re-run
+ const consoleOutputs = useDebouncedConsoleOutputs(rawConsoleOutputs);
+
/* The debugger UI needs some work. For now just use the regular
/* console output. */
/* if (debuggerActive) {
diff --git a/frontend/src/components/editor/output/console/__tests__/ConsoleOutput.test.tsx b/frontend/src/components/editor/output/console/__tests__/ConsoleOutput.test.tsx
index 5a08956a8a..c83895240b 100644
--- a/frontend/src/components/editor/output/console/__tests__/ConsoleOutput.test.tsx
+++ b/frontend/src/components/editor/output/console/__tests__/ConsoleOutput.test.tsx
@@ -1,13 +1,13 @@
/* Copyright 2026 Marimo. All rights reserved. */
-import { fireEvent, render, screen } from "@testing-library/react";
-import { describe, expect, it, vi } from "vitest";
+import { act, fireEvent, render, screen } from "@testing-library/react";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { SetupMocks } from "@/__mocks__/common";
import { TooltipProvider } from "@/components/ui/tooltip";
import type { CellId } from "@/core/cells/ids";
import type { WithResponse } from "@/core/cells/types";
import type { OutputMessage } from "@/core/kernel/messages";
-import { ConsoleOutput } from "../ConsoleOutput";
+import { CONSOLE_CLEAR_DEBOUNCE_MS, ConsoleOutput } from "../ConsoleOutput";
SetupMocks.resizeObserver();
@@ -193,3 +193,94 @@ describe("ConsoleOutput pdb history", () => {
expect(input).toHaveValue("");
});
});
+
+describe("ConsoleOutput debounced clearing", () => {
+ beforeEach(() => {
+ vi.useFakeTimers();
+ });
+
+ afterEach(() => {
+ vi.useRealTimers();
+ });
+
+ const createOutput = (
+ data: string,
+ channel = "stdout",
+ ): WithResponse<OutputMessage> => ({
+ channel: channel as "stdout" | "stderr",
+ mimetype: "text/plain",
+ data,
+ timestamp: 0,
+ response: undefined,
+ });
+
+ const defaultProps = {
+ cellId: "cell-1" as CellId,
+ cellName: "test_cell",
+ consoleOutputs: [] as WithResponse<OutputMessage>[],
+ stale: false,
+ debuggerActive: false,
+ onSubmitDebugger: vi.fn(),
+ };
+
+ it("should keep old outputs visible when cleared, then show new outputs immediately", () => {
+ const outputs1 = [createOutput("hello world")];
+
+ const { rerender } = renderWithProvider(
+ <ConsoleOutput {...defaultProps} consoleOutputs={outputs1} />,
+ );
+
+ // Old output is visible
+ expect(screen.getByText("hello world")).toBeInTheDocument();
+
+ // Clear outputs (simulates cell re-run)
+ rerender(
+ <TooltipProvider>
+ <ConsoleOutput {...defaultProps} consoleOutputs={[]} />
+ </TooltipProvider>,
+ );
+
+ // Old output should still be visible during debounce period
+ expect(screen.getByText("hello world")).toBeInTheDocument();
+
+ // New outputs arrive before debounce fires
+ const outputs2 = [createOutput("new output")];
+ rerender(
+ <TooltipProvider>
+ <ConsoleOutput {...defaultProps} consoleOutputs={outputs2} />
+ </TooltipProvider>,
+ );
+
+ // New output should be shown immediately
+ expect(screen.getByText("new output")).toBeInTheDocument();
+ expect(screen.queryByText("hello world")).not.toBeInTheDocument();
+ });
+
+ it("should clear outputs after debounce period if no new outputs arrive", () => {
+ const outputs1 = [createOutput("old output")];
+
+ const { rerender } = renderWithProvider(
+ <ConsoleOutput {...defaultProps} consoleOutputs={outputs1} />,
+ );
+
+ expect(screen.getByText("old output")).toBeInTheDocument();
+
+ // Clear outputs
+ rerender(
+ <TooltipProvider>
+ <ConsoleOutput {...defaultProps} consoleOutputs={[]} />
+ </TooltipProvider>,
+ );
+
+ // Still visible during debounce
+ expect(screen.getByText("old output")).toBeInTheDocument();
+
+ // Advance past debounce period
+ act(() => {
+ vi.advanceTimersByTime(CONSOLE_CLEAR_DEBOUNCE_MS + 1);
+ });
+
+ // Now the output should be cleared
+ expect(screen.queryByText("old output")).not.toBeInTheDocument();
+ });
+});
diff --git a/marimo/_smoke_tests/_matplotlib/with_slider.py b/marimo/_smoke_tests/_matplotlib/with_slider.py
new file mode 100644
index 0000000000..c4d5646751
--- /dev/null
+++ b/marimo/_smoke_tests/_matplotlib/with_slider.py
@@ -0,0 +1,34 @@
+import marimo
+
+__generated_with = "0.20.4"
+app = marimo.App(width="medium")
+
+
+def _():
+ import marimo as mo
+
+ return (mo,)
+
+
+def _(mo):
+ v = mo.ui.slider(start=6.28, stop=62.8, step=0.1, show_value=True)
+ v
+ return (v,)
+
+
+def _(v):
+ import numpy as np
+ import matplotlib.pyplot as plt
+
+ x = np.arange(0, 1, 0.001)
+ y = np.sin(x * v.value)
+ plt.plot(x, y)
+ plt.show()
+ return
+
+
+if __name__ == "__main__":
+ app.run()