How to shutdown a JupyterDash app in external mode?

Hi,

I’m running app.run_server(debug=True, mode='external') from a jupyter notebook and my app runs on http://127.0.0.1:8050/.

Is there a way to shutdown the server without restarting the jupyter kernel?

(So that the address is free for another app, for example, to run from the command line - which currently produces OSError: [Errno 98] Address already in use.)

I tried to run requests.post('http://127.0.0.1:8050/shutdown') which I found here - Shutdown dash app after n seconds - #3 by W_R, but I get <Response [500]>.

Thanks, JupyterDash is a great addition by the way!

Bumping this to hopefully get an answer?

Seems like I found the answer myself perusing the source code at https://github.com/plotly/jupyter-dash/blob/master/jupyter_dash/jupyter_app.py:

JupyterDash has a classmethod called _terminate_server_for_port:

@classmethod
    def _terminate_server_for_port(cls, host, port):
        shutdown_url = "http://{host}:{port}/_shutdown_{token}".format(
            host=host, port=port, token=JupyterDash._token
        )
        try:
            response = requests.get(shutdown_url)
        except Exception as e:
            pass

So if you start a JupyterDash server like:

from jupyter_dash import JupyterDash

app = JupyterDash()
app.layout. = html.Div()
app.run_server(mode='external', port=8060)

You can then run app._terminate_server_for_port("localhost", 8060) to kill the server.

Mind you this is a private method, so guess we’re not supposed to use it, but when in need…

2 Likes

Works perfectly, thanks a lot!

Doesn’t work anymore… :frowning:

1 Like