动态下拉菜单使用多真以短划线方式进行回调 [英] Dynamic dropdowns plotly dash callback with multi true

查看:14
本文介绍了动态下拉菜单使用多真以短划线方式进行回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

提前感谢您的帮助。

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)

推荐答案

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

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
  1. 这就是您需要的。dff现在具有匹配值
  2. 您使用all_1创建列表,但它应该使用dff。您创建了它,但从未使用过它
  3. 这是基于错误的列表,因此它的值也是错误的

这篇关于动态下拉菜单使用多真以短划线方式进行回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆