【问题标题】:Dynamic dropdowns plotly dash callback with multi true动态下拉列表用多 true 破折号回调
【发布时间】: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


    【解决方案1】:

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

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

    【讨论】:

    • 知道了!不敢相信我错过了。我使用了 dff['PRO Manager'].unique() 。非常感谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    • 1970-01-01
    • 2022-07-01
    • 1970-01-01
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多