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 debounce option to marimo-code-editor (#4461)
Florian Verdonck1199b75775ed
diff --git a/frontend/src/plugins/impl/CodeEditorPlugin.tsx b/frontend/src/plugins/impl/CodeEditorPlugin.tsxindex c08c466893..d646715ce3 100644--- a/frontend/src/plugins/impl/CodeEditorPlugin.tsx+++ b/frontend/src/plugins/impl/CodeEditorPlugin.tsx@@ -6,6 +6,10 @@ import { Labeled } from "./common/labeled";import { type Theme, useTheme } from "@/theme/useTheme";import { LazyAnyLanguageCodeMirror } from "./code/LazyAnyLanguageCodeMirror";import { TooltipProvider } from "@/components/ui/tooltip";+import { useCallback, useState, useMemo } from "react";+import { useDebounceControlledState } from "@/hooks/useDebounce";+import { EditorView } from "@codemirror/view";+import useEvent from "react-use-event-hook";type T = string;@@ -18,6 +22,7 @@ interface Data {minHeight?: number;maxHeight?: number;showCopyButton?: boolean;+ debounce: boolean | number;}export class CodeEditorPlugin implements IPlugin<T, Data> {@@ -33,6 +38,7 @@ export class CodeEditorPlugin implements IPlugin<T, Data> {minHeight: z.number().optional(),maxHeight: z.number().optional(),showCopyButton: z.boolean().optional(),+ debounce: z.union([z.boolean(), z.number()]).default(false),});render(props: IPluginProps<T, Data>): JSX.Element {@@ -57,6 +63,38 @@ const CodeEditorComponent = (props: CodeEditorComponentProps) => {const minHeight = props.minHeight ? `${props.minHeight}px` : "70px";const maxHeight = props.maxHeight ? `${props.maxHeight}px` : undefined;+ const [localValue, setLocalValue] = useState(props.value);+ const { onChange: setValueDebounced } = useDebounceControlledState<string>({+ initialValue: props.value,+ delay: Number.isFinite(props.debounce) ? (props.debounce as number) : 0,+ onChange: props.setValue,+ disabled: !Number.isFinite(props.debounce),+ });++ const handleChange = useCallback(+ (newValue: string) => {+ setLocalValue((_) => newValue);+ if (typeof props.debounce === "number") {+ setValueDebounced(newValue);+ } else if (!props.debounce) {+ props.setValue(newValue);+ }+ },+ [setValueDebounced, props.debounce, props.setValue],+ );++ const onBlur = useEvent(() => {+ props.setValue(localValue);+ });++ const extensions = useMemo(() => {+ if (props.debounce === true) {+ return [EditorView.domEventHandlers({ blur: onBlur })];+ }++ return [];+ }, [props.debounce, onBlur]);+return (<TooltipProvider><Labeled label={props.label} align="top" fullWidth={true}>@@ -67,10 +105,11 @@ const CodeEditorComponent = (props: CodeEditorComponentProps) => {maxHeight={maxHeight}placeholder={props.placeholder}editable={!props.disabled}- value={props.value}+ value={localValue}language={props.language}- onChange={props.setValue}+ onChange={handleChange}showCopyButton={props.showCopyButton}+ extensions={extensions}/></Labeled></TooltipProvider>diff --git a/marimo/_plugins/ui/_impl/input.py b/marimo/_plugins/ui/_impl/input.pyindex 3a722900d5..86f0e51cc8 100644--- a/marimo/_plugins/ui/_impl/input.py+++ b/marimo/_plugins/ui/_impl/input.py@@ -836,6 +836,9 @@ class code_editor(UIElement[str, str]):this element's value changes. Defaults to None.show_copy_button (bool): Whether to show a button to copy the codeto the clipboard. Defaults to True.+ debounce (bool | int, optional): Whether the input is debounced. If number,+ debounce by that many milliseconds. If True, then value is only emitted+ when the input loses focus. Defaults to False."""_name: Final[str] = "marimo-code-editor"@@ -850,6 +853,7 @@ class code_editor(UIElement[str, str]):min_height: Optional[int] = None,max_height: Optional[int] = None,show_copy_button: bool = True,+ debounce: bool | int = False,*,label: str = "",on_change: Optional[Callable[[str], None]] = None,@@ -875,6 +879,7 @@ class code_editor(UIElement[str, str]):"min-height": min_height,"max-height": max_height,"show-copy-button": show_copy_button,+ "debounce": debounce,},on_change=on_change,)