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__':
        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?

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():
    shutdown_server()
    return 'Server shutting down...'

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

But then in another script (or thread or process) you need to actually post to this endpoint. See example of using post here: http://docs.python-requests.org/en/master/user/quickstart/#make-a-request

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

by

you need to actually post to this endpoint

do you mean I need a separate file app_helper.py with this

import requests
r = requests.post('http://127.0.0.1:8050')

How would this app_helper.py 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.:

requests.post('http://127.0.0.1:8050/shutdown')

This would be called from a separate process.