1
1
import dash
2
2
import os
3
3
import requests
4
- from flask import request
5
4
import flask .cli
6
- from threading import Thread
7
5
from retrying import retry
8
6
import io
9
7
import re
21
19
from werkzeug .debug .tbtools import get_current_traceback
22
20
23
21
from .comms import _dash_comm , _jupyter_config , _request_jupyter_config
22
+ from ._stoppable_thread import StoppableThread
24
23
25
24
26
25
class JupyterDash (dash .Dash ):
@@ -41,6 +40,8 @@ class JupyterDash(dash.Dash):
41
40
_in_colab = "google.colab" in sys .modules
42
41
_token = str (uuid .uuid4 ())
43
42
43
+ _server_thread = None
44
+
44
45
@classmethod
45
46
def infer_jupyter_proxy_config (cls ):
46
47
"""
@@ -130,15 +131,6 @@ def __init__(self, name=None, server_url=None, **kwargs):
130
131
131
132
self .server_url = server_url
132
133
133
- # Register route to shut down server
134
- @self .server .route ('/_shutdown_' + JupyterDash ._token , methods = ['GET' ])
135
- def shutdown ():
136
- func = request .environ .get ('werkzeug.server.shutdown' )
137
- if func is None :
138
- raise RuntimeError ('Not running with the Werkzeug Server' )
139
- func ()
140
- return 'Server shutting down...'
141
-
142
134
# Register route that we can use to poll to see when server is running
143
135
@self .server .route ('/_alive_' + JupyterDash ._token , methods = ['GET' ])
144
136
def alive ():
@@ -217,7 +209,8 @@ def run_server(
217
209
inline_exceptions = mode == "inline"
218
210
219
211
# Terminate any existing server using this port
220
- self ._terminate_server_for_port (host , port )
212
+ if self ._server_thread :
213
+ self ._server_thread .kill ()
221
214
222
215
# Configure pathname prefix
223
216
requests_pathname_prefix = self .config .get ('requests_pathname_prefix' , None )
@@ -291,9 +284,9 @@ def run_server(
291
284
def run ():
292
285
super_run_server (** kwargs )
293
286
294
- thread = Thread (target = run )
295
- thread .setDaemon (True )
296
- thread .start ()
287
+ self . _server_thread = StoppableThread (target = run )
288
+ self . _server_thread .setDaemon (True )
289
+ self . _server_thread .start ()
297
290
298
291
# Wait for server to start up
299
292
alive_url = "http://{host}:{port}/_alive_{token}" .format (
@@ -412,16 +405,6 @@ def _wrap_errors(_):
412
405
413
406
return html_str , 500
414
407
415
- @classmethod
416
- def _terminate_server_for_port (cls , host , port ):
417
- shutdown_url = "http://{host}:{port}/_shutdown_{token}" .format (
418
- host = host , port = port , token = JupyterDash ._token
419
- )
420
- try :
421
- response = requests .get (shutdown_url )
422
- except Exception as e :
423
- pass
424
-
425
408
426
409
def _custom_formatargvalues (
427
410
args , varargs , varkw , locals ,
0 commit comments