Hi Everybody,
I’m loving dash, but there definitely are some nuances to it that has me head-scratching at times. I’m trying to figure out how to break out of a while loop properly based on the state of a BooleanSwitch from the dash-daq library. What I would like to do is enter the while loop when BooleanSwitch is set to True and break out of the while loop while BooleanSwitch is set to false. Instead what I’m observing is that I get successive while loops running in parallel and the only way I can break out of them is Ctrl-C, which kills my app. I believe in a non-Dash environment you could fix this problem by making the state of the BooleanSwitch a global variable, but that isn’t recommended in dash. How would I implement this instead?
Below is some code to illustrate my issue:
boolean_switch_test.py
import dash
import dash_daq as daq
import dash_html_components as html
from dash.dependencies import Input, Output
import time
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
daq.BooleanSwitch(
id='my-boolean-switch',
on=False
),
html.Div(id='dummy-div',style={'display':'none'})
])
def dummy_test(on):
while on:
for i in range(1000):
print(i)
time.sleep(1)
if on==False: #since 'on' is not a global variable, this doesn't work
break
@app.callback(
Output('dummy-div', 'children'),
[Input('my-boolean-switch', 'on')])
def update_output(on):
if on:
dummy_test(on)
return None
if __name__ == '__main__':
app.run_server(debug=True)
Thanks,
Ben
2 posts - 2 participants