dash
dash copied to clipboard
[BUG] Incorrect code example for callback_context
The code example in the documentation section titled "Properties for callback_context" (https://dash.plotly.com/advanced-callbacks#properties-for-callback_context) has a bug in the line
button_id = ctx.triggered_id if not None else 'No clicks yet'
Of course not None will always evaluate to True. The line should instead say
button_id = ctx.triggered_id if ctx.triggered_id is not None else 'No clicks yet'
or a similar variant.
Full block code copied below for reference.
import json
from dash import Dash, html, Input, Output, callback, ctx
app = Dash()
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')
])
@callback(Output('container', 'children'),
Input('btn-1', 'n_clicks'),
Input('btn-2', 'n_clicks'),
Input('btn-3', 'n_clicks'))
def display(btn1, btn2, btn3):
button_id = ctx.triggered_id if not None else 'No clicks yet'
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(debug=True)
cc @LiamConnors
Thanks for reporting this @carlosles. It will be updated in the live docs shortly.