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.

add selected_key to mo.ui.dropdown (#183)

Myles Scolnick8b0b8b8e4962
diff --git a/marimo/_plugins/ui/_impl/input.py b/marimo/_plugins/ui/_impl/input.py
index 192f832c6b..ea2ddc40c0 100644
--- a/marimo/_plugins/ui/_impl/input.py
+++ b/marimo/_plugins/ui/_impl/input.py
@@ -432,7 +432,7 @@ class dropdown(UIElement[List[str], Any]):
```python
dropdown = mo.ui.dropdown(
- options={'one': 1, 'two': 2, 'three': 3],
+ options={'one': 1, 'two': 2, 'three': 3},
value='one',
label='pick a number'
)
@@ -456,6 +456,7 @@ class dropdown(UIElement[List[str], Any]):
"""
_name: Final[str] = "marimo-dropdown"
+ _selected_key: Optional[str] = None
def __init__(
self,
@@ -499,10 +500,17 @@ class dropdown(UIElement[List[str], Any]):
def _convert_value(self, value: list[str]) -> Any:
if value:
assert len(value) == 1
+ self._selected_key = value[0]
return self.options[value[0]]
else:
+ self._selected_key = None
return None
+ @property
+ def selected_key(self) -> Optional[str]:
+ """The selected option's key, or `None` if no selection."""
+ return self._selected_key
+
@mddoc
class multiselect(UIElement[List[str], List[object]]):