Working in data visualisation, I created a
brushable_widget
python package (available at pypi) so that I can draw an SVG with circles, brush those circles, and get the selected IDs back into marimo (python).
What I really want to do, is build brushing/linking between 2 plots: having 2 plots next to each other showing different views on the same data. If you brush (select) points in the first plot, the points in the second plot are also highlighted; and vice versa.
The API is the following, where
circles1
and
circles2
are arrays of circles based on the same datapoints but different axes.
svg1 = SVG(width=200, height=200, elements=circles1)
svg2 = SVG(width=200, height=200, elements=circles2)
The SVGs are then wrapped in an anywidget and
plot1 = mo.ui.anywidget(BrushableWidget(svg=svg1.as_str(), selected_ids = []))
plot2 = mo.ui.anywidget(BrushableWidget(svg=svg2.as_str(), selected_ids = []))
mo.hstack([plot1, plot2])
When hovering over datapoints, the
selected_ids
trait of that widget is updated. And here's the thing: it should be propagated to the other widget.
I have tried 2 approaches, but each have issues:
Approach 1:
traitlets.link((plot1, "selected_ids"),(plot2, "selected_ids"))
Problem: even though they
are linked, a
plot1.selected_ids
is not updated in other marimo cells.
Approach 2:
get_selected, set_selected = mo.state([])
Problem: you end up with infinite loops:
set_selected(plot1.selected_ids)
,
plot1.selected_ids = get_selected()
,
set_selected(plot2.selected_ids)
and
plot2.selected_ids = get_selected()
will keep triggering each other.
Any ideas on how to get this working? (cc @manzt )