Preserve trace visibility between callbacks


I tried the method suggested by @chriddyp in this community post on my scatter plot but the trace visibility (toggled by clicking on the trace legends) doesn’t persist between callbacks. Would passing the specific traces in a callback fix this? If so is there a way I can specifically access the toggled traces in a callback? If not, any tips other tips? Any help would be greatly appreciated.

Thanks In Advance!
Here’s my code
    app.layout = html.Div([
            style={'font-family': 'Product Sans'},
                placeholder='Select the devices you want to display',
            interval=1.5*1000, # in milliseconds

    # Multiple components can update everytime interval gets fired.
    @app.callback(Output('live-update-graph', 'figure'),
                 [Input('device-dropdown', 'value'),
                  Input('interval-component', 'n_intervals')],
                 [State('live-update-graph', 'figure')])
    def update_graph_live(devices, n, previous_figure):
        power, net_traff = get_power_and_net_traff(devices)

        return create_figure_temp(devices, power, net_traff)

Copying from my response in the other thread:

There might be a way to do this by getting the event data from the restyle event (not currently exposed) and passing that data into the callback as State and using it to determine whether or not the legend items have been clicked on or not. I created an issue about exposing the restyle event here:

More generally, I wonder if we should just have some kind of flag that tells the graph whether it should reset user interactions or not (like zooming, panning, clicking on legends, clicking on mode bar items). In some cases, like if you were to switch the chart type or display completely different data, you’d want to reset the user interactions. In other cases, you wouldn’t necessarily want to. I made an issue here: