动态下拉菜单使用多真以短划线方式进行回调

编程入门 行业动态 更新时间:2024-10-25 07:17:46
本文介绍了动态下拉菜单使用多真以短划线方式进行回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对DASH/PLOTLY非常陌生,我正在尝试在DASH/PLOTLY中创建一个从属下拉列表,其中第二个下拉列表中的选择选项取决于第一个下拉列表中的选择。第一个下拉列表是PIELL,第二个是MANAGER。

我希望看到的是,如果选择了柱子,经理选项将自动填充为仅为柱子服务的人员。

我使用下面的代码得到的是,当选择一个支柱时,所有经理都会被选中,而不仅仅是那些为该支柱服务的经理。

提前感谢您的帮助。

app = dash.Dash(__name__) external_stylesheets = ['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) 推荐答案

我将逐行遍历此回调,我想您将看到需要更改的内容。

def set_manager_options(choosen_pillar): dff=df[df.Pillar==choosen_pillar] # 1 manager_list = [{'label': c, 'value': c} for c in all_1] # 2 values_selected = [x['value'] for x in manager_list] # 3 return manager_list, values_selected
  • 这就是您需要的。dff现在具有匹配值
  • 您使用all_1创建列表,但它应该使用dff。您创建了它,但从未使用过它
  • 这是基于错误的列表,因此它的值也是错误的
  • 更多推荐

    动态下拉菜单使用多真以短划线方式进行回调

    本文发布于:2023-08-01 09:15:42,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1267175.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:回调   菜单   方式   动态

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!