@MicSto wrote:
Hey there,
I am facing a problem with the code snippet in the FAQ which refers to callback contexts (how to check which input has changed, see below).
If I execute this snippet in my own python project and visit localhost to see it there is already the ‘btn-3’ marked under ‘Most recent clicked’ even if I did not clicked it. It has also still the count 0.I tried some other implementation which include the callback_context functionality and it always pretends that something was clicked even if it is not the case.
import json import dash import dash_html_components as html from dash.dependencies import Input, Output app = dash.Dash(__name__) app.layout = html.Div([ html.Button('Button 1', id='btn-1'), html.Button('Button 2', id='btn-2'), html.Button('Button 3', id='btn-3'), html.Div(id='container') ]) @app.callback(Output('container', 'children'), [Input('btn-1', 'n_clicks'), Input('btn-2', 'n_clicks'), Input('btn-3', 'n_clicks')]) def display(btn1, btn2, btn3): ctx = dash.callback_context if not ctx.triggered: button_id = 'No clicks yet' else: button_id = ctx.triggered[0]['prop_id'].split('.')[0] ctx_msg = json.dumps({ 'states': ctx.states, 'triggered': ctx.triggered, 'inputs': ctx.inputs }, indent=2) return html.Div([ html.Table([ html.Tr([html.Th('Button 1'), html.Th('Button 2'), html.Th('Button 3'), html.Th('Most Recent Click')]), html.Tr([html.Td(btn1 or 0), html.Td(btn2 or 0), html.Td(btn3 or 0), html.Td(button_id)]) ]), html.Pre(ctx_msg) ]) if __name__ == '__main__': app.run_server(debug=True)
Posts: 2
Participants: 2