Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

update asynchronous example to dash 1.17.0 #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions dash-asynchronous.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import dash
from dash.dependencies import Input, Output, Event
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
import logging

import datetime
import time


class Semaphore:
def __init__(self, filename='semaphore.txt'):
self.filename = filename
Expand All @@ -24,10 +24,8 @@ def unlock(self):
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')
Expand All @@ -36,36 +34,36 @@ def long_process():
semaphore.unlock()
return datetime.datetime.now()


app = dash.Dash()
server = app.server

app.logger.setLevel(logging.DEBUG)

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'),
dcc.RadioItems(
id='lock',
options=[{'label': i, 'value': i} for i in ['Running...', 'Free']]),
html.Div(id='output')
])


app.layout = layout


@app.callback(
Output('lock', 'children'),
events=[Event('interval', 'interval')])
def display_status():
Output('lock', 'value'),
[Input('interval', 'n_intervals')])
def display_status(interval):
app.logger.debug("display_status")
return 'Running...' if semaphore.is_locked() else 'Free'


@app.callback(
Output('output', 'children'),
events=[Event('button', 'click')])
def run_process():
[Input('button', 'n_clicks')])
def run_process(button_input):
app.logger.debug("run_process")
return 'Finished at {}'.format(long_process())

app.scripts.config.serve_locally = True

if __name__ == '__main__':
app.run_server(debug=True, processes=5)
app.run_server(debug=True)