Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

Fix dash-asynchronous.py for dash 0.39.0 #27

Description

@ChrisArnault

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')

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 = 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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions