Hi I am trying to run a dash app and then shut it down after 20 seconds. Here is my basic app per a datacamp beginner’s blog post/tutorial.
What I have in mind (this is just a basic workflow to show the behavior that I want)
if __name__ == '__main__':
app.run_server(debug=True)
time.sleep(20) # pause or 20 seconds
sys.exit()
Since this did not work, I made a second attempt per below:
My attempt (based on these links: 1, 2), here is my app.py
# app.py
import dash
import dash_core_components as dcc
import dash_html_components as html
app = dash.Dash()
colors = {
'background': '#111111',
'text': '#7FDBFF'
}
app.layout = html.Div(style={'backgroundColor': colors['background']}, children=[
html.H1(
children='Hello Dash',
style={
'textAlign': 'center',
'color': colors['text']
}
),
html.Div(children='Dash: A web application framework for Python.', style={
'textAlign': 'center',
'color': colors['text']
}),
dcc.Graph(
id='Graph1',
figure={
'data': [
{'x': [1, 2, 3], 'y': [4, 1, 2], 'type': 'bar', 'name': 'SF'},
{'x': [1, 2, 3], 'y': [2, 4, 5], 'type': 'bar', 'name': u'Montréal'},
],
'layout': {
'plot_bgcolor': colors['background'],
'paper_bgcolor': colors['background'],
'font': {
'color': colors['text']
}
}
}
)
])
from flask import request
def shutdown_server():
func = request.environ.get('werkzeug.server.shutdown')
if func is None:
raise RuntimeError('Not running with the Werkzeug Server')
func()
if __name__ == '__main__':
app.run_server(debug=True)
@app.route('/shutdown', methods=['POST'])
def shutdown():
shutdown_server()
return 'Server shutting down...'
But, this doesn’t work either. The app
runs fine. But it does not shutdown…it just stays running. Based on the 2 links I used, I thought this code would start the app and immediately shut it down. But this did not happen…I am likely misunderstanding something though.
Is there a way to (programmatically) shut down a dash app after 20 seconds?