Sharing in case anyone is still looking for an answer: I ended up changing the callback so that it’s still activated on restyleData, but grab the required components from ‘figure’ instead.
Note: this will not run on the first load of the page, but will show names of active traces after the first click on the legend:
import json
from textwrap import dedent as d
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
styles = {
'pre': {
'border': 'thin lightgrey solid',
'overflowX': 'scroll'
}
}
app.layout = html.Div([
dcc.Graph(
id='basic-interactions',
config={'editable':True},
figure={
'data': [
{
'x': [1, 2, 3, 4],
'y': [4, 1, 3, 5],
'text': ['a', 'b', 'c', 'd'],
'customdata': ['c.a', 'c.b', 'c.c', 'c.d'],
'name': 'Trace 1',
'mode': 'markers',
'marker': {'size': 12}
},
{
'x': [1, 2, 3, 4],
'y': [9, 4, 1, 4],
'text': ['w', 'x', 'y', 'z'],
'customdata': ['c.w', 'c.x', 'c.y', 'c.z'],
'name': 'Trace 2',
'mode': 'markers',
'marker': {'size': 12}
}
],
'layout': {
'clickmode': 'event+select'
}
}
),
html.Div(className='row', children=[
html.Div([
html.Pre(id='click-data', style=styles['pre']),
], className='three columns'),
])
])
@app.callback(
Output('click-data', 'children'),
Input('basic-interactions', 'restyleData'),
State('basic-interactions', 'figure')
)
def display_restyle_data(restyleData, figure):
# return json.dumps(figure, indent=2)
return [x['name'] for x in figure['data'] if 'visible' in x.keys() and x['visible'] == True]
if __name__ == '__main__':
app.run_server(debug=True)