简体   繁体   中英

Can I create dash callbacks for a list of unknown number of buttons. I.e don't know how many buttons the list will have and how many callbacks needed

My dash project includes functionality that the user can customise alerts. These alerts appear in a customised alerts list with a delete button that gives the user the ability to delete a customised alert from the list.

[scheenshot of dashboard feature]

1

This list is created with the following code:

    @callback(
        Output('customized-list','children'),
        Input('customized-alerts-button','n_clicks'),
        State('customized_alerts','data'),prevent_initial_call=True)
    def showCustomizedAlerts(_,customized_alerts):
        return [html.H2('Customized alerts', style={"textAlign": "left"}),
                html.Ul([html.Li(html.Div([
                    html.Div([i],style={'display':'inline-block'}),
                    html.Div([html.Button('Delete alert',id='delete-alert-button'+str(customized_alerts.index(i)))],style={'display':'inline-block'})])) for i in customized_alerts])]

Now in order for the delete alert feature to work I need callbacks for every delete button (with id's 'delete-alert-button1','delete-alert-button2',...,'delete-alert-buttonm' for m alerts). Is there a way I can implement this such that this works for however many alerts a user wants to make? My intuition says that maybe I can create callbacks in a loop, in the same way as the button are made. Something like:

@callback(..Input('delete-alert-button'+str(customized_alerts.index(i)),'n_clicks')... for i in customised_alerts

As a quick fix I just created a bunch of callbacks so it covers so many alerts. However, if the user creates more alerts than I accounted for this becomes a problem.

https://dash.plotly.com/pattern-matching-callbacks

You can use pattern matching on the callback definitions, that will pick up any elements that are identified by an ID.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM