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
Add option for copy button in code-editor (#4412)
Florian Verdonck076ddc6832a3
diff --git a/examples/ui/code_editor.py b/examples/ui/code_editor.pyindex f26bfb88a9..34f924835e 100644--- a/examples/ui/code_editor.py+++ b/examples/ui/code_editor.py@@ -1,6 +1,6 @@import marimo-__generated_with = "0.10.6"+__generated_with = "0.12.4"app = marimo.App(width="medium")@@ -31,5 +31,16 @@ def _(editor):return+def _(mo):+ copy_editor = mo.ui.code_editor(+ value="let a = 'b';",+ language="javascript",+ show_copy_button = True+ )+ copy_editor+ return (copy_editor,)++if __name__ == "__main__":app.run()diff --git a/frontend/src/plugins/impl/CodeEditorPlugin.tsx b/frontend/src/plugins/impl/CodeEditorPlugin.tsxindex 93fff1a5c5..c45e8819b3 100644--- a/frontend/src/plugins/impl/CodeEditorPlugin.tsx+++ b/frontend/src/plugins/impl/CodeEditorPlugin.tsx@@ -16,6 +16,7 @@ interface Data {disabled?: boolean;minHeight?: number;maxHeight?: number;+ showCopyButton?: boolean;}export class CodeEditorPlugin implements IPlugin<T, Data> {@@ -30,6 +31,7 @@ export class CodeEditorPlugin implements IPlugin<T, Data> {disabled: z.boolean().optional(),minHeight: z.number().optional(),maxHeight: z.number().optional(),+ showCopyButton: z.boolean().optional(),});render(props: IPluginProps<T, Data>): JSX.Element {@@ -66,6 +68,7 @@ const CodeEditorComponent = (props: CodeEditorComponentProps) => {value={props.value}language={props.language}onChange={props.setValue}+ showCopyButton={props.showCopyButton}/></Labeled>);diff --git a/frontend/src/plugins/impl/code/any-language-editor.tsx b/frontend/src/plugins/impl/code/any-language-editor.tsxindex 4001826fa2..23bb4500fe 100644--- a/frontend/src/plugins/impl/code/any-language-editor.tsx+++ b/frontend/src/plugins/impl/code/any-language-editor.tsx@@ -9,9 +9,13 @@ 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";/*** A code editor that supports any language.@@ -23,8 +27,9 @@ const AnyLanguageCodeMirror: React.FC<ReactCodeMirrorProps & {language: string | undefined;theme: ResolvedTheme;+ showCopyButton?: boolean;}-> = ({ language, extensions = [], ...props }) => {+> = ({ language, showCopyButton, extensions = [], ...props }) => {const isNotSupported = language && !(language in langs);if (isNotSupported) {Logger.warn(`Language ${language} not found in CodeMirror.`);@@ -40,7 +45,7 @@ const AnyLanguageCodeMirror: React.FC<}, [language, extensions]);return (- <>+ <div className="relative w-full hover-actions-parent">{isNotSupported && (<ErrorBannerclassName="mb-1 rounded-sm"@@ -49,8 +54,24 @@ const AnyLanguageCodeMirror: React.FC<).join(", ")}`}/>)}+ {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>+ )}<ReactCodeMirror {...props} extensions={finalExtensions} />- </>+ </div>);};diff --git a/marimo/_plugins/ui/_impl/input.py b/marimo/_plugins/ui/_impl/input.pyindex f094cc3b57..2a301643b2 100644--- a/marimo/_plugins/ui/_impl/input.py+++ b/marimo/_plugins/ui/_impl/input.py@@ -834,6 +834,7 @@ 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"""_name: Final[str] = "marimo-code-editor"@@ -847,6 +848,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,*,label: str = "",on_change: Optional[Callable[[str], None]] = None,@@ -871,6 +873,7 @@ class code_editor(UIElement[str, str]):"disabled": disabled,"min-height": min_height,"max-height": max_height,+ "show-copy-button": show_copy_button,},on_change=on_change,)