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
fix ui.array updating logic: check against frontend value (#289)
Akshay Agrawal2e3cae127823
diff --git a/marimo/_plugins/ui/_core/ui_element.py b/marimo/_plugins/ui/_core/ui_element.pyindex e5d3315c51..9b974ece4b 100644--- a/marimo/_plugins/ui/_core/ui_element.py+++ b/marimo/_plugins/ui/_core/ui_element.py@@ -69,6 +69,7 @@ class UIElement(Html, Generic[S, T], metaclass=abc.ABCMeta):- form: create a submittable form this `UIElement`."""+ _value_frontend: S_value: Tdef __init__(@@ -175,6 +176,7 @@ class UIElement(Html, Generic[S, T], metaclass=abc.ABCMeta):namespace=self._id, function=function)self._initial_value_frontend = initial_value+ self._value_frontend = initial_valueself._value = self._initial_value = self._convert_value(initial_value)self._on_change = on_change@@ -276,6 +278,7 @@ class UIElement(Html, Generic[S, T], metaclass=abc.ABCMeta):Calls the on_change handler with the element's new value as aside-effect."""+ self._value_frontend = valuetry:self._value = self._convert_value(value)except MarimoConvertValueException:diff --git a/marimo/_plugins/ui/_impl/array.py b/marimo/_plugins/ui/_impl/array.pyindex a9117028ad..b2cae9293e 100644--- a/marimo/_plugins/ui/_impl/array.py+++ b/marimo/_plugins/ui/_impl/array.py@@ -98,7 +98,7 @@ class array(UIElement[Dict[str, JSONType], Sequence[object]]):for k, v in value.items():element = self._elements[int(k)]# only call update if the value has changed- if element and element._value != v:+ if element and element._value_frontend != v:element._update(v)return [e._value for e in self._elements]diff --git a/marimo/_plugins/ui/_impl/test_array.py b/marimo/_plugins/ui/_impl/test_array.pyindex 90c1105afc..17cdc3c7e9 100644--- a/marimo/_plugins/ui/_impl/test_array.py+++ b/marimo/_plugins/ui/_impl/test_array.py@@ -43,3 +43,32 @@ def test_nested_array() -> None:outer._update({"0": {"1": "hello"}})assert outer.value == [[7, "hello"], ""]assert inner.value == [1, ""]+++def test_update_on_frontend_value_change_only() -> None:+ array = ui.array([ui.button(value=0, on_click=lambda v: v + 1)])+ # Multiple updates with the same value -- should only register once+ array._update({"0": 2})+ array._update({"0": 2})+ array._update({"0": 2})+ assert array.value == [1]+++def test_update_checks_against_frontend_value() -> None:+ class NoEquality:+ def __init__(self) -> None:+ pass++ def __eq__(self, other: object) -> bool:+ del other+ raise ValueError++ def __neq__(self, other: object) -> bool:+ del other+ raise ValueError++ v = NoEquality()+ array = ui.array([ui.dropdown({"option": v})])+ # smoke test: don't check against backend value, which will raise+ array._update({"0": ["option"]})+ assert len(array.value) == 1 and isinstance(array.value[0], NoEquality)