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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: code_editor copy to clipboard default True (#4438)
Myles Scolnick682a29505d64
diff --git a/examples/ui/code_editor.py b/examples/ui/code_editor.pyindex 34f924835e..937586b711 100644--- a/examples/ui/code_editor.py+++ b/examples/ui/code_editor.py@@ -1,6 +1,8 @@++import marimo-__generated_with = "0.12.4"+__generated_with = "0.12.5"app = marimo.App(width="medium")@@ -17,12 +19,9 @@ def _(mo):..."""- editor = mo.ui.code_editor(- value=initial_code,- language="python"- )+ editor = mo.ui.code_editor(value=initial_code, language="python")editor- return editor, initial_code+ return (editor,)@app.cell@@ -36,10 +35,10 @@ def _(mo):copy_editor = mo.ui.code_editor(value="let a = 'b';",language="javascript",- show_copy_button = True+ show_copy_button=False,)copy_editor- return (copy_editor,)+ returnif __name__ == "__main__":diff --git a/frontend/src/components/icons/copy-icon.tsx b/frontend/src/components/icons/copy-icon.tsxindex d7de02822d..679d9f9512 100644--- a/frontend/src/components/icons/copy-icon.tsx+++ b/frontend/src/components/icons/copy-icon.tsx@@ -6,17 +6,20 @@ import { Tooltip } from "../ui/tooltip";import { cn } from "@/utils/cn";import { Events } from "@/utils/events";import { copyToClipboard } from "@/utils/copy";+import { toast } from "../ui/use-toast";interface Props {value: string | (() => string);className?: string;tooltip?: string | false;+ toastTitle?: string;}export const CopyClipboardIcon: React.FC<Props> = ({value,className,tooltip,+ toastTitle,}) => {const [isCopied, setIsCopied] = useState(false);@@ -25,6 +28,9 @@ export const CopyClipboardIcon: React.FC<Props> = ({await copyToClipboard(valueToCopy).then(() => {setIsCopied(true);setTimeout(() => setIsCopied(false), 2000);+ if (toastTitle) {+ toast({ title: toastTitle });+ }});});diff --git a/frontend/src/plugins/impl/CodeEditorPlugin.tsx b/frontend/src/plugins/impl/CodeEditorPlugin.tsxindex c45e8819b3..c08c466893 100644--- a/frontend/src/plugins/impl/CodeEditorPlugin.tsx+++ b/frontend/src/plugins/impl/CodeEditorPlugin.tsx@@ -5,6 +5,7 @@ import type { IPlugin, IPluginProps, Setter } from "../types";import { Labeled } from "./common/labeled";import { type Theme, useTheme } from "@/theme/useTheme";import { LazyAnyLanguageCodeMirror } from "./code/LazyAnyLanguageCodeMirror";+import { TooltipProvider } from "@/components/ui/tooltip";type T = string;@@ -57,19 +58,21 @@ const CodeEditorComponent = (props: CodeEditorComponentProps) => {const maxHeight = props.maxHeight ? `${props.maxHeight}px` : undefined;return (- <Labeled label={props.label} align="top" fullWidth={true}>- <LazyAnyLanguageCodeMirror- className={`cm [&>*]:outline-none border rounded overflow-hidden ${finalTheme}`}- theme={finalTheme === "dark" ? "dark" : "light"}- minHeight={minHeight}- maxHeight={maxHeight}- placeholder={props.placeholder}- editable={!props.disabled}- value={props.value}- language={props.language}- onChange={props.setValue}- showCopyButton={props.showCopyButton}- />- </Labeled>+ <TooltipProvider>+ <Labeled label={props.label} align="top" fullWidth={true}>+ <LazyAnyLanguageCodeMirror+ className={`cm [&>*]:outline-none border rounded overflow-hidden ${finalTheme}`}+ theme={finalTheme === "dark" ? "dark" : "light"}+ minHeight={minHeight}+ maxHeight={maxHeight}+ placeholder={props.placeholder}+ editable={!props.disabled}+ value={props.value}+ language={props.language}+ onChange={props.setValue}+ showCopyButton={props.showCopyButton}+ />+ </Labeled>+ </TooltipProvider>);};diff --git a/frontend/src/plugins/impl/code/any-language-editor.tsx b/frontend/src/plugins/impl/code/any-language-editor.tsxindex 23bb4500fe..5a8ac1d33a 100644--- a/frontend/src/plugins/impl/code/any-language-editor.tsx+++ b/frontend/src/plugins/impl/code/any-language-editor.tsx@@ -9,13 +9,10 @@ import {type LanguageName,} from "@uiw/codemirror-extensions-langs";import React, { useMemo } from "react";-import { CopyIcon } from "lucide-react";import { Logger } from "@/utils/Logger";import { ErrorBanner } from "../common/error-banner";import type { ResolvedTheme } from "@/theme/useTheme";-import { Button } from "@/components/ui/button";-import { copyToClipboard } from "@/utils/copy";-import { toast } from "@/components/ui/use-toast";+import { CopyClipboardIcon } from "@/components/icons/copy-icon";/*** A code editor that supports any language.@@ -45,7 +42,7 @@ const AnyLanguageCodeMirror: React.FC<}, [language, extensions]);return (- <div className="relative w-full hover-actions-parent">+ <div className="relative w-full group hover-actions-parent">{isNotSupported && (<ErrorBannerclassName="mb-1 rounded-sm"@@ -55,20 +52,12 @@ const AnyLanguageCodeMirror: React.FC</>)}{showCopyButton && !isNotSupported && (- <Button- key="copy-button"- data-testid="any-language-editor-copy-button"- variant="secondary"- size="xs"- className="absolute top-0 right-0 z-10 hover-action"- onClick={async () => {- await copyToClipboard(props.value || "");- toast({ title: "Copied to clipboard" });- }}- >- <CopyIcon className="w-4 h-4 mr-1" />- Copy code- </Button>+ <CopyClipboardIcon+ tooltip={false}+ className="absolute top-2 right-2 p-1 hover-action z-10 text-muted-foreground"+ value={props.value || ""}+ toastTitle="Copied to clipboard"+ />)}<ReactCodeMirror {...props} extensions={finalExtensions} /></div>diff --git a/marimo/_plugins/ui/_impl/dataframes/transforms/print_code.py b/marimo/_plugins/ui/_impl/dataframes/transforms/print_code.pyindex 9cec06085b..5f683a210f 100644--- a/marimo/_plugins/ui/_impl/dataframes/transforms/print_code.py+++ b/marimo/_plugins/ui/_impl/dataframes/transforms/print_code.py@@ -342,8 +342,6 @@ def python_print_ibis(return (f"{df_name}[{_as_literal(column_id)}] == {_as_literal(value)}")- elif operator == "does_not_equal" or operator == "!=":- return f"({_as_literal(column_id)}) != {_as_literal(value)}"elif operator == "does_not_equal" or operator == "!=":return (f"{df_name}[{_as_literal(column_id)}] != {_as_literal(value)})" # noqa: E501diff --git a/marimo/_plugins/ui/_impl/input.py b/marimo/_plugins/ui/_impl/input.pyindex 2a301643b2..3a722900d5 100644--- a/marimo/_plugins/ui/_impl/input.py+++ b/marimo/_plugins/ui/_impl/input.py@@ -834,7 +834,8 @@ class code_editor(UIElement[str, str]):label (str, optional): Markdown label for the element. Defaults to "".on_change (Callable[[str], None], optional): Optional callback to run whenthis element's value changes. Defaults to None.- show_copy_button (bool, optional): Whether to show a button to copy the code+ show_copy_button (bool): Whether to show a button to copy the code+ to the clipboard. Defaults to True."""_name: Final[str] = "marimo-code-editor"@@ -848,7 +849,7 @@ class code_editor(UIElement[str, str]):disabled: bool = False,min_height: Optional[int] = None,max_height: Optional[int] = None,- show_copy_button: Optional[bool] = None,+ show_copy_button: bool = True,*,label: str = "",on_change: Optional[Callable[[str], None]] = None,