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
docs: marimo.ui.dictionary (#2712)
eugene45056be4c37e
diff --git a/examples/ui/arrays_and_dicts.py b/examples/ui/arrays_and_dicts.pyindex aaa2b72f65..09b045ed40 100644--- a/examples/ui/arrays_and_dicts.py+++ b/examples/ui/arrays_and_dicts.py@@ -7,7 +7,7 @@import marimo-__generated_with = "0.8.19"+__generated_with = "0.9.14"app = marimo.App()@@ -78,6 +78,55 @@ def __(array, dictionary, mo):return+def __(mo):+ mo.md(+ r"""+ Fundamental difference between marimo dict and standard python dict.++ The UI elements in marimo dict are clones of the original: interacting with the marimo dictionary will not update the original elements, and vice versa.++ This allows you to reuse multiple UI components (e.g., sliders, text inputs, or date pickers) independently, ensuring they don’t interfere with each other when managed through marimo dict.++ However, if you use a regular python dict, all references remain synchronized, and changes to one element propagate across all linked instances.+ """+ )+ return++[email protected](hide_code=True)+def __(create, mo):+ create++ slider = mo.ui.slider(1, 10, show_value=True)+ text = mo.ui.text()+ date = mo.ui.date()++ mo_d = mo.ui.dictionary(+ {+ "slider": slider,+ "text": text,+ "date": date,+ }+ )++ py_d = {+ "slider": slider,+ "text": text,+ "date": date,+ }++ mo.hstack(+ [+ mo.vstack(["marimo dict", mo_d]),+ mo.vstack(["original elements", mo.vstack([slider, text, date])]),+ mo.vstack(["python dict", py_d]),+ ],+ justify="space-around",+ )+ return date, mo_d, py_d, slider, text++@app.celldef __():import marimo as modiff --git a/marimo/_plugins/ui/_impl/dictionary.py b/marimo/_plugins/ui/_impl/dictionary.pyindex 1152f96b54..5c5aeb8e45 100644--- a/marimo/_plugins/ui/_impl/dictionary.py+++ b/marimo/_plugins/ui/_impl/dictionary.py@@ -34,6 +34,14 @@ class dictionary(_batch_base):elements: interacting with the dictionary will _not_ update the originalelements, and vice versa.+ This allows you to reuse multiple UI components (e.g., sliders, text+ inputs, or date pickers) independently, ensuring they don’t interfere with+ each other when managed through `mo.ui.dictionary`.++ However, if you use a regular Python dictionary, all references remain+ synchronized, and changes to one element propagate across all linked+ instances.+**Examples.**A heterogeneous collection of UI elements:@@ -59,16 +67,29 @@ class dictionary(_batch_base):Access and output a UI element in the array:```python- mo.md(f"This is a slider: d['slider']")+ mo.md(f"This is a slider: {d['slider']}")```Some number of UI elements, determined at runtime:```python- mo.ui.dictionary({- f"option {i}": mo.ui.slider(1, 10)- for i in range random.randint(4, 8)- })+ mo.ui.dictionary(+ {+ f"option {i}": mo.ui.slider(1, 10)+ for i in range(random.randint(4, 8))+ }+ )+ ```++ Quick layouts of UI elements:++ ```python+ mo.ui.dictionary(+ {+ f"option {i}": mo.ui.slider(1, 10)+ for i in range(random.randint(4, 8))+ }+ ).vstack() # Can also use `hstack`, `callout`, `center`, etc.```**Attributes.**