Shutdown dash app after n seconds

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__':
        time.sleep(20) # pause or 20 seconds

Since this did not work, I made a second attempt per below:

My attempt (based on these links: 1, 2), here is my

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=[
        children='Hello Dash',
            'textAlign': 'center',
            'color': colors['text']
    html.Div(children='Dash: A web application framework for Python.', style={
        'textAlign': 'center',
        'color': colors['text']
            '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')

if __name__ == '__main__':

    @app.route('/shutdown', methods=['POST'])
	def shutdown():
	    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?

app.run_server() runs forever, so in both cases your sys.exit() and shutdown() callback are never executed.

Your approach of having a post command that can be used to shut down the server is a common one, but you need to implement it like this:

@app.server.route('/shutdown', methods=['POST'])
def shutdown():
    return 'Server shutting down...'

if __name__ == '__main__':

But then in another script (or thread or process) you need to actually post to this endpoint. See example of using post here:

Thanks for your feedback! That is helpful.

Ok, I’ve now gone through the link you included. So if I use the default port of 8050, then with this

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


you need to actually post to this endpoint

do you mean I need a separate file with this

import requests
r ='')

How would this file know to use the task shutdown? Also, is there a way to specify a delay of 20 seconds before shutting down - would time.sheep(20) go before shutdown_server()?

You need to point it to the route you specified, e.g.:'')

This would be called from a separate process.