Lasso deselect from inside a function

Hi @Alexboiboi,

You can use the scatter.selectedpoints property to control which points are selected programmatically. Hereโ€™s an example:

import numpy as np
import plotly.graph_objs as go
import ipywidgets as widgets
from plotly.colors import DEFAULT_PLOTLY_COLORS

x = np.linspace(0,6,100)
y = np.sin(x)

data = [go.Scattergl(x=x, y=y, mode='markers', name = 'All nodes',
                     selected=dict(marker=dict(color=DEFAULT_PLOTLY_COLORS[1])))]

layout = dict(title=dict(text = 'plot1'), dragmode='lasso')
fig = go.FigureWidget(data=data, layout= layout)

data2 = [go.Scattergl(x=y, y=x, mode='markers', name = 'All nodes',
                      selected=dict(marker=dict(color=DEFAULT_PLOTLY_COLORS[1])))]

layout2 = dict(title=dict(text = 'plot2'), dragmode='lasso')
fig2 = go.FigureWidget(data=data2, layout= layout2)

def selection_fn(trace, points, selector):
    fig.data[0].selectedpoints = None
    fig.data[0].selectedpoints = []
    fig2.data[0].selectedpoints = points.point_inds 

    
def selection_fn2(trace, points, selector):
    fig2.data[0].selectedpoints = None    
    fig2.data[0].selectedpoints = []
    fig.data[0].selectedpoints = points.point_inds


fig.data[0].on_selection(selection_fn)
fig2.data[0].on_selection(selection_fn2)
display(widgets.HBox([fig,fig2]))

Does this do what you want? Note that I set the selectedpoints property to None and then to []. This was to work around a problem I was seeing where the selectedpoints update would only be processed the first time the callback was called. I think it has something to do with the order in which the selectedpoints property is updated and the when the on_selection callback is called.

Hope that helps!

-Jon