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 expand button to cell output (#419)

Myles Scolnickc3274b4c679f
diff --git a/frontend/src/components/editor/Cell.tsx b/frontend/src/components/editor/Cell.tsx
index 7e1cb45e17..0930858cd6 100644
--- a/frontend/src/components/editor/Cell.tsx
+++ b/frontend/src/components/editor/Cell.tsx
@@ -396,6 +396,7 @@ const CellComponent = (
const outputArea = (
<OutputArea
+ allowExpand={editing}
output={output}
className="output-area"
cellId={cellId}
diff --git a/frontend/src/components/editor/Output.tsx b/frontend/src/components/editor/Output.tsx
index b7d5e782a6..08bec26aae 100644
--- a/frontend/src/components/editor/Output.tsx
+++ b/frontend/src/components/editor/Output.tsx
@@ -1,5 +1,5 @@
/* Copyright 2023 Marimo. All rights reserved. */
-import React, { useMemo } from "react";
+import React, { useEffect, useMemo, useRef, useState } from "react";
import { OutputMessage } from "@/core/kernel/messages";
@@ -15,6 +15,10 @@ import { cn } from "@/utils/cn";
import { ErrorBoundary } from "./boundary/ErrorBoundary";
import "./output/Outputs.css";
+import { Button } from "../ui/button";
+import { ChevronsDownUpIcon, ChevronsUpDownIcon } from "lucide-react";
+import { Tooltip } from "../ui/tooltip";
+import { useExpandedOutput } from "@/core/cells/outputs";
/**
* Renders an output based on an OutputMessage.
@@ -64,57 +68,140 @@ export function formatOutput({
}
}
+interface OutputAreaProps {
+ output: OutputMessage | null;
+ cellId: CellId;
+ stale: boolean;
+ allowExpand: boolean;
+ className?: string;
+}
+
export const OutputArea = React.memo(
- (props: {
- output: OutputMessage | null;
- cellId: CellId;
- stale: boolean;
- className?: string;
- }) => {
+ ({ output, cellId, stale, allowExpand, className }: OutputAreaProps) => {
// Memoize parsing the json data
const parsedJsonData = useMemo(() => {
- if (!props.output) {
+ if (!output) {
return;
}
- const { mimetype, data } = props.output;
+ const { mimetype, data } = output;
switch (mimetype) {
case "application/json":
return typeof data === "string" ? JSON.parse(data) : data;
default:
return;
}
- }, [props.output]);
+ }, [output]);
- if (props.output === null) {
+ if (output === null) {
return null;
- } else if (props.output.channel === "output" && props.output.data === "") {
+ } else if (output.channel === "output" && output.data === "") {
return null;
} else {
// TODO(akshayka): More descriptive title
// 1. This output is stale (this cell has been edited but not run)
// 2. This output is stale (this cell is queued to run)
// 3. This output is stale (its inputs have changed)
- const title = props.stale ? "This output is stale" : undefined;
+ const title = stale ? "This output is stale" : undefined;
+ const Container = allowExpand ? ExpandableOutput : Div;
+
return (
<ErrorBoundary>
- <div
+ <Container
title={title}
- id={`output-${props.cellId}`}
- className={cn(
- props.stale && "marimo-output-stale",
- props.className
- )}
+ cellId={cellId}
+ id={`output-${cellId}`}
+ className={cn(stale && "marimo-output-stale", className)}
>
{formatOutput({
- message: props.output,
+ message: output,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
parsedJsonData: parsedJsonData as Record<string, any>,
})}
- </div>
+ </Container>
</ErrorBoundary>
);
}
}
);
OutputArea.displayName = "OutputArea";
+
+const Div = React.forwardRef<
+ HTMLDivElement,
+ React.ComponentPropsWithoutRef<"div">
+>((props, ref) => <div ref={ref} {...props} />);
+Div.displayName = "Div";
+
+/**
+ * Detects if there is overflow in the output area and adds a button to optionally expand
+ */
+const ExpandableOutput = React.memo(
+ ({
+ cellId,
+ children,
+ ...props
+ }: React.HTMLProps<HTMLDivElement> & {
+ cellId: CellId;
+ }) => {
+ const containerRef = useRef<HTMLDivElement>(null);
+ const [isExpanded, setIsExpanded] = useExpandedOutput(cellId);
+ const [isOverflowing, setIsOverflowing] = useState(false);
+
+ // 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 className="increase-pointer-area-x" />
+ <div
+ {...props}
+ className={cn("relative", props.className)}
+ ref={containerRef}
+ style={isExpanded ? { maxHeight: "none" } : undefined}
+ >
+ {children}
+ </div>
+ {(isOverflowing || isExpanded) && (
+ <Button
+ className={cn(
+ "absolute top-1 -right-10",
+ // Force show button if expanded
+ !isExpanded && "hover-action"
+ )}
+ onClick={() => setIsExpanded(!isExpanded)}
+ size="xs"
+ variant="text"
+ >
+ {isExpanded ? (
+ <Tooltip content="Collapse output" side="left">
+ <ChevronsDownUpIcon className="h-4 w-4" />
+ </Tooltip>
+ ) : (
+ <Tooltip content="Expand output" side="left">
+ <ChevronsUpDownIcon className="h-4 w-4" />
+ </Tooltip>
+ )}
+ </Button>
+ )}
+ </>
+ );
+ }
+);
+
+ExpandableOutput.displayName = "ExpandableOutput";
diff --git a/frontend/src/components/editor/renderers/grid-layout/grid-layout.tsx b/frontend/src/components/editor/renderers/grid-layout/grid-layout.tsx
index 2f30a8f31d..abbe631b70 100644
--- a/frontend/src/components/editor/renderers/grid-layout/grid-layout.tsx
+++ b/frontend/src/components/editor/renderers/grid-layout/grid-layout.tsx
@@ -363,7 +363,12 @@ const GridCell = memo(
side === "right" && "flex justify-end"
)}
>
- <OutputArea output={output} cellId={cellId} stale={loading} />
+ <OutputArea
+ allowExpand={false}
+ output={output}
+ cellId={cellId}
+ stale={loading}
+ />
</div>
);
}
diff --git a/frontend/src/components/editor/renderers/vertical-layout/vertical-layout.tsx b/frontend/src/components/editor/renderers/vertical-layout/vertical-layout.tsx
index 5fe9b78bca..d3520d3d76 100644
--- a/frontend/src/components/editor/renderers/vertical-layout/vertical-layout.tsx
+++ b/frontend/src/components/editor/renderers/vertical-layout/vertical-layout.tsx
@@ -8,6 +8,7 @@ import { ICellRendererPlugin, ICellRendererProps } from "../types";
import { VerticalLayoutWrapper } from "./vertical-layout-wrapper";
import { z } from "zod";
import { useDelayVisibility } from "./useDelayVisiblity";
+import { AppMode } from "@/core/mode";
type VerticalLayout = null;
type VerticalLayoutProps = ICellRendererProps<VerticalLayout>;
@@ -32,6 +33,7 @@ const VerticalLayoutRenderer: React.FC<VerticalLayoutProps> = ({
status={cell.status}
stopped={cell.stopped}
errored={cell.errored}
+ mode={mode}
interrupted={cell.interrupted}
/>
))}
@@ -45,6 +47,7 @@ interface VerticalCellProps
"output" | "status" | "stopped" | "errored" | "interrupted"
> {
cellId: CellId;
+ mode: AppMode;
}
const VerticalCell = memo(
@@ -55,6 +58,7 @@ const VerticalCell = memo(
stopped,
errored,
interrupted,
+ mode,
}: VerticalCellProps) => {
const cellRef = useRef<HTMLDivElement>(null);
const loading = status === "running" || status === "queued";
@@ -70,6 +74,7 @@ const VerticalCell = memo(
return hidden ? null : (
<div tabIndex={-1} id={HTMLId} ref={cellRef} className={className}>
<OutputArea
+ allowExpand={mode === "edit"}
output={output}
className="output-area"
cellId={cellId}
diff --git a/frontend/src/core/cells/outputs.ts b/frontend/src/core/cells/outputs.ts
new file mode 100644
index 0000000000..f381fd81f2
--- /dev/null
+++ b/frontend/src/core/cells/outputs.ts
@@ -0,0 +1,19 @@
+/* Copyright 2023 Marimo. All rights reserved. */
+
+import { useState } from "react";
+import { 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> = {};
+
+export function useExpandedOutput(cellId: CellId) {
+ const [state, setState] = useState(expandedOutputs[cellId] ?? false);
+ return [
+ state,
+ (expanded: boolean) => {
+ setState(expanded);
+ expandedOutputs[cellId] = expanded;
+ },
+ ] as const;
+}
diff --git a/frontend/src/stories/cell.stories.tsx b/frontend/src/stories/cell.stories.tsx
index 0c6fa727eb..c201c3a866 100644
--- a/frontend/src/stories/cell.stories.tsx
+++ b/frontend/src/stories/cell.stories.tsx
@@ -94,6 +94,27 @@ export const WithOutput: Story = {
),
};
+export const WithLargeOutput: Story = {
+ render: () => (
+ <div className="p-20 max-w-4xl">
+ <TooltipProvider>
+ <Cell
+ {...props}
+ runElapsedTimeMs={20}
+ output={{
+ channel: "output",
+ data: "<span class='markdown'><h1>Layout</h1>\n<p><code>marimo</code> provides functions to help you lay out your output, such as\nin rows and columns, accordions, tabs, and callouts. This tutorial\nshows some examples.</p></span>".repeat(
+ 10
+ ),
+ mimetype: "text/html",
+ timestamp: 1_686_863_688,
+ }}
+ />
+ </TooltipProvider>
+ </div>
+ ),
+};
+
export const UnsavedEditsOutput: Story = {
render: () => (
<div className="p-20 max-w-4xl">
diff --git a/frontend/tailwind.config.js b/frontend/tailwind.config.js
index 381f961aba..b868b789c1 100644
--- a/frontend/tailwind.config.js
+++ b/frontend/tailwind.config.js
@@ -1,4 +1,7 @@
/* Copyright 2023 Marimo. All rights reserved. */
+
+const plugin = require("tailwindcss/plugin");
+
/** @type {import('tailwindcss').Config} */
module.exports = {
darkMode: ["class"],
@@ -117,5 +120,34 @@ module.exports = {
},
},
},
- plugins: [require("tailwindcss-animate")],
+ plugins: [
+ require("tailwindcss-animate"),
+ plugin(function ({ addUtilities }) {
+ const newUtilities = {
+ ".increase-pointer-area-x": {
+ position: "absolute",
+ width: "100%",
+ height: "100%",
+ "&::before": {
+ content: '""',
+ position: "absolute",
+ top: "0",
+ bottom: "0",
+ left: "-50px",
+ width: "50px",
+ },
+ "&::after": {
+ content: '""',
+ position: "absolute",
+ top: "0",
+ bottom: "0",
+ right: "-50px",
+ width: "50px",
+ },
+ },
+ };
+
+ addUtilities(newUtilities);
+ }),
+ ],
};