I am currently building a dashboard that includes multiple graphs.
The idea is that each graph in the dashboard displays the data for the same country.
For that I want to have a dropdown at the top that selects the country and then all graphs update accordingly.
Is that possible? So far it seems like the regular dropdown function in plotly can only be linked to a single graph.
Are Dropdown Widgets the answer here or will I have to go with a completely different solution to Plotly/Dash?
@radekwlsk
Hello There !
I was trying to use your solution since I wanted to do the same thing.
It is working, but I was wondering if I was using the repeated callbacks correctly ?
Is there a way to have a more simple code ?
Thank you for your help.
Below is my code :
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.graph_objs as go
from plotly import tools
import pandas as pd
df = pd.read_excel('data/test-anon-3IPP.xlsx', index_col='IPP')
app = dash.Dash()
# Added for heroku
server = app.server
# Variable for dropdown
name_options = []
for Nom in df['Nom'].unique():
name_options.append({'label':Nom,'value':Nom})
app.layout = html.Div([
# Div for Dropdown
html.Div([
dcc.Dropdown(id='name_picker', options=name_options)
],style={'width':'40%'}),
# Div for graph1
html.Div([
dcc.Graph(id='graph1')
],style={'width':'70%'}),
# Div for graph2
html.Div([
dcc.Graph(id='graph2')
],style={'width':'70%'}),
])
@app.callback(Output('graph1','figure'),
[Input('name_picker','value')])
def update_figure(selected_name):
# data only for the selected name from the dropdown menu
filtered_df = df[df['Nom']== selected_name]
traces = []
for patient_name in filtered_df['Nom'].unique():
df_by_name = filtered_df[filtered_df['Nom']==patient_name]
# Platelets
traces.append(go.Bar(x = df_by_name['Date de prélèvement'],
y = df_by_name['Valeur de résultat du Plaquettes'],
name = 'Plaquettes',
marker=dict(color='#d87224'))
)
return {'data': traces,
'layout' :go.Layout(title='Plaquettes')
}
########## Repeated code
@app.callback(Output('graph2','figure'),
[Input('name_picker','value')])
def update_figure(selected_name):
# data only for the selected name from the dropdown menu
filtered_df = df[df['Nom']== selected_name]
traces = []
for patient_name in filtered_df['Nom'].unique():
df_by_name = filtered_df[filtered_df['Nom']==patient_name]
# Monocytes
traces.append(go.Bar(x = df_by_name['Date de prélèvement'],
y = df_by_name['Valeur de résultat du Monocytes'],
name = 'Monocytes',
marker=dict(color='red'))
)
return {'data': traces,
'layout' :go.Layout(title='Monocytes')
}
##########
# Append an externally hosted CSS stylesheet
my_css_url = "https://codepen.io/chriddyp/pen/bWLwgP.css"
app.css.append_css({
"external_url": my_css_url
})
if __name__ == '__main__':
app.run_server(debug=True)
traces = []
for patient_name in filtered_df['Nom'].unique():
df_by_name = filtered_df[filtered_df['Nom']==patient_name]
traces.append(go.Scatter(x = df_by_name['Date de prélèvement'],
xaxis={'type': 'log'},
y = df_by_name['Valeur de résultat du {}'.format(trace_name)],
name = trace_name,
marker=dict(color=trace_color))
)
return traces