Callbacks

Callbacks#

Documentation:
https://dash.plotly.com/basic-callbacks

from dash import Dash, html, Input, Output
import dash_bootstrap_components as dbc

app = Dash( external_stylesheets=[dbc.themes.CERULEAN] )

# put everything into a dbc Container
app.layout = dbc.Container(
    children=[
        html.H1("Best bourbons"),

        # the checklist
        dbc.Checklist(
            # options in the checklist
            options=[
                dict(label="Basil Hayden", value="Basil Hayden"),
                dict(label="Blanton's", value="Blanton's"),
                dict(label="Buffalo Trace", value="Buffalo Trace"),
                dict(label="Eagle Rare", value="Eagle Rare"),
                dict(label="Four Roses", value="Four Roses"),
                dict(label="Knob Creek", value="Knob Creek"),
                dict(label="Michter's", value="Michter's"),
                dict(label="Old Forester", value="Old Forester"),
                dict(label="Old Rip Van Winkle", value="Old Rip Van Winkle"),
                dict(label="Pappy Van Winkle", value="Pappy Van Winkle"),
                dict(label="Woodford Reserve", value="Woodford Reserve"),
                dict(label="1800", value="1800"),
            ],
            # the starting values
            value=["Basil Hayden", "Pappy Van Winkle"],
            # the id of the checklist
            # this will be used to identify the component in the callback
            id="checklist",
        ),

        # the results output
        html.H2('The results'),
        html.P(id='results'),
        html.P(id='drinker'),
        html.P(id='nope'),
    ]
)

# the callback
# one input and three outputs
@app.callback(
    Output('results', 'children'),
    Output('drinker', 'children'),
    Output('nope', 'children'),
    Input('checklist', 'value'),
)
def update_results(bourbon_list):
    # the first string to return
    str_output = ', '.join(bourbon_list)
    str_return_1 = f'The best bourbons are: {str_output}'

    # the second string to return if the person drinks too much
    if len(bourbon_list) > 3:
        str_return_2 = 'You drink too much... 🥃🥴'
    else:
        str_return_2 = ''

    # the third string to return if the person does not know what bourbon is
    if '1800' in bourbon_list:
        str_return_3 = 'That is not a bourbon... 🤬'
    else:
        str_return_3 = ''

    return str_return_1, str_return_2, str_return_3

if __name__ == '__main__':
    app.run_server(debug=True)