I’m pretty new to dash/plotly and I’m trying to create a dependent dropdown in dash/plotly, where the selection options in the second dropdown is dependent on the selection in the first dropdown.The first dropdown is pillar and second is manager.
What I would like to see if a pillar is selected the manager options will be for those serving the pilllar only.
What I am getting with the below code is when a pillar is selected all the managers are selected not just those serving the pillar.
A lot of the example I have seen around are with multi=False. My code for both dropdown is multi-true. So far no error just not giving the output I would like.
Thanks in advance for helping.
app = dash.Dash(__name__)
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
all = df.Pillar.unique()
all_1 = df['PRO Manager'].unique()
app.layout=html.Div([
html.H1("PRO Project Management dashboard"),
html.Div([
html.Div([
html.P('Pillar'),
dcc.Dropdown(id='pillar-choice', options=[{'label':x, 'value':x} for x in all] + [{'label':'Select All' , 'value': 'all'}], value=None , multi=True),
],className='six columns'),
html.Div([
html.P('Manager'),
dcc.Dropdown(id='manager-choice', options=[], value=[], multi=True),
], className='six columns'),
], className='row'),
html.Div([
dcc.Graph(id='graph1', style={'display':'inline-block', 'width' :'33%'})
]),
])
@app.callback(
Output(component_id='manager-choice',component_property='options'),
Output(component_id='manager-choice',component_property='value'),
Input(component_id='pillar-choice', component_property='value'),
)
def set_manager_options(choosen_pillar):
dff=df[df.Pillar==choosen_pillar]
manager_list = [{'label': c, 'value': c} for c in all_1]
values_selected = [x['value'] for x in manager_list]
return manager_list, values_selected
@app.callback(
Output(component_id='graph1', component_property='figure'),
Input(component_id='manager-choice',component_property='value'),
Input(component_id='pillar-choice', component_property='value'),
prevent_initial_call=True
)
def update_graph1(manager_selected, pillar_selected):
if pillar_selected == ['all'] :
dff = df
else:
dff =df[(df.Pillar.isin(pillar_selected)) & (df['PRO Manager'].isin(manager_selected))]
fig = px.pie(data_frame=dff, names='Pillar', values='Project No', title='Number of Projects by Pillars')
return fig
if __name__=='__main__':
app.run_server()