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.

fix: anywidget reactivity (#2104)

Myles Scolnick9c8ab6579423
diff --git a/marimo/_plugins/ui/_impl/from_anywidget.py b/marimo/_plugins/ui/_impl/from_anywidget.py
index 462c0e9430..06ebd27ac0 100644
--- a/marimo/_plugins/ui/_impl/from_anywidget.py
+++ b/marimo/_plugins/ui/_impl/from_anywidget.py
@@ -92,6 +92,7 @@ class anywidget(UIElement[T, T]):
"tooltip",
"keys",
"_esm",
+ "_css",
"_anywidget_id",
"_dom_classes",
"_model_module",
@@ -165,7 +166,10 @@ class anywidget(UIElement[T, T]):
# forward all setattr to the widget
def __setattr__(self, name: str, value: Any) -> None:
if self._initialized:
- return setattr(self.widget, name, value)
+ # If the widget has the attribute, set it
+ if hasattr(self.widget, name):
+ return setattr(self.widget, name, value)
+ return super().__setattr__(name, value)
return super().__setattr__(name, value)
# After the widget has been initialized
diff --git a/tests/_plugins/ui/_impl/test_anywidget.py b/tests/_plugins/ui/_impl/test_anywidget.py
index c1a4763af1..5faeee4b03 100644
--- a/tests/_plugins/ui/_impl/test_anywidget.py
+++ b/tests/_plugins/ui/_impl/test_anywidget.py
@@ -5,6 +5,7 @@ import pytest
from marimo._dependencies.dependencies import DependencyManager
from marimo._plugins.ui._impl.from_anywidget import anywidget
+from marimo._runtime.requests import SetUIElementValueRequest
from marimo._runtime.runtime import Kernel
from tests.conftest import ExecReqProvider
@@ -108,9 +109,22 @@ x = as_marimo_element.count
),
]
)
- assert isinstance(k.globals["w"], anywidget)
+
+ ui_element = k.globals["w"]
+ assert isinstance(ui_element, anywidget)
assert k.globals["w_value"]["count"] == 10
assert k.globals["w_count"] == 10
+ assert ui_element.value == {"count": 10}
+
+ await k.set_ui_element_value(
+ SetUIElementValueRequest.from_ids_and_values(
+ [(ui_element._id, {"count": 5})]
+ )
+ )
+
+ assert k.globals["w_value"]["count"] == 5
+ assert k.globals["w_count"] == 5
+ assert ui_element.value == {"count": 5}
@staticmethod
async def test_getters_setters() -> None:
@@ -147,3 +161,10 @@ x = as_marimo_element.count
assert wrapped.widget.count == 7 # type: ignore
assert wrapped._initialized is True
+
+ @staticmethod
+ async def test_can_set_value() -> None:
+ wrapped = anywidget(CounterWidget())
+ assert wrapped.value == {"count": 0}
+ wrapped._update({"count": 10})
+ assert wrapped.value == {"count": 10}