【问题标题】:Python Plotly Dash : How to insert new values to filter after the callback? (Update filter)Python Plotly Dash:如何在回调后插入新值进行过滤? (更新过滤器)
【发布时间】:2021-11-05 13:45:27
【问题描述】:

我的情况是,只有在他登录后,我才能通过 user_id 获得一张表。

例如:

app = dash.Dash(__name__)

#Filter with static values
Month_Selector = dcc.Dropdown(id='month_dropdown',
                                  options=[
                                      {"label": "November 2021", "value": "November 2021"},
                                      {"label": "October 2021", "value": "October 2021"},
                                      {"label": "September 2021", "value": "September 2021"},
                                  ],
                                          placeholder="Select Month",
                                          value = datetime.today().strftime('%B %Y'),
                                          searchable=False
                                          )

app.layout = html.Div(
    children=[
    html.Div(Month_Selector, style={'width': '200px'}),
    dcc.Graph(id='chart')
    ]
)


    @dash_app.callback(
        [
        Output(component_id='chart', component_property='figure')
        ],
        [Input(component_id="submit-button-state", component_property="n_clicks"),
         Input(component_id="month_dropdown", component_property="value")]
    )
    def get_user_name(n_clicks, selected_month):
        
        # Can get a table only after user authorization.
        df= get_table_by_an_authorized_user_id
        
        #filter table by slicer value
        filtered_df= df[ (df['Month Name'] == selected_month)]
        
        # This new values that need to insert to slicer Month_Selector
        new_options_for_month_selector=[{'label': i, 'value': i} for i in df['Month Name'].unique()]
        
        fig = px.bar(filtered_df, x='Month Name', y='Sales')
        return fig
    # Run Local Server
if __name__ == '__main__':
    app.run_server(debug=True, use_reloader=False)    

因此在@callback 之前我只能使用过滤静态值。 如何使用 @callback 后获得的动态值更新或替换过滤器值?

【问题讨论】:

    标签: python callback plotly-dash


    【解决方案1】:

    您想更新下拉列表的options 属性,因此您可以添加带有新选项的输出,并使回调将它们与更新后的图形一起返回:

    @dash_app.callback(
        [Output(component_id='chart', component_property='figure'),
         Output(component_id='month_dropdown', component_property='options')],
        [Input(component_id="submit-button-state", component_property="n_clicks"),
         Input(component_id="month_dropdown", component_property="value")]
    )
    def get_user_name(n_clicks, selected_month):
        
        # Can get a table only after user authorization.
        df= get_table_by_an_authorized_user_id
        
        #filter table by slicer value
        filtered_df= df[ (df['Month Name'] == selected_month)]
        
        # This new values that need to insert to slicer Month_Selector
        new_options_for_month_selector=[{'label': i, 'value': i} for i in df['Month Name'].unique()]
        
        fig = px.bar(filtered_df, x='Month Name', y='Sales')
        return [fig, new_options_for_month_selector]
    

    【讨论】:

      猜你喜欢
      • 2021-02-08
      • 1970-01-01
      • 2020-09-19
      • 2019-09-28
      • 2022-07-13
      • 2019-08-04
      • 2011-01-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多