Update dcc.Graph trace visibility with callback

Hello, I am trying to emulate the behavior of toggling trace visibility with the legend but through a callback that triggers from the value of a dcc.Dropdown.

I’m able to do it with fig.update_traces(visible = True) or fig.update_traces(visible = “legendonly“) and returning the modified figure but interactions are really slow between 1000 and 2000 ms for scatter lines that are less than 40 indexes long.

I do have a fairly large (~10mb) image in the layout. Disabling it, the times reported in the callback graph are in the same range but it feels snappier.

Is there anyway I can speed it up? I’ve looked into layout.legend.uirevision but I’m not sure I understand how to use that if it’s applicable.

The other thing I considered was doing it as a clientside callback with Plotly.restyle() but I am much more familiar with python than JavaScript.

@app.callback(
    dash.dependencies.Output('graph', 'figure'),
    [dash.dependencies.Input('dropdown', 'value')],
    [dash.dependencies.State('graph', 'figure')
)

def filter_traces(value, figure):
    for item in figure['data']:
			if item['name'] not in value and item['showlegend'] == True:
				item['visible'] = 'legendonly'
			else:
				item['visible'] = True
		figure['layout']['legend']['uirevision']=True
	return figure

is how I’m currently doing it