【发布时间】:2021-10-08 20:45:39
【问题描述】:
我对 dash/plotly 很陌生,我正在尝试在 dash/plotly 中创建一个依赖下拉列表,其中第二个下拉列表中的选择选项取决于第一个下拉列表中的选择。第一个下拉列表是支柱第二个是经理。
我希望看到的是,如果选择了一个支柱,则将为仅服务于该支柱的人自动填充经理选项。
我从下面的代码中得到的是,当一个支柱被选中时,所有的经理都会被选中,而不仅仅是那些为支柱服务的经理。
提前感谢您的帮助。
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.isin(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')
)
def update_graph1(manager_selected, pillar_selected):
if ((len(manager_selected) !=0) and (len(pillar_selected) ==0)):
dff =df[(df['PRO Manager'].isin(manager_selected))]
elif ((len(manager_selected) ==0) and (len(pillar_selected) !=0)):
dff =df[(df.Pillar.isin(pillar_selected))]
elif ((manager_selected is None ) and (pillar_selected is None)):
return dash.no_update
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(debug=False)
【问题讨论】:
标签: plotly-dash