SolaraS
Solara2y ago
2 replies
artursp.

Using existing plotly widget to update figures

With go.FigureWidget it is possible to update the data/properties of a figure without redrawing it.
E.g. I can create a scatter plot with a lot of points:
N = 1000000
fig = go.FigureWidget(
    data=go.Scattergl(
        x = np.random.randn(N),
        y = np.random.randn(N),     
        mode='markers',
        selected=go.scattergl.Selected(marker = {"color":"red", "size":25}),
        selectedpoints=[5,10,25]
    )
)

and then update the highlighted points without redrawing it the whole thing:
scatter = fig.data[0]
scatter.selectedpoints = np.random.randint(0,N,size=50)


The straight forward solara equivalent could look like this:

N = 1000000
selected = solara.reactive([5,10,25])

x = np.random.randn(N)
y = np.random.randn(N)

def rand_sel():
    selected.set(np.random.randint(0,N,size=50))

@solara.component
def Page():
    fig = go.FigureWidget(
        data=go.Scattergl(
            x = x,
            y = y,     
            mode='markers',
            selected=go.scattergl.Selected(marker = {"color":"red", "size":25}),
            selectedpoints=selected.value
        )
    )
    
    solara.FigurePlotly(fig)
    solara.Button('Random select', on_click=rand_sel)
    
Page()


but this will generally run slower as the figure is redrawn IIUC. Is there a way to update the figure using the plotly widget within solara?
Was this page helpful?