Show Alert when Button Clicked

I’m trying to show an alert message after a user presses a button. Here is my code and the error I am receiving:

@app.callback(
    [Output(component_id='alert-data-pushed',component_property= 'is_open')],
    Input(component_id = 'submit_data', component_property='n_clicks')
)
def show_alert(n_clicks):
    if n_clicks >= 1:
       return True
    return False

Error (after button clicked):

Expected type: (<class ‘tuple’>, <class ‘list’>)
Received value of type <class ‘bool’>:
True

So it looks like it is returning true… but the property ‘is_open’ doesn’t like that. Any help would be greatly appreciated!

2 Likes

Try removing the brackets around your output component, like so:

@app.callback(
    Output(component_id='alert-data-pushed',component_property= 'is_open'),
    Input(component_id = 'submit_data', component_property='n_clicks')
)
def show_alert(n_clicks, is_open):
    if n_clicks >= 1:
       return True
    return False
1 Like

Ahhh. thank you!!! Edited my post to reflect that I previously deleted the ‘is_open’ input in the def.

Can you possibly explain why removing the brackets works? I’m pretty new to python/dash.

When you include the brackets, it expects you to return a list where the first item of the list corresponds to your first Output item and so on. It’s best to not use a list as it can get quite cumbersome.

Read up more on this topic in the Flexible Callback Signatures section of the official documentation!

2 Likes

Makes sense. Thank you!