【问题标题】:Execute a callback if the value in the element changes如果元素中的值发生变化,则执行回调
【发布时间】:2018-08-28 09:22:43
【问题描述】:

假设我正在对几个因变量(d1d2d3)进行线性回归,以预测数据流中的一个自变量(id)。

app = dash.Dash()

app.layout = html.Div([
    dcc.Interval(id='data-stream', interval=1000, n_intervals=0),
    html.Div(id='count'),
    html.Div(id='betas'),
])

@app.callback(
    Output('count', 'children'),
    [Input('data-stream', 'n_intervals')]
)
def get_count(data):
    df = pd.read_csv(*some url that updates and returns a csv*)
    count = len(df['id'])
    return html.H2(f'The count is {count}')

@app.callback(
    Output('betas', 'children'),
    [*what goes in here?*]
)
def run_regression(*arg_inputs):
    # Run Regression here that outputs a table of betas

当计数值改变时我如何进行回调(运行回归),即html.Div(id='count'),而不是依赖于间隔???

我查看了Event 的文档,甚至尝试将Input('count', 'children') 作为inputs 参数作为run_regression 函数的回调装饰器的参数,并且它仍然流式传输而不是重新运行回归观察次数变化。

【问题讨论】:

    标签: python plotly-dash


    【解决方案1】:

    你的输入应该是count,你可以使用类似的东西

    @app.callback(
        Output('betas', 'children'),
        Input('count','value')
    )
    def run_regression(count_value):
        # Run Regression here that outputs a table of betas
    

    在这里,您的输入就像您提到了 count 的长度,因此您可以使用它来做您想做的事情(确保您可以在函数 run_regression 中添加 print 以打印 count_value 以查看它的样子) 如果你需要更多帮助,你应该解释你的 run_regression 函数应该做什么;)

    【讨论】:

      猜你喜欢
      • 2022-01-20
      • 2011-07-05
      • 2021-06-18
      • 2023-01-31
      • 1970-01-01
      • 2015-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多