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: fix how names bound to lensed elements are resolved (#715)
Akshay Agrawale0b69d329391
diff --git a/marimo/_plugins/ui/_core/registry.py b/marimo/_plugins/ui/_core/registry.pyindex ad5f13c8e9..235eaa5319 100644--- a/marimo/_plugins/ui/_core/registry.py+++ b/marimo/_plugins/ui/_core/registry.py@@ -58,19 +58,35 @@ class UIElementRegistry:self._register_bindings(object_id)return self._bindings[object_id]+ def _has_parent_id(+ self, child: UIElement[Any, Any], parent_id: UIElementId+ ) -> bool:+ """Returns True if `child` has id `parent_id` or is a view of it"""+ if child._id == parent_id:+ return True+ elif child._lens is not None:+ element_ref = self._objects.get(child._lens.parent_id)+ element = element_ref() if element_ref is not None else None+ if element is not None:+ return self._has_parent_id(element, parent_id)+ return False+def _register_bindings(self, object_id: UIElementId) -> None:kernel = get_context().kernel- # Get all variable names that are bound to this UI element+ # Get all variable names that are either:+ # 1. bound to this UI element, or+ # 2. bound to a view (child) of this elementnames = set([namefor name in kernel.globalsif (isinstance(kernel.globals[name], UIElement)- and kernel.globals[name]._id == object_id+ and self._has_parent_id(kernel.globals[name], object_id))])+ print(names)self._bindings[object_id] = namesdef get_object(self, object_id: UIElementId) -> UIElement[Any, Any]:diff --git a/marimo/_smoke_tests/ansi.py b/marimo/_smoke_tests/ansi.pyindex 62a2432e06..470d4e9fae 100644--- a/marimo/_smoke_tests/ansi.py+++ b/marimo/_smoke_tests/ansi.py@@ -1,3 +1,4 @@+# Copyright 2024 Marimo. All rights reserved.import marimo__generated_with = "0.1.88"diff --git a/marimo/_smoke_tests/third_party/rich.py b/marimo/_smoke_tests/third_party/rich.pyindex 7555ff0563..4a2d3472a9 100644--- a/marimo/_smoke_tests/third_party/rich.py+++ b/marimo/_smoke_tests/third_party/rich.py@@ -1,3 +1,4 @@+# Copyright 2024 Marimo. All rights reserved.import marimo__generated_with = "0.1.88"diff --git a/tests/_plugins/ui/_core/test_registry.py b/tests/_plugins/ui/_core/test_registry.pyindex 25bf3f9cf9..3741994a88 100644--- a/tests/_plugins/ui/_core/test_registry.py+++ b/tests/_plugins/ui/_core/test_registry.py@@ -149,3 +149,20 @@ def test_lens_not_bound(k: Kernel, exec_req: ExecReqProvider) -> None:registry = get_context().ui_element_registryassert not registry.bound_names(array[0]._id)assert registry.bound_names(array._id) == set(["array"])+++def test_parent_bound_to_view(k: Kernel, exec_req: ExecReqProvider) -> None:+ k.run(+ [+ exec_req.get(+ """+ import marimo as mo+ array = mo.ui.array([mo.ui.text(), mo.ui.slider(1, 10)])+ child = array[0]+ """+ )+ ]+ )+ array = k.globals["array"]+ registry = get_context().ui_element_registry+ assert registry.bound_names(array._id) == set(["array", "child"])diff --git a/tests/_runtime/test_runtime.py b/tests/_runtime/test_runtime.pyindex ef0ff38fc5..d80835c61b 100644--- a/tests/_runtime/test_runtime.py+++ b/tests/_runtime/test_runtime.py@@ -132,6 +132,29 @@ def test_set_ui_element_value_lensed(assert k.globals["x"] == 6+def test_set_ui_element_value_lensed_bound_child(+ k: Kernel, exec_req: ExecReqProvider+) -> None:+ """Test setting the value of a lensed element.++ Make sure reactivity flows through its parent and also to names bound+ to children.+ """+ k.run([exec_req.get(code="import marimo as mo")])++ cell_one_code = """+ array = mo.ui.array([mo.ui.slider(0, 10, value=1)])+ child = array[0]+ """+ k.run([exec_req.get(code=cell_one_code)])+ k.run([exec_req.get(code="x = child.value + 1")])++ array_id = k.globals["array"]._id+ k.set_ui_element_value(SetUIElementValueRequest([(array_id, {"0": 5})]))+ assert k.globals["array"].value == [5]+ assert k.globals["x"] == 6++def test_set_ui_element_value_lensed_with_state(k: Kernel, exec_req: ExecReqProvider) -> None: