[Python] Dash, how to callback depending on which button is being clicked?

Discussão em 'Python' iniciado por Stack, Fevereiro 19, 2025.

  1. Stack

    Stack Membro Participativo

    I have a function that dynamically creates a set of buttons:

    def generate_team_button(team_shortName):
    return dbc.Button(
    str(team_shortName),
    className="btn btn-primary",
    id=str(team_shortName),
    style={
    "margin-right": "10px",
    "margin-bottom": '10px',
    },
    n_clicks=0,

    )


    These buttons are displayed by looping through a set of items

    import dash
    import dash_core_components as dcc
    import dash_html_components as html

    import dash_bootstrap_components as dbc

    from dash.dependencies import Input
    from dash.dependencies import Output
    from dash.dependencies import State

    import pandas as pd

    data = {'teams': ['team1', 'team2', 'team3', 'team4']}
    df_teams = pd.DataFrame(data)

    app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])

    app.layout = html.Div(
    [

    dbc.Row([
    dbc.Col(
    children=[generate_team_button(i) for i in df_teams['teams']]
    ),

    ]),

    dbc.Row([
    dbc.Col(
    id='section',
    #WANT TO UPDATE THIS SECTION BASED ON WHICH BUTTON IS BEEING CLICKED


    ),

    ]),
    ]
    ),


    What I want to do is to update id=section based on which button is being pressed. To do that I need a @app.callback like below. But what I get is a tuple with how many times each button has been pressed.

    @app.callback(
    [
    Output('league-table', 'value'),
    ],
    [
    Input(str(i), 'n_clicks') for i in df_teams['teams']
    ]

    )

    def update_league_table(*args):
    print(args)
    return f'{args}'



    How can I update a section based on which button has been clicked?

    Continue reading...

Compartilhe esta Página