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 expand console output button (#7919)

Shahmir Varqha8dbec774fc80
diff --git a/frontend/src/components/editor/Output.tsx b/frontend/src/components/editor/Output.tsx
index 2a058951c0..7300ca1e34 100644
--- a/frontend/src/components/editor/Output.tsx
+++ b/frontend/src/components/editor/Output.tsx
@@ -1,12 +1,5 @@
/* Copyright 2026 Marimo. All rights reserved. */
-import React, {
- memo,
- Suspense,
- useEffect,
- useMemo,
- useRef,
- useState,
-} from "react";
+import React, { memo, Suspense, useMemo, useRef } from "react";
import { type CellId, CellOutputId } from "@/core/cells/ids";
import type { CellOutput, OutputMessage } from "@/core/kernel/messages";
import { cn } from "@/utils/cn";
@@ -30,6 +23,7 @@ import { tooltipHandler } from "@/components/charts/tooltip";
import { useExpandedOutput } from "@/core/cells/outputs";
import { viewStateAtom } from "@/core/mode";
import { useIframeCapabilities } from "@/hooks/useIframeCapabilities";
+import { useOverflowDetection } from "@/hooks/useOverflowDetection";
import { renderHTML } from "@/plugins/core/RenderHTML";
import { Banner } from "@/plugins/impl/common/error-banner";
import type { TopLevelFacetedUnitSpec } from "@/plugins/impl/data-explorer/queries/types";
@@ -427,28 +421,9 @@ const ExpandableOutput = React.memo(
}) => {
const containerRef = useRef<HTMLDivElement>(null);
const [isExpanded, setIsExpanded] = useExpandedOutput(cellId);
- const [isOverflowing, setIsOverflowing] = useState(false);
+ const isOverflowing = useOverflowDetection(containerRef);
const { hasFullscreen } = useIframeCapabilities();
- // Create resize observer to detect overflow
- useEffect(() => {
- if (!containerRef.current) {
- return;
- }
- const el = containerRef.current;
-
- const detectOverflow = () => {
- setIsOverflowing(el.scrollHeight > el.clientHeight);
- };
-
- const resizeObserver = new ResizeObserver(detectOverflow);
- resizeObserver.observe(el);
-
- return () => {
- resizeObserver.disconnect();
- };
- }, [props.id]);
-
return (
<>
<div>
diff --git a/frontend/src/components/editor/output/console/ConsoleOutput.tsx b/frontend/src/components/editor/output/console/ConsoleOutput.tsx
index adf3987f1a..bece78ff80 100644
--- a/frontend/src/components/editor/output/console/ConsoleOutput.tsx
+++ b/frontend/src/components/editor/output/console/ConsoleOutput.tsx
@@ -1,17 +1,25 @@
/* Copyright 2026 Marimo. All rights reserved. */
-import { ChevronRightIcon, WrapTextIcon } from "lucide-react";
+import {
+ ChevronRightIcon,
+ ChevronsDownUpIcon,
+ ChevronsUpDownIcon,
+ WrapTextIcon,
+} from "lucide-react";
import React, { useLayoutEffect } from "react";
import { ToggleButton } from "react-aria-components";
import { DebuggerControls } from "@/components/debugger/debugger-code";
import { CopyClipboardIcon } from "@/components/icons/copy-icon";
+import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Tooltip } from "@/components/ui/tooltip";
import type { CellId } from "@/core/cells/ids";
import { isInternalCellName } from "@/core/cells/names";
+import { useExpandedConsoleOutput } from "@/core/cells/outputs";
import type { WithResponse } from "@/core/cells/types";
import type { OutputMessage } from "@/core/kernel/messages";
import { useInputHistory } from "@/hooks/useInputHistory";
+import { useOverflowDetection } from "@/hooks/useOverflowDetection";
import { useSelectAllContent } from "@/hooks/useSelectAllContent";
import { cn } from "@/utils/cn";
import { invariant } from "@/utils/invariant";
@@ -45,6 +53,7 @@ export const ConsoleOutput = (props: Props) => {
const ConsoleOutputInternal = (props: Props): React.ReactNode => {
const ref = React.useRef<HTMLDivElement>(null);
const { wrapText, setWrapText } = useWrapText();
+ const [isExpanded, setIsExpanded] = useExpandedConsoleOutput(props.cellId);
const {
consoleOutputs,
stale,
@@ -72,6 +81,9 @@ const ConsoleOutputInternal = (props: Props): React.ReactNode => {
// Enable Ctrl/Cmd-A to select all content within the console output
const selectAllProps = useSelectAllContent(hasOutputs);
+ // Detect overflow on resize
+ const isOverflowing = useOverflowDetection(ref, hasOutputs);
+
// Keep scroll at the bottom if it is within 120px of the bottom,
// so when we add new content, it will lock to the bottom
//
@@ -121,7 +133,7 @@ const ConsoleOutputInternal = (props: Props): React.ReactNode => {
return (
<div className="relative group">
{hasOutputs && (
- <div className="absolute top-1 right-5 z-10 opacity-0 group-hover:opacity-100 flex gap-1">
+ <div className="absolute top-1 right-4 z-10 opacity-0 group-hover:opacity-100 flex items-center gap-1 print:hidden">
<CopyClipboardIcon
tooltip="Copy console output"
value={getOutputString}
@@ -139,6 +151,25 @@ const ConsoleOutputInternal = (props: Props): React.ReactNode => {
</ToggleButton>
</span>
</Tooltip>
+ {(isOverflowing || isExpanded) && (
+ <Button
+ aria-label={isExpanded ? "Collapse output" : "Expand output"}
+ className="p-0 mb-px"
+ onClick={() => setIsExpanded(!isExpanded)}
+ size="xs"
+ variant={null}
+ >
+ {isExpanded ? (
+ <Tooltip content="Collapse output">
+ <ChevronsDownUpIcon className="h-4 w-4" />
+ </Tooltip>
+ ) : (
+ <Tooltip content="Expand output">
+ <ChevronsUpDownIcon className="h-4 w-4 " />
+ </Tooltip>
+ )}
+ </Button>
+ )}
</div>
)}
<div
@@ -154,6 +185,7 @@ const ConsoleOutputInternal = (props: Props): React.ReactNode => {
hasOutputs ? "p-5" : "p-3",
className,
)}
+ style={isExpanded ? { maxHeight: "none" } : undefined}
>
{reversedOutputs.map((output, idx) => {
if (output.channel === "pdb") {
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 5fc8957dbb..c1a5b9e782 100644
--- a/frontend/src/components/editor/output/console/__tests__/ConsoleOutput.test.tsx
+++ b/frontend/src/components/editor/output/console/__tests__/ConsoleOutput.test.tsx
@@ -7,6 +7,19 @@ import type { CellId } from "@/core/cells/ids";
import type { OutputMessage } from "@/core/kernel/messages";
import { ConsoleOutput } from "../ConsoleOutput";
+// Mock ResizeObserver for tests
+global.ResizeObserver = class ResizeObserver {
+ observe() {
+ // noop
+ }
+ unobserve() {
+ // noop
+ }
+ disconnect() {
+ // noop
+ }
+};
+
describe("ConsoleOutput integration", () => {
const createOutput = (data: string, channel = "stdout"): OutputMessage => ({
channel: channel as "stdout" | "stderr",
diff --git a/frontend/src/core/cells/outputs.ts b/frontend/src/core/cells/outputs.ts
index d0c9dc9fd3..52206a9e1d 100644
--- a/frontend/src/core/cells/outputs.ts
+++ b/frontend/src/core/cells/outputs.ts
@@ -1,22 +1,34 @@
/* Copyright 2026 Marimo. All rights reserved. */
-import { useState } from "react";
+import { useEffect, useState } from "react";
import type { OutputMessage } from "../kernel/messages";
import type { CellId } from "./ids";
// This does not need to be overcomplicated. We can just store the expanded
// state in a global map instead of Jotai since state is not shared between cells.
const expandedOutputs: Record<CellId, boolean> = {};
+const expandedConsoleOutputs: Record<CellId, boolean> = {};
export function useExpandedOutput(cellId: CellId) {
const [state, setState] = useState(expandedOutputs[cellId] ?? false);
- return [
- state,
- (expanded: boolean) => {
- setState(expanded);
- expandedOutputs[cellId] = expanded;
- },
- ] as const;
+
+ // Sync state to external storage
+ useEffect(() => {
+ expandedOutputs[cellId] = state;
+ }, [cellId, state]);
+
+ return [state, setState] as const;
+}
+
+export function useExpandedConsoleOutput(cellId: CellId) {
+ const [state, setState] = useState(expandedConsoleOutputs[cellId] ?? false);
+
+ // Sync state to external storage
+ useEffect(() => {
+ expandedConsoleOutputs[cellId] = state;
+ }, [cellId, state]);
+
+ return [state, setState] as const;
}
export function isOutputEmpty(
diff --git a/frontend/src/hooks/__tests__/useOverflowDetection.test.tsx b/frontend/src/hooks/__tests__/useOverflowDetection.test.tsx
new file mode 100644
index 0000000000..571849970e
--- /dev/null
+++ b/frontend/src/hooks/__tests__/useOverflowDetection.test.tsx
@@ -0,0 +1,127 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+import { renderHook } from "@testing-library/react";
+import { useRef } from "react";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { useOverflowDetection } from "../useOverflowDetection";
+
+// Mock ResizeObserver
+const mockObserve = vi.fn();
+const mockDisconnect = vi.fn();
+
+global.ResizeObserver = class MockResizeObserver {
+ callback: ResizeObserverCallback;
+
+ constructor(callback: ResizeObserverCallback) {
+ this.callback = callback;
+ }
+
+ observe(target: Element) {
+ mockObserve(target);
+ // Simulate initial observation callback
+ this.callback([], this);
+ }
+
+ unobserve() {
+ // noop
+ }
+
+ disconnect() {
+ mockDisconnect();
+ }
+};
+
+describe("useOverflowDetection", () => {
+ beforeEach(() => {
+ mockObserve.mockClear();
+ mockDisconnect.mockClear();
+ });
+
+ it("should return false when ref is null", () => {
+ const { result } = renderHook(() => {
+ const ref = useRef<HTMLDivElement>(null);
+ return useOverflowDetection(ref);
+ });
+
+ expect(result.current).toBe(false);
+ expect(mockObserve).not.toHaveBeenCalled();
+ });
+
+ it("should observe element when ref is set", () => {
+ const element = document.createElement("div");
+
+ const { result } = renderHook(() => {
+ const ref = useRef<HTMLDivElement>(element);
+ return useOverflowDetection(ref);
+ });
+
+ expect(mockObserve).toHaveBeenCalledWith(element);
+ expect(result.current).toBe(false); // No overflow by default
+ });
+
+ it("should not observe when enabled is false", () => {
+ const element = document.createElement("div");
+
+ renderHook(() => {
+ const ref = useRef<HTMLDivElement>(element);
+ return useOverflowDetection(ref, false);
+ });
+
+ expect(mockObserve).not.toHaveBeenCalled();
+ });
+
+ it("should disconnect observer on unmount", () => {
+ const element = document.createElement("div");
+
+ const { unmount } = renderHook(() => {
+ const ref = useRef<HTMLDivElement>(element);
+ return useOverflowDetection(ref);
+ });
+
+ unmount();
+ expect(mockDisconnect).toHaveBeenCalled();
+ });
+
+ it("should re-observe when enabled changes from false to true", () => {
+ const element = document.createElement("div");
+
+ const { rerender } = renderHook(
+ ({ enabled }) => {
+ const ref = useRef<HTMLDivElement>(element);
+ return useOverflowDetection(ref, enabled);
+ },
+ { initialProps: { enabled: false } },
+ );
+
+ expect(mockObserve).not.toHaveBeenCalled();
+
+ rerender({ enabled: true });
+ expect(mockObserve).toHaveBeenCalledWith(element);
+ });
+
+ it("should detect overflow when scrollHeight > clientHeight", () => {
+ const element = document.createElement("div");
+ Object.defineProperty(element, "scrollHeight", { value: 500 });
+ Object.defineProperty(element, "clientHeight", { value: 200 });
+
+ const { result } = renderHook(() => {
+ const ref = useRef<HTMLDivElement>(element);
+ return useOverflowDetection(ref);
+ });
+
+ expect(result.current).toBe(true);
+ });
+
+ it("should not detect overflow when scrollHeight <= clientHeight", () => {
+ const element = document.createElement("div");
+ Object.defineProperty(element, "scrollHeight", { value: 100 });
+ Object.defineProperty(element, "clientHeight", { value: 200 });
+
+ const { result } = renderHook(() => {
+ const ref = useRef<HTMLDivElement>(element);
+ return useOverflowDetection(ref);
+ });
+
+ expect(result.current).toBe(false);
+ });
+});
diff --git a/frontend/src/hooks/useOverflowDetection.ts b/frontend/src/hooks/useOverflowDetection.ts
new file mode 100644
index 0000000000..0c377d17cd
--- /dev/null
+++ b/frontend/src/hooks/useOverflowDetection.ts
@@ -0,0 +1,36 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+import type { RefObject } from "react";
+import { useEffect, useState } from "react";
+
+/**
+ * Hook to detect if an element's content overflows its visible area.
+ * Uses ResizeObserver to track size changes.
+ *
+ * @param ref - Ref to the element to observe
+ * @param enabled - When this changes, re-setup the observer (use to handle element recreation)
+ */
+export function useOverflowDetection(
+ ref: RefObject<HTMLElement | null>,
+ enabled = true,
+): boolean {
+ const [isOverflowing, setIsOverflowing] = useState(false);
+
+ useEffect(() => {
+ const el = ref.current;
+ if (!el || !enabled) {
+ return;
+ }
+
+ const resizeObserver = new ResizeObserver(() => {
+ setIsOverflowing(el.scrollHeight > el.clientHeight);
+ });
+ resizeObserver.observe(el);
+
+ return () => {
+ resizeObserver.disconnect();
+ };
+ }, [ref, enabled]);
+
+ return isOverflowing;
+}