Skip to content

Commit 3cd5b99

Browse files
committed
Popuplate initial plotly values selections
Having not found a more elegant solution, we iterate over the figure's data to create the initial values in the marimo wrapper side.
1 parent 368d59d commit 3cd5b99

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

marimo/_plugins/ui/_impl/plotly.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,45 @@ def add_selection(selection: go.layout.Selection) -> None:
142142
hasattr(selection, k) for k in ["x0", "x1", "y0", "y1"]
143143
):
144144
return
145+
145146
initial_value["range"] = {
146147
"x": [selection.x0, selection.x1],
147148
"y": [selection.y0, selection.y1],
148149
}
149150

151+
# Find points within the selection range
152+
selected_points = []
153+
selected_indices = []
154+
155+
x_axes: list[go.layout.XAxis] = []
156+
figure.for_each_xaxis(x_axes.append)
157+
[x_axis] = x_axes if len(x_axes) == 1 else [None]
158+
y_axes: list[go.layout.YAxis] = []
159+
figure.for_each_yaxis(y_axes.append)
160+
[y_axis] = y_axes if len(y_axes) == 1 else [None]
161+
162+
for trace in figure.data:
163+
x_data = getattr(trace, "x", None)
164+
y_data = getattr(trace, "y", None)
165+
if x_data is None or y_data is None:
166+
continue
167+
for point_idx, (x, y) in enumerate(zip(x_data, y_data)):
168+
if (
169+
selection.x0 <= x <= selection.x1
170+
and selection.y0 <= y <= selection.y1
171+
):
172+
selected_points.append(
173+
{
174+
axis.title.text: val
175+
for axis, val in [(x_axis, x), (y_axis, y)]
176+
if axis and axis.title.text
177+
}
178+
)
179+
selected_indices.append(point_idx)
180+
181+
initial_value["points"] = selected_points
182+
initial_value["indices"] = selected_indices
183+
150184
figure.for_each_selection(add_selection)
151185

152186
super().__init__(

0 commit comments

Comments
 (0)