动态下拉列表以多重真实的方式绘制破折号回调

Dynamic dropdowns plotly dash callback with multi true

我是 dash/plotly 的新手,我正在尝试在 dash/plotly 中创建一个依赖下拉列表,其中第二个下拉列表中的选择选项取决于第一个 dropdown.The 第一个下拉菜单是支柱,第二个是经理。

我希望看到的是,如果选择了一个支柱,经理选项将自动填充给那些只为支柱服务的人。

我从以下代码中获得的是选择支柱时,所有经理都被选中了所有的经理,而不仅仅是为支柱服务的经理。

在此先感谢您的帮助。

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. 这是基于错误的列表,因此它也有错误的值