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.

improve: Virtualize mo.ui.multiselect to handle large lists (#1654)

Wasim Sandhu3df9d96bc238
diff --git a/frontend/package.json b/frontend/package.json
index ef88cfa851..b207e180e7 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -117,6 +117,7 @@
"react-resizable-panels": "2.0.19",
"react-use-event-hook": "^0.9.6",
"react-vega": "^7.6.0",
+ "react-virtuoso": "^4.7.11",
"reactflow": "^11.11.3",
"rpc-anywhere": "^1.7.0",
"string-dedent": "^3.0.1",
diff --git a/frontend/pnpm-lock.yaml b/frontend/pnpm-lock.yaml
index 173d34aabf..6e5c81eec1 100644
--- a/frontend/pnpm-lock.yaml
+++ b/frontend/pnpm-lock.yaml
@@ -317,6 +317,9 @@ dependencies:
react-vega:
specifier: ^7.6.0
+ react-virtuoso:
+ specifier: ^4.7.11
reactflow:
specifier: ^11.11.3
@@ -16497,6 +16500,17 @@ packages:
vega-lite: 5.18.1([email protected])
dev: false
+ resolution: {integrity: sha512-Kdn9qEtQI2ulEuBMzW2BTkDsfijB05QUd6lpZ1K36oyA3k65Cz4lG4EKrh2pCfUafX4C2uMSZOwzMOhbrMOTFA==}
+ engines: {node: '>=10'}
+ peerDependencies:
+ react: '>=16 || >=17 || >= 18'
+ react-dom: '>=16 || >=17 || >= 18'
+ dependencies:
+ react: 18.3.1
+ react-dom: 18.3.1([email protected])
+ dev: false
+
resolution: {integrity: sha512-Y0Cx+dnU6NLa5/EvoHukUD0BklJ8qITCtVEPY1C/nL8wwoZ0b5aEw8Ff1dOVHw7fCzMt55XfJDd8S8W8LCaUCg==}
engines: {node: '>8.0.0'}
diff --git a/frontend/src/plugins/impl/MultiselectPlugin.tsx b/frontend/src/plugins/impl/MultiselectPlugin.tsx
index 657ba59a82..c39fc46a0a 100644
--- a/frontend/src/plugins/impl/MultiselectPlugin.tsx
+++ b/frontend/src/plugins/impl/MultiselectPlugin.tsx
@@ -1,11 +1,12 @@
/* Copyright 2024 Marimo. All rights reserved. */
-import { useId } from "react";
+import { useId, useMemo, useState } from "react";
import { z } from "zod";
import { IPlugin, IPluginProps, Setter } from "../types";
import { Combobox, ComboboxItem } from "../../components/ui/combobox";
import { Labeled } from "./common/labeled";
import { cn } from "@/utils/cn";
+import { Virtuoso } from "react-virtuoso";
interface Data {
label: string | null;
@@ -53,6 +54,16 @@ interface MultiselectProps extends Data {
const Multiselect = (props: MultiselectProps): JSX.Element => {
const id = useId();
+ const [searchQuery, setSearchQuery] = useState<string>("");
+
+ const filteredOptions = useMemo(() => {
+ if (!searchQuery) {
+ return props.options;
+ }
+ return props.options.filter(
+ (option) => multiselectFilterFn(option, searchQuery) === 1,
+ );
+ }, [props.options, searchQuery]);
function setValue(newValues: T) {
if (props.maxSelections != null && newValues.length > props.maxSelections) {
@@ -67,18 +78,34 @@ const Multiselect = (props: MultiselectProps): JSX.Element => {
displayValue={(option) => option}
placeholder="Select..."
multiple={true}
- filterFn={multiselectFilterFn}
className={cn({
"w-full": props.fullWidth,
})}
value={props.value}
onValueChange={(newValues) => setValue(newValues || [])}
+ shouldFilter={false}
+ search={searchQuery}
+ onSearchChange={setSearchQuery}
>
- {props.options.map((option) => (
- <ComboboxItem key={option} value={option}>
- {option}
- </ComboboxItem>
- ))}
+ {filteredOptions.length > 200 ? (
+ // List virtualization
+ <Virtuoso
+ style={{ height: "200px" }}
+ totalCount={filteredOptions.length}
+ overscan={50}
+ itemContent={(i: number) => (
+ <ComboboxItem key={filteredOptions[i]} value={filteredOptions[i]}>
+ {filteredOptions[i]}
+ </ComboboxItem>
+ )}
+ />
+ ) : (
+ filteredOptions.map((option) => (
+ <ComboboxItem key={option} value={option}>
+ {option}
+ </ComboboxItem>
+ ))
+ )}
</Combobox>
</Labeled>
);
diff --git a/marimo/_plugins/ui/_impl/input.py b/marimo/_plugins/ui/_impl/input.py
index 86e2299588..658d2e5820 100644
--- a/marimo/_plugins/ui/_impl/input.py
+++ b/marimo/_plugins/ui/_impl/input.py
@@ -901,7 +901,7 @@ class multiselect(UIElement[List[str], List[object]]):
- `max_selections`: maximum number of items that can be selected
"""
- _MAX_OPTIONS: Final[int] = 1000
+ _MAX_OPTIONS: Final[int] = 100000
_name: Final[str] = "marimo-multiselect"
def __init__(
diff --git a/marimo/_smoke_tests/bugs/1654-virtualize-multiselect.py b/marimo/_smoke_tests/bugs/1654-virtualize-multiselect.py
new file mode 100644
index 0000000000..fef04b8480
--- /dev/null
+++ b/marimo/_smoke_tests/bugs/1654-virtualize-multiselect.py
@@ -0,0 +1,87 @@
+# Copyright 2024 Marimo. All rights reserved.
+import marimo
+
+__generated_with = "0.6.19"
+app = marimo.App(app_title="1654 - Virtualize Multiselect")
+
+
+def __():
+ import marimo as mo
+ mo.md("[improve: Virtualize mo.ui.multiselect to handle large lists #1654](https://github.com/marimo-team/marimo/pull/1654)")
+ return mo,
+
+
+def __(mo):
+ fuzzy_match_test = ["foo bar", "bar foo", "foob", "foobar", "barfoo"]
+ mo.ui.multiselect(fuzzy_match_test, label="Fuzzy match search")
+ return fuzzy_match_test,
+
+
+def __(mo, xs_list):
+ mo.ui.multiselect(xs_list, label="Extra small list with 10 items")
+ return
+
+
+def __(mo, sm_list):
+ mo.ui.multiselect(sm_list, label="Small list with 100 items")
+ return
+
+
+def __(md_list, mo):
+ mo.ui.multiselect(md_list, label="Medium list with 500 items")
+ return
+
+
+def __(lg_list, mo):
+ mo.ui.multiselect(lg_list, label="Large list with 1K items")
+ return
+
+
+def __(mo, xl_list):
+ mo.ui.multiselect(xl_list, label="XL list with 10K items")
+ return
+
+
+def __(mo, xxl_list):
+ mo.ui.multiselect(xxl_list, label="XXL list with 100K items")
+ return
+
+
+def __(mo, xxxl_list):
+ mo.ui.multiselect(xxxl_list, label="XXXL list with 200K items")
+ return
+
+
+def __():
+ RANGE = 10000
+ xs_list = [f"Row {i}" for i in range(RANGE // 1000)]
+ sm_list = [f"Row {i}" for i in range(RANGE // 100)]
+ md_list = [f"Row {i}" for i in range(RANGE // 20)]
+ lg_list = [f"Row {i}" for i in range(RANGE // 10)]
+ xl_list = [f"Row {i}" for i in range(RANGE)]
+ xxl_list = [f"Row {i}" for i in range(RANGE * 10)]
+ xxxl_list = [f"Row {i}" for i in range(RANGE * 20)]
+ return (
+ RANGE,
+ lg_list,
+ md_list,
+ sm_list,
+ xl_list,
+ xs_list,
+ xxl_list,
+ xxxl_list,
+ )
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_plugins/ui/_impl/test_input.py b/tests/_plugins/ui/_impl/test_input.py
index 6720d38a7d..1a9f6ee6b5 100644
--- a/tests/_plugins/ui/_impl/test_input.py
+++ b/tests/_plugins/ui/_impl/test_input.py
@@ -303,7 +303,7 @@ def test_multiselect() -> None:
def test_multiselect_too_many_options() -> None:
with pytest.raises(ValueError) as e:
- ui.multiselect(options={str(i): i for i in range(2000)})
+ ui.multiselect(options={str(i): i for i in range(200000)})
assert "maximum number" in str(e.value)