Skip to content

Commit 9c47539

Browse files
author
Wasim
authored
improvement: Wrap output for lists and dictionaries (#2995)
## 📝 Summary Completes #2977. ## 🔍 Description of Changes Add switch to wrap text for list and dictionary (JSON) outputs. Makes it easier to read long string outputs 🙂 Before: <img width="847" alt="Screenshot 2024-11-27 at 10 25 32 PM" src="https://github.com/user-attachments/assets/b6d4c7e2-8293-4e5d-ad18-b220b83751a5"> After: <img width="836" alt="Screenshot 2024-11-27 at 10 25 16 PM" src="https://github.com/user-attachments/assets/e0cba359-a9a0-4b8a-b68b-a23175464a9b"> ## 📋 Checklist - [x] I have read the [contributor guidelines](https://github.com/marimo-team/marimo/blob/main/CONTRIBUTING.md). - [x] For large changes, or changes that affect the public API: this change was discussed or approved through an issue, on [Discord](https://marimo.io/discord?ref=pr), or the community [discussions](https://github.com/marimo-team/marimo/discussions) (Please provide a link if applicable). - [ ] I have added tests for the changes made. - [x] I have run the code and verified that it works as expected. ## 📜 Reviewers @akshayka OR @mscolnick
1 parent 0be51d3 commit 9c47539

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

frontend/src/components/editor/output/JsonOutput.tsx

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* Copyright 2024 Marimo. All rights reserved. */
2-
import { memo } from "react";
2+
import { memo, useState } from "react";
33
import {
44
type DataType,
55
JsonViewer,
@@ -45,7 +45,7 @@ export const JsonOutput: React.FC<Props> = memo(
4545
case "tree":
4646
return (
4747
<JsonViewer
48-
className={"marimo-json-output"}
48+
className="marimo-json-output"
4949
rootName={name}
5050
theme={theme}
5151
value={data}
@@ -75,6 +75,23 @@ function inferBestFormat(data: unknown): "tree" | "raw" {
7575
return typeof data === "object" && data !== null ? "tree" : "raw";
7676
}
7777

78+
// Text with length > 500 is collapsed by default, and can be expanded by clicking on it.
79+
const CollapsibleTextOutput = (props: { text: string }) => {
80+
const [isCollapsed, setIsCollapsed] = useState(true);
81+
return (
82+
<span className="cursor-pointer">
83+
{isCollapsed ? (
84+
<span onClick={() => setIsCollapsed(false)}>
85+
{props.text.slice(0, 500)}
86+
{props.text.length > 500 && "..."}
87+
</span>
88+
) : (
89+
<span onClick={() => setIsCollapsed(true)}>{props.text}</span>
90+
)}
91+
</span>
92+
);
93+
};
94+
7895
/**
7996
* Map from mimetype-prefix to render function.
8097
*
@@ -84,7 +101,7 @@ const LEAF_RENDERERS = {
84101
"image/": (value: string) => <ImageOutput src={value} />,
85102
"video/": (value: string) => <VideoOutput src={value} />,
86103
"text/html": (value: string) => <HtmlOutput html={value} inline={true} />,
87-
"text/plain": (value: string) => <TextOutput text={value} />,
104+
"text/plain": (value: string) => <CollapsibleTextOutput text={value} />,
88105
};
89106

90107
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -131,6 +148,6 @@ function renderLeaf(
131148
try {
132149
return render(leafData(leaf));
133150
} catch {
134-
return <TextOutput text={"Invalid leaf: {leaf}"} />;
151+
return <TextOutput text={`Invalid leaf: ${leaf}`} />;
135152
}
136153
}

frontend/src/components/editor/output/Outputs.css

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,13 @@
5555
.marimo-json-output {
5656
padding-top: 2px;
5757
padding-bottom: 2px;
58-
5958
/* content-visibility is set by material UI;
6059
* when set on an element that has overflow auto, causes abrupt jumps when
6160
* scrolling in Chrome */
6261
content-visibility: unset;
63-
6462
/* Coarse hack to stop children from overflowing; doesn't seem to affect
6563
* margin collapse. */
6664
overflow: auto;
67-
6865
@apply text-xs;
6966
}
7067

0 commit comments

Comments
 (0)