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.

support copying raw value for console output (#6751)

Shahmir Varqha34b048b67b15
diff --git a/frontend/src/components/editor/output/ConsoleOutput.tsx b/frontend/src/components/editor/output/ConsoleOutput.tsx
index ed8637d808..e71d3beae1 100644
--- a/frontend/src/components/editor/output/ConsoleOutput.tsx
+++ b/frontend/src/components/editor/output/ConsoleOutput.tsx
@@ -1,10 +1,11 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { AnsiUp } from "ansi_up";
-import { ChevronRightIcon, CopyIcon, WrapTextIcon } from "lucide-react";
+import { ChevronRightIcon, 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 { Input } from "@/components/ui/input";
import { Tooltip } from "@/components/ui/tooltip";
import type { CellId } from "@/core/cells/ids";
@@ -13,7 +14,6 @@ import type { WithResponse } from "@/core/cells/types";
import type { OutputMessage } from "@/core/kernel/messages";
import { useSelectAllContent } from "@/hooks/useSelectAllContent";
import { cn } from "@/utils/cn";
-import { copyToClipboard } from "@/utils/copy";
import { ansiToPlainText, parseHtmlContent } from "@/utils/dom";
import { invariant } from "@/utils/invariant";
import { Strings } from "@/utils/strings";
@@ -112,39 +112,33 @@ const ConsoleOutputInternal = (props: Props): React.ReactNode => {
(output) => output.channel === "stdin",
);
+ const getOutputString = (): string => {
+ const text = consoleOutputs
+ .filter((output) => output.channel !== "pdb")
+ .map((output) => {
+ if (
+ output.mimetype.startsWith("application/vnd.marimo") ||
+ output.mimetype === "text/html"
+ ) {
+ return parseHtmlContent(Strings.asString(output.data));
+ }
+
+ // Convert ANSI to HTML, then parse as HTML
+ return ansiToPlainText(Strings.asString(output.data));
+ })
+ .join("\n");
+ return text;
+ };
+
return (
<div className="relative group">
{hasOutputs && (
<div className="absolute top-1 right-5 z-10 opacity-0 group-hover:opacity-100 flex gap-1">
- <Tooltip content="Copy all">
- <span>
- <button
- aria-label="Copy all console output"
- className="p-1 rounded bg-transparent text-muted-foreground hover:text-foreground"
- type="button"
- onClick={() => {
- const text = reversedOutputs
- .filter((output) => output.channel !== "pdb")
- .map((output) => {
- // If starts with `<`, then assume it's HTML
- if (
- typeof output.data === "string" &&
- output.data.startsWith("<")
- ) {
- return parseHtmlContent(output.data);
- }
-
- // Otherwise, convert the ANSI to HTML, then parse as HTML
- return ansiToPlainText(Strings.asString(output.data));
- })
- .join("\n");
- void copyToClipboard(text);
- }}
- >
- <CopyIcon className="h-4 w-4" />
- </button>
- </span>
- </Tooltip>
+ <CopyClipboardIcon
+ tooltip="Copy console output"
+ value={getOutputString}
+ className="h-4 w-4"
+ />
<Tooltip content={wrapText ? "Disable wrap text" : "Wrap text"}>
<span>
<ToggleButton
diff --git a/frontend/src/components/icons/copy-icon.tsx b/frontend/src/components/icons/copy-icon.tsx
index 3fa2a8185e..4bd540ea1b 100644
--- a/frontend/src/components/icons/copy-icon.tsx
+++ b/frontend/src/components/icons/copy-icon.tsx
@@ -9,10 +9,11 @@ import { Tooltip } from "../ui/tooltip";
import { toast } from "../ui/use-toast";
interface Props {
- value: string | (() => string);
+ value: string | ((event: React.MouseEvent) => string);
className?: string;
- tooltip?: string | false;
+ tooltip?: React.ReactNode | false;
toastTitle?: string;
+ ariaLabel?: string;
}
export const CopyClipboardIcon: React.FC<Props> = ({
@@ -20,11 +21,12 @@ export const CopyClipboardIcon: React.FC<Props> = ({
className,
tooltip,
toastTitle,
+ ariaLabel,
}) => {
const [isCopied, setIsCopied] = useState(false);
- const handleCopy = Events.stopPropagation(async () => {
- const valueToCopy = typeof value === "function" ? value() : value;
+ const handleCopy = Events.stopPropagation(async (event: React.MouseEvent) => {
+ const valueToCopy = typeof value === "function" ? value(event) : value;
await copyToClipboard(valueToCopy).then(() => {
setIsCopied(true);
setTimeout(() => setIsCopied(false), 2000);
@@ -35,7 +37,11 @@ export const CopyClipboardIcon: React.FC<Props> = ({
});
const button = (
- <button type="button" onClick={handleCopy} aria-label="Copy to clipboard">
+ <button
+ type="button"
+ onClick={handleCopy}
+ aria-label={ariaLabel ?? "Copy to clipboard"}
+ >
{isCopied ? (
<CheckIcon className={cn(className, "text-(--grass-11)")} />
) : (
diff --git a/frontend/src/components/shortcuts/renderShortcut.tsx b/frontend/src/components/shortcuts/renderShortcut.tsx
index 44f900cef4..66187cad09 100644
--- a/frontend/src/components/shortcuts/renderShortcut.tsx
+++ b/frontend/src/components/shortcuts/renderShortcut.tsx
@@ -102,7 +102,9 @@ export const MinimalHotkeys: React.FC<{
);
};
-function prettyPrintHotkey(key: string): [label: string, symbol?: string] {
+export function prettyPrintHotkey(
+ key: string,
+): [label: string, symbol?: string] {
const platform = isPlatformMac() ? "mac" : "default";
const lowerKey = key.toLowerCase();