Quantcast
Channel: 🎛️ Dash - Plotly Community Forum
Viewing all articles
Browse latest Browse all 6271

Output different value based on condition with the help of dash.callback_context

$
0
0

Hello forum

I am trying to output a different value based on which input has fired.
For example when a button gets pressed I want to output a number and when another input got pressed like dropdown or radio button I want just output zero and to reset both buttons to zero.

html.Div(
id='buttons-output',
style={'display': 'none'}),

@app.callback(
    [Output(component_id='buttons-output', component_property='children'),],
    [Input(component_id='button-left', component_property='n_clicks'),
     Input(component_id='button-right', component_property='n_clicks'),
     Input(component_id='product-dropdown', component_property='value'),
     Input(component_id='radio-buttons', component_property='value')],
)
def update_buttons_output(left_button, right_button, dropdown_output, radio_output):
    ctx = dash.callback_context
    context_id = ctx.triggered[0]['prop_id'].split('.')[0]
    if context_id == 'button-left' or 'button-right':
        return [left_button - right_button]        
    else:
        return [0]

@app.callback(
    [Output(component_id='button-left', component_property='n_clicks'),
     Output(component_id='button-right', component_property='n_clicks')],
     Input(component_id='buttons-output', component_property='children')
)
def update_buttons(buttons_output):
    return buttons_output, 0

But this results in a circular dependency and in an infinite loop…
What’s the way to do this?

3 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 6271

Trending Articles