You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.
I have made asynchronous events work with dash 0.39.0 (where Event has been removed)
Here is the new code for dash-asynchronous.py
Note that this is tested under Winow10 thus I have used multithreaded setup (and single process)
[changes marked with trailing ###]
#################################################
import dash
from dash.dependencies import Input, Output ### removed Event from imports
import dash_core_components as dcc
import dash_html_components as html
import datetime
import time
class Semaphore:
def init(self, filename='semaphore.txt'):
self.filename = filename
with open(self.filename, 'w') as f:
f.write('done')
def lock(self):
with open(self.filename, 'w') as f:
f.write('working')
def unlock(self):
with open(self.filename, 'w') as f:
f.write('done')
def is_locked(self):
return open(self.filename, 'r').read() == 'working'
semaphore = Semaphore()
def long_process():
if semaphore.is_locked():
raise Exception('Resource is locked')
semaphore.lock()
time.sleep(7)
semaphore.unlock()
return datetime.datetime.now()
@app.callback(
Output('lock', 'children'),
[Input('interval', 'n_intervals')]) ### New way to handle events for timers
def display_status(intervals):
return 'Running...' if semaphore.is_locked() else 'Free'
@app.callback(
Output('output', 'children'),
[Input('button', 'n_clicks')]) ### new way to handle events for buttons
def run_process(clicks):
return 'Finished at {}'.format(long_process()) if not clicks is None else '' ### no click at start
if name == 'main':
# app.run_server(debug=True, processes=5, threaded=False)
app.run_server(debug=True, processes=1, threaded=True) ### threaded mode (Windows10)
Hi
I have made asynchronous events work with dash 0.39.0 (where Event has been removed)
Here is the new code for dash-asynchronous.py
Note that this is tested under Winow10 thus I have used multithreaded setup (and single process)
[changes marked with trailing ###]
#################################################
import dash
from dash.dependencies import Input, Output ### removed Event from imports
import dash_core_components as dcc
import dash_html_components as html
import datetime
import time
class Semaphore:
def init(self, filename='semaphore.txt'):
self.filename = filename
with open(self.filename, 'w') as f:
f.write('done')
semaphore = Semaphore()
def long_process():
if semaphore.is_locked():
raise Exception('Resource is locked')
semaphore.lock()
time.sleep(7)
semaphore.unlock()
return datetime.datetime.now()
app = dash.Dash()
server = app.server
def layout():
return html.Div([
html.Button('Run Process', id='button'),
dcc.Interval(id='interval', interval=500),
html.Div(id='lock'),
html.Div(id='output'),
])
app.layout = layout
@app.callback(
Output('lock', 'children'),
[Input('interval', 'n_intervals')]) ### New way to handle events for timers
def display_status(intervals):
return 'Running...' if semaphore.is_locked() else 'Free'
@app.callback(
Output('output', 'children'),
[Input('button', 'n_clicks')]) ### new way to handle events for buttons
def run_process(clicks):
return 'Finished at {}'.format(long_process()) if not clicks is None else '' ### no click at start
if name == 'main':
# app.run_server(debug=True, processes=5, threaded=False)
app.run_server(debug=True, processes=1, threaded=True) ### threaded mode (Windows10)
#################################################
here is my requirements.txt
#################################################
dash==0.39.0
dash-core-components==0.44.0
dash-html-components==0.14.0
dash-renderer==0.15.0
#################################################
Chris Arnault
The text was updated successfully, but these errors were encountered: