【问题标题】:Is there a way to update two properties of a dash component with a callback有没有办法用回调更新破折号组件的两个属性
【发布时间】:2020-10-25 01:34:56
【问题描述】:

我正在用破折号创建一个简单的应用程序。我正在利用 dcc 间隔组件。我想要当开关关闭时,我需要在关闭之前的某个时间间隔,并且我希望应用程序停止更新,然后当触发滑块时,我想打开开关,我想要计时器在它关闭之前的那个时间点开始。

这是我的代码的 sn-p

app.layout = html.Div([

    dcc.Interval(id='interval-test', interval=1000, disabled =False),
    dcc.Interval(id='interval-test2', interval=1000, disabled =False),
    dcc.Loading(
            id="loading-1"),
    
    daq.BooleanSwitch(
        id='control-on',
        on=True,
        color="#9B51E0"),
    
    
    dcc.Slider(
        id='my-slider',
        min=0,
        max=20,
        step=0.5,
        value=10,
    ),

    html.H1(id='test-output1'),
    html.H1(id='test-output2'),
    html.Div(id='intermediate-value', style={'display': 'none'},)

])


@app.callback(Output('intermediate-value', 'children'),
              Output('interval-test', 'disabled'),
              Input('control-on', 'on'),
              Input('interval-test', 'n_intervals'))
def interval_update(switch, n):
    if switch==False:
        print('current n', n)
        return n, True
 

@app.callback(Output('interval-test', 'n_intervals'),
              Output('interval-test', 'disabled'),
              Output('control-on', 'on'),
              Input('intermediate-value', 'children'),
              Input('my-slider', 'value'))
def interval_update(n, slider):
    return n, False, True
    
if __name__ == '__main__':
    app.run_server(debug=True)

不幸的是,我收到以下错误:

Duplicate callback outputs
In the callback for output(s):
  interval-test.n_intervals
  interval-test.disabled
  control-on.on
Output 1 (interval-test.disabled) is already in use.
Any given output can only have one callback that sets it.
To resolve this situation, try combining these into
one callback function, distinguishing the trigger
by using `dash.callback_context` if necessary.

有没有聪明的方法来处理这个问题?

【问题讨论】:

    标签: python python-3.x plotly-dash plotly-python


    【解决方案1】:

    您的输出需要像这样用方括号括起来(实际上您的输入也是如此):

    @app.callback([Output('intermediate-value', 'children'),
                   Output('interval-test', 'disabled')],
                  [Input('control-on', 'on'),
                   Input('interval-test', 'n_intervals')])
    def interval_update(switch, n):
        if switch==False:
            print('current n', n)
            return n, True
     
    
    @app.callback([Output('interval-test', 'n_intervals'),
                   Output('interval-test', 'disabled'),
                   Output('control-on', 'on')],
                  [Input('intermediate-value', 'children'),
                   Input('my-slider', 'value')])
    def interval_update(n, slider):
        return n, False, True
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-21
      • 2021-03-03
      • 1970-01-01
      相关资源
      最近更新 更多