Plotly Dash - 仅当开关状态改变时触发回调
Plotly Dash - Fire Callback only when switch state changes
我在 Plotly Dash 中遇到以下问题:
我有一个 bool-switch,我通过间隔方法每 10 秒更新一次它的状态,以检查它是否已被后台 运行 的第二个程序更改。
结构(简体)
app.layout(
html.div(id='switch')
)
@app.callback(Input('switch', 'on'),
Output("tracing_status","children"))
def act_when_switch_state_changes():
do_something()
return output
@app.callback(Input('interval-component', 'n_intervals'),
Output('switch','children'))
def check switch_state():
state = read_datebase()
return html.div(daq.BooleanSwitch('id'='switch', on = state))
每次我在间隔组件中更新它的状态时都会触发开关的回调。但是,我只希望它在状态发生变化时触发。由于无状态的设计,我在这里很挣扎。
你有解决我的问题的方法吗?
谢谢!
因此,10 分钟后,回调计数器为间隔组件提供了 60,为开关提供了 59 - 即使我没有更改开关状态。
编辑:
或者,有没有一种方法可以通过 switch.on = True
之类的属性来更改开关状态,而不必 return 并重新加载整个 Div(switch) 元素?
是的,有一种方法可以更改开关而无需每次都重新创建 daq.BooleanSwitch
元素,这是您应该采用的方法。相反,将开关包含在您的基本布局中并直接处理其状态。最小示例:
import random
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import dash_daq as daq
app = dash.Dash(__name__)
app.layout = html.Div([
html.Div(id='text-output'),
daq.BooleanSwitch(id='switch', on=True),
dcc.Interval(id='interval-component', interval=1000),
])
@app.callback(Output('text-output', 'children'),
Input('switch', 'on'))
def act_when_switch_state_changes(switch):
return 'switch is on' if switch else 'switch is off'
@app.callback(Output('switch', 'on'),
Input('interval-component', 'n_intervals'),
State('switch', 'on'),
prevent_initial_call=True)
def update_switch_state(n_intervals, old_state):
new_state = random.choice([True, False]) # coin flip
return dash.no_update if new_state == old_state else new_state
if __name__ == '__main__':
app.run_server(debug=True)
我用随机抛硬币替换了你的 read_database()
以获得新状态,但它当然可以是任何 return 布尔值。
请注意 old_state
如何在 update_switch_state
中使用,以确保开关的值仅在实际更改时更新。这样做可以防止 ('switch', 'on')
作为 Input
的回调在开关未更改时触发。
我在 Plotly Dash 中遇到以下问题: 我有一个 bool-switch,我通过间隔方法每 10 秒更新一次它的状态,以检查它是否已被后台 运行 的第二个程序更改。 结构(简体)
app.layout(
html.div(id='switch')
)
@app.callback(Input('switch', 'on'),
Output("tracing_status","children"))
def act_when_switch_state_changes():
do_something()
return output
@app.callback(Input('interval-component', 'n_intervals'),
Output('switch','children'))
def check switch_state():
state = read_datebase()
return html.div(daq.BooleanSwitch('id'='switch', on = state))
每次我在间隔组件中更新它的状态时都会触发开关的回调。但是,我只希望它在状态发生变化时触发。由于无状态的设计,我在这里很挣扎。
你有解决我的问题的方法吗?
谢谢! 因此,10 分钟后,回调计数器为间隔组件提供了 60,为开关提供了 59 - 即使我没有更改开关状态。
编辑:
或者,有没有一种方法可以通过 switch.on = True
之类的属性来更改开关状态,而不必 return 并重新加载整个 Div(switch) 元素?
是的,有一种方法可以更改开关而无需每次都重新创建 daq.BooleanSwitch
元素,这是您应该采用的方法。相反,将开关包含在您的基本布局中并直接处理其状态。最小示例:
import random
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import dash_daq as daq
app = dash.Dash(__name__)
app.layout = html.Div([
html.Div(id='text-output'),
daq.BooleanSwitch(id='switch', on=True),
dcc.Interval(id='interval-component', interval=1000),
])
@app.callback(Output('text-output', 'children'),
Input('switch', 'on'))
def act_when_switch_state_changes(switch):
return 'switch is on' if switch else 'switch is off'
@app.callback(Output('switch', 'on'),
Input('interval-component', 'n_intervals'),
State('switch', 'on'),
prevent_initial_call=True)
def update_switch_state(n_intervals, old_state):
new_state = random.choice([True, False]) # coin flip
return dash.no_update if new_state == old_state else new_state
if __name__ == '__main__':
app.run_server(debug=True)
我用随机抛硬币替换了你的 read_database()
以获得新状态,但它当然可以是任何 return 布尔值。
请注意 old_state
如何在 update_switch_state
中使用,以确保开关的值仅在实际更改时更新。这样做可以防止 ('switch', 'on')
作为 Input
的回调在开关未更改时触发。