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.

improvement: Add scroll lock to bottom of console output (#828)

Myles Scolnickea52864bccf5
diff --git a/frontend/src/components/editor/code/readonly-python-code.tsx b/frontend/src/components/editor/code/readonly-python-code.tsx
index 82fb4de663..dd8d2c7bef 100644
--- a/frontend/src/components/editor/code/readonly-python-code.tsx
+++ b/frontend/src/components/editor/code/readonly-python-code.tsx
@@ -25,7 +25,12 @@ export const ReadonlyPythonCode = memo(
const [hideCode, setHideCode] = useState(initiallyHideCode);
return (
- <div className={cn("relative hover-actions-parent w-full", className)}>
+ <div
+ className={cn(
+ "relative hover-actions-parent w-full overflow-hidden",
+ className,
+ )}
+ >
{hideCode && <HideCodeButton onClick={() => setHideCode(false)} />}
{!hideCode && <FloatingCopyButton text={code} />}
<CodeMirror
diff --git a/frontend/src/components/editor/output/ConsoleOutput.tsx b/frontend/src/components/editor/output/ConsoleOutput.tsx
index 34f841376c..252eb5f839 100644
--- a/frontend/src/components/editor/output/ConsoleOutput.tsx
+++ b/frontend/src/components/editor/output/ConsoleOutput.tsx
@@ -1,5 +1,5 @@
/* Copyright 2024 Marimo. All rights reserved. */
-import React from "react";
+import React, { useLayoutEffect } from "react";
import { OutputMessage } from "@/core/kernel/messages";
import { formatOutput } from "../Output";
import { cn } from "@/utils/cn";
@@ -21,6 +21,7 @@ interface Props {
}
export const ConsoleOutput = (props: Props): React.ReactNode => {
+ const ref = React.useRef<HTMLDivElement>(null);
const { consoleOutputs, stale, cellName, cellId, onSubmitDebugger } = props;
/* The debugger UI needs some work. For now just use the regular
@@ -36,6 +37,28 @@ export const ConsoleOutput = (props: Props): React.ReactNode => {
const hasOutputs = consoleOutputs.length > 0;
+ // 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
+ //
+ // We use flex flex-col-reverse to handle this, but it doesn't
+ // always work perfectly when moved form the bottom and back.
+ useLayoutEffect(() => {
+ const el = ref.current;
+ if (!el) {
+ return;
+ }
+ // N.B. This won't handle large jumps in the scroll position
+ // if there is a lot of content added at once.
+ // This is 'good enough' for now.
+ const threshold = 120;
+
+ const scrollOffset = el.scrollHeight - el.clientHeight;
+ const distanceFromBottom = scrollOffset - el.scrollTop;
+ if (distanceFromBottom < threshold) {
+ el.scrollTop = scrollOffset;
+ }
+ });
+
if (!hasOutputs && cellName === DEFAULT_CELL_NAME) {
return null;
}
@@ -46,17 +69,20 @@ export const ConsoleOutput = (props: Props): React.ReactNode => {
);
};
+ const reversedOutputs = [...consoleOutputs].reverse();
+
return (
<div
title={stale ? "This console output is stale" : undefined}
data-testid="console-output-area"
+ ref={ref}
className={cn(
- "console-output-area overflow-hidden rounded-b-lg",
+ "console-output-area overflow-hidden rounded-b-lg flex flex-col-reverse w-full",
stale && "marimo-output-stale",
hasOutputs ? "p-5" : "p-3",
)}
>
- {consoleOutputs.map((output, idx) => {
+ {reversedOutputs.map((output, idx) => {
if (output.channel === "pdb") {
return null;
}
diff --git a/frontend/src/core/cells/__tests__/collapseConsoleOutputs.test.ts b/frontend/src/core/cells/__tests__/collapseConsoleOutputs.test.ts
index 513d2b1c9a..dcd4da638e 100644
--- a/frontend/src/core/cells/__tests__/collapseConsoleOutputs.test.ts
+++ b/frontend/src/core/cells/__tests__/collapseConsoleOutputs.test.ts
@@ -24,6 +24,25 @@ describe("collapseConsoleOutputs", () => {
expect(result[0].data).toMatchInlineSnapshot(`"Hello World"`);
});
+ it("should collapse last two text/plain outputs on the same channel if they end in newlines", () => {
+ const consoleOutputs: OutputMessage[] = [
+ {
+ mimetype: "text/plain",
+ channel: "output",
+ data: "Hello\n",
+ timestamp: 0,
+ },
+ {
+ mimetype: "text/plain",
+ channel: "output",
+ data: "World\n",
+ timestamp: 0,
+ },
+ ];
+ const result = collapseConsoleOutputs(consoleOutputs);
+ expect(result[0].data).toMatchInlineSnapshot(`"Hello\nWorld\n"`);
+ });
+
it("should not collapse outputs on different channels", () => {
const consoleOutputs: OutputMessage[] = [
{
diff --git a/frontend/src/core/cells/collapseConsoleOutputs.tsx b/frontend/src/core/cells/collapseConsoleOutputs.tsx
index 4f9efc3abb..968b08df4d 100644
--- a/frontend/src/core/cells/collapseConsoleOutputs.tsx
+++ b/frontend/src/core/cells/collapseConsoleOutputs.tsx
@@ -1,37 +1,64 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { OutputMessage } from "@/core/kernel/messages";
+import { invariant } from "@/utils/invariant";
-// collapses the last text/plain cells with the preceding one on the same
-// channel (if any), and handles bare carriage returns ("\r")
+/**
+ * Collapses the last text/plain cells with the preceding one on the same
+ * channel (if any), and handles bare carriage returns ("\r").
+ */
export function collapseConsoleOutputs(
consoleOutputs: OutputMessage[],
): OutputMessage[] {
- let nextOutput = consoleOutputs[consoleOutputs.length - 1];
- if (nextOutput.mimetype !== "text/plain") {
- return consoleOutputs;
+ if (consoleOutputs.length < 2) {
+ return handleCarriageReturns(consoleOutputs);
}
- // Skip stdin
- if (
- consoleOutputs.length >= 2 &&
- consoleOutputs[consoleOutputs.length - 2].mimetype === "text/plain" &&
- consoleOutputs[consoleOutputs.length - 2].channel === nextOutput.channel &&
- nextOutput.channel !== "stdin"
- ) {
+ const lastOutput = consoleOutputs[consoleOutputs.length - 1];
+ const secondLastOutput = consoleOutputs[consoleOutputs.length - 2];
+
+ if (shouldCollapse(lastOutput, secondLastOutput)) {
+ invariant(typeof lastOutput.data === "string", "expected string");
+ invariant(typeof secondLastOutput.data === "string", "expected string");
+
+ secondLastOutput.data += lastOutput.data;
consoleOutputs.pop();
- // eslint-disable-next-line @typescript-eslint/restrict-plus-operands
- consoleOutputs[consoleOutputs.length - 1].data += nextOutput.data;
- nextOutput = consoleOutputs[consoleOutputs.length - 1];
}
- if (nextOutput.mimetype !== "text/plain") {
+ return handleCarriageReturns(consoleOutputs);
+}
+
+function shouldCollapse(
+ lastOutput: OutputMessage,
+ secondLastOutput: OutputMessage,
+): boolean {
+ const isTextPlain =
+ lastOutput.mimetype === "text/plain" &&
+ secondLastOutput.mimetype === "text/plain";
+ if (!isTextPlain) {
+ return false;
+ }
+ const isSameChannel = lastOutput.channel === secondLastOutput.channel;
+ const isNotStdin = lastOutput.channel !== "stdin";
+
+ return isTextPlain && isSameChannel && isNotStdin;
+}
+
+function handleCarriageReturns(
+ consoleOutputs: OutputMessage[],
+): OutputMessage[] {
+ if (consoleOutputs.length === 0) {
+ return consoleOutputs;
+ }
+
+ const lastOutput = consoleOutputs[consoleOutputs.length - 1];
+ if (lastOutput.mimetype !== "text/plain") {
return consoleOutputs;
}
// eslint-disable-next-line no-control-regex
const carriagePattern = new RegExp("\r[^\n]", "g");
// collapse carriage returns in the final output's data
- let text = nextOutput.data;
+ let text = lastOutput.data;
let carriageIdx = text.search(carriagePattern);
while (carriageIdx > -1) {
// find the newline character preceding the carriage return, if any
@@ -54,6 +81,6 @@ export function collapseConsoleOutputs(
carriageIdx = text.search(carriagePattern);
}
- consoleOutputs[consoleOutputs.length - 1].data = text;
+ lastOutput.data = text;
return consoleOutputs;
}
diff --git a/frontend/src/css/Cell.css b/frontend/src/css/Cell.css
index 892bcbe0a0..82e53a6ad4 100644
--- a/frontend/src/css/Cell.css
+++ b/frontend/src/css/Cell.css
@@ -353,10 +353,10 @@
/* ------------------------------ Output Areas ------------------------------ */
-.output-area,
-.console-output-area {
+.output-area {
max-width: inherit;
width: 100%;
+ padding: 0.25rem 1.85rem;
/* Prevent floated elements from extending out of the output areas and into
* the editor. */
@@ -376,10 +376,6 @@
transition-delay: 200ms;
}
-.output-area {
- padding: 0.25rem 1.85rem;
-}
-
.console-output-area {
background-color: var(--gray-1);
}