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.

improvement: allow mo.ui.dropdown more than 1000 items (#5098)

Myles Scolnickce7c64d0e476
diff --git a/marimo/_plugins/ui/_impl/input.py b/marimo/_plugins/ui/_impl/input.py
index ba6138975d..ceb5cb928c 100644
--- a/marimo/_plugins/ui/_impl/input.py
+++ b/marimo/_plugins/ui/_impl/input.py
@@ -974,6 +974,8 @@ class dropdown(UIElement[list[str], Any]):
Defaults to None.
searchable (bool, optional): Whether to enable search functionality.
Defaults to False.
+ If the number of options is greater than 1000, this will be set to True,
+ automatically.
label (str, optional): Markdown label for the element. Defaults to "".
on_change (Callable[[Any], None], optional): Optional callback to run when
this element's value changes. Defaults to None.
@@ -981,7 +983,7 @@ class dropdown(UIElement[list[str], Any]):
of its container. Defaults to False.
"""
- _MAX_OPTIONS: Final[int] = 1000
+ _FORCE_SEARCHABLE: Final[int] = 1000
_name: Final[str] = "marimo-dropdown"
_selected_key: Optional[str] = None
_RESERVED_OPTION: Final[str] = "--"
@@ -997,16 +999,10 @@ class dropdown(UIElement[list[str], Any]):
on_change: Optional[Callable[[Any], None]] = None,
full_width: bool = False,
) -> None:
- if len(options) > dropdown._MAX_OPTIONS:
- raise ValueError(
- "The maximum number of dropdown options allowed "
- f"is {dropdown._MAX_OPTIONS}, but your dropdown has "
- f"{len(options)} options. "
- "If you really want to expose that many options, consider "
- "using `mo.ui.text()` to let the user type an option name, "
- "and `mo.ui.table()` to present the options matching the "
- "user's query.",
- )
+ # Force searchable if there are too many options
+ # This makes the list 'virtualized' on the frontend
+ if len(options) > dropdown._FORCE_SEARCHABLE:
+ searchable = True
if not isinstance(options, dict):
options = {_to_option_name(option): option for option in options}
diff --git a/tests/_plugins/ui/_impl/test_input.py b/tests/_plugins/ui/_impl/test_input.py
index 58abd7e8f1..ab06b8acfc 100644
--- a/tests/_plugins/ui/_impl/test_input.py
+++ b/tests/_plugins/ui/_impl/test_input.py
@@ -392,11 +392,9 @@ def test_dropdown_with_non_string_options() -> None:
assert dd.value == "2"
-def test_dropdown_too_many_options() -> None:
- with pytest.raises(ValueError) as e:
- ui.dropdown(options={str(i): i for i in range(2000)})
-
- assert "maximum number" in str(e.value)
+def test_dropdown_lots_of_options() -> None:
+ dropdown = ui.dropdown(options={str(i): i for i in range(2000)})
+ assert dropdown._component_args["searchable"] is True
@pytest.mark.skipif(not HAS_PANDAS, reason="pandas not installed")