I have been developing my first dash app over the past several days and have been blown away by the flexibility and functionality! One issue I am currently running into is how to access the trace name from clickData. I have searched the forum and attempted to implement the solution outlined here. However, I am receiving the following error:
AttributeError: 'Graph' object has no attribute 'figure'
I believe that the issue I am running into is I am returning the figure attribute through a callback. See below for the exact implementation:
pertinent app layout:
html.Div(
className = 'six columns',
style = {'border': '1px solid red'},
children = [
dcc.Graph(
id = 'source_count_bar'
)
]
)
callbacks:
@app.callback(
dash.dependencies.Output('sentiment_graph', 'figure'),
[dash.dependencies.Input('body_title_radio', 'value'),
dash.dependencies.Input('week_month_radio', 'value'),
dash.dependencies.Input('trump_clinton_checklist', 'values')])
def update_sentiment(body_title, week_month, trump_clinton):
if body_title == 'body':
sentiment_data = article_sentiment_dict
else:
sentiment_data = title_sentiment_dict
traces = []
for i in trump_clinton:
traces.append(go.Scatter(
x = list(sentiment_data[i][week_month].keys()),
y = list(sentiment_data[i][week_month].values()),
mode = 'lines+markers',
name = i)
)
return {
'data': traces,
'layout': {
'height': 400,
'margin': {
'l':40,
'b':80,
't':20,
'r':0
},
'clickmode': 'event+select'
}
}
@app.callback(
dash.dependencies.Output('word_count_bar', 'figure'),
[dash.dependencies.Input('body_title_radio', 'value'),
dash.dependencies.Input('week_month_radio', 'value'),
dash.dependencies.Input('sentiment_graph', 'clickData')])
def update_word_count(body_title, week_month, clickData):
if body_title == 'body':
word_count_data = article_wordcount_dict
else:
word_count_data = title_wordcount_dict
curve_number = clickData['points'][0]['curveNumber']
trace_name = app.layout['sentiment_graph'].figure['data'][curve_number]['name']
date = str(clickData['points'][0]['x'])
print(date, trace_name)
bars = [go.Bar(
x = list(word_count_data[trace_name][week_month][date].keys()),
y = list(word_count_data[trace_name][week_month][date].values())
)]
return {
'data': bars
}