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
improvement: proxy attributes for `mo.ui.anywidget` (#1694)
Myles Scolnickf825f22097ad
diff --git a/docs/api/inputs/anywidget.md b/docs/api/inputs/anywidget.mdindex d6d77b364a..1ced2c9509 100644--- a/docs/api/inputs/anywidget.md+++ b/docs/api/inputs/anywidget.md@@ -51,6 +51,9 @@ widget = mo.ui.anywidget(CounterWidget())# In another cell, you can access the widget's valuewidget.value++# You can also access the widget's specific properties+widget.count```## Importing a widget@@ -63,6 +66,10 @@ widget = mo.ui.anywidget(ScatterWidget())# In another cell, you can access the widget's valuewidget.value++# You can also access the widget's specific properties+widget.data+widget.data_as_polars```---diff --git a/marimo/_plugins/ui/_impl/from_anywidget.py b/marimo/_plugins/ui/_impl/from_anywidget.pyindex 65c36bf97f..775a3c3fb3 100644--- a/marimo/_plugins/ui/_impl/from_anywidget.py+++ b/marimo/_plugins/ui/_impl/from_anywidget.py@@ -1,4 +1,6 @@# Copyright 2024 Marimo. All rights reserved.+from __future__ import annotations+import weakreffrom typing import TYPE_CHECKING, Any, Dict@@ -31,6 +33,7 @@ T = Dict[str, Any]class anywidget(UIElement[T, T]):"""Create a UIElement from an AnyWidget.+ This proxies all the widget's attributes and methods.**Example.**@@ -38,10 +41,16 @@ class anywidget(UIElement[T, T]):from drawdata import ScatterWidgetimport marimo as mo- widget = mo.ui.anywidget(ScatterWidget())+ scatter = ScatterWidget()+ scatter = mo.ui.anywidget(scatter)# In another cell, access its value- widget.value+ # This works for all widgets+ scatter.value++ # Or attributes specifically on the ScatterWidget+ scatter.data_as_pandas+ scatter.data_as_polars```**Attributes.**@@ -93,3 +102,7 @@ class anywidget(UIElement[T, T]):def _convert_value(self, value: T) -> T:return value++ # Proxy all the widget's attributes+ def __getattr__(self, name: str) -> Any:+ return getattr(self.widget, name)diff --git a/tests/_plugins/ui/_impl/test_anywidget.py b/tests/_plugins/ui/_impl/test_anywidget.pyindex 302304c36e..5602fafa88 100644--- a/tests/_plugins/ui/_impl/test_anywidget.py+++ b/tests/_plugins/ui/_impl/test_anywidget.py@@ -60,12 +60,14 @@ base_widget = CounterWidget()are_same = mo.as_html(base_widget).text == mo.as_html(base_widget).textare_different = mo.as_html(CounterWidget()) is not mo.as_html(CounterWidget())as_marimo_element = mo.ui.anywidget(base_widget)+x = as_marimo_element.count""")])assert k.globals["are_same"] is Trueassert k.globals["are_different"] is True+ assert k.globals["x"] == 10assert isinstance(k.globals["base_widget"], _anywidget.AnyWidget)assert "marimo-anywidget" in k.globals["as_marimo_element"].text@@ -101,9 +103,11 @@ as_marimo_element = mo.ui.anywidget(base_widget)exec_req.get("""w_value = w.value+ w_count = w.count"""),])assert isinstance(k.globals["w"], anywidget)assert k.globals["w_value"]["count"] == 10+ assert k.globals["w_count"] == 10