【问题标题】: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我可以为未知数量的按钮列表创建破折号回调吗?即不知道列表将有多少个按钮以及需要多少个回调
【发布时间】:2022-08-19 02:06:32
【问题描述】:

我的 dash 项目包括用户可以自定义警报的功能。这些警报出现在自定义警报列表中,并带有一个删除按钮,使用户能够从列表中删除自定义警报。

[仪表板功能截图]

此列表是使用以下代码创建的:

    @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])]

现在,为了使删除警报功能起作用,我需要为每个删除按钮(使用 id\'s \'delete-alert-button1\',\'delete-alert-button2\',...,\'delete -alert-buttonm\' 用于 m 个警报)。有没有一种方法可以实现这一点,这样无论用户想要发出多少警报,它都可以工作?我的直觉说,也许我可以在循环中创建回调,就像制作按钮一样。就像是:

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

作为一个快速修复,我刚刚创建了一堆回调,所以它涵盖了这么多警报。但是,如果用户创建的警报比我考虑的多,这就会成为一个问题。

    标签: python callback plotly-dash dashboard


    【解决方案1】:

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

    您可以在回调定义上使用模式匹配,这将获取由 ID 标识的任何元素。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-13
      • 1970-01-01
      • 1970-01-01
      • 2012-09-25
      • 2022-01-17
      • 2021-12-01
      • 1970-01-01
      相关资源
      最近更新 更多