破折号:创建多个回调(带循环?) [英] plotly dash: create multiple callbacks (with loop?)

查看:129
本文介绍了破折号:创建多个回调(带循环?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个带有20个参数的模型,并且为每个参数制作了一个输入组件.

Say I have a model with 20 parameters and I made one input component for each param.

[dcc.Input(type = 'number', id = 'input %i'%i) for i in range(20)]

我想要一个按钮html.Button('populate parameters', id = 'button populate'),该按钮应该为所有输入填充最佳的预拟合值.

I want to have one button html.Button('populate parameters', id = 'button populate') that is supposed to populate best pre-fitted value for all the inputs.

代码如下所示,但无法正常工作.

Code should look like below, except it doesn't work.

for i in range(20):
    @app.callback(
        dash.dependencies.Output('input %i'%i, 'value'),
        [dash.dependencies.Input('button populate', 'n_clicks')]
    )
    def update(ignore):
        return np.random.uniform()

是否必须为具有相同功能的每个输出编写20个回调?我找不到一种可以一口气制作它们的方法(循环吗?)

Do I have to write 20 callbacks for each output with identical functionality? I can't find a way to make them in one go (loop?)

推荐答案

我处理了相同的问题并找到了解决方案.您要做的是绕过装饰器并直接调用app.callback函数:

I have dealt with the same issue and found a solution. What you do is bypass the decorator and call the app.callback function directly:

def update(ignore):
    return np.random.uniform()

for i in range(20):
    app.callback(
        dash.dependencies.Output('input %i' % i, 'value'),
        [dash.dependencies.Input('button populate', 'n_clicks')]
    )(update)

这篇关于破折号:创建多个回调(带循环?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆