2
2
import os
3
3
import requests
4
4
import flask .cli
5
+ from IPython .core .display import HTML
5
6
from retrying import retry
6
7
import io
7
8
import re
8
9
import sys
9
10
import inspect
10
11
import traceback
11
12
import warnings
13
+ import queue
12
14
13
15
from IPython import get_ipython
14
16
from IPython .display import IFrame , display
@@ -298,6 +300,8 @@ def run(
298
300
except ImportError :
299
301
pass
300
302
303
+ err_q = queue .Queue ()
304
+
301
305
@retry (
302
306
stop_max_attempt_number = 15 ,
303
307
wait_exponential_multiplier = 100 ,
@@ -308,6 +312,9 @@ def run():
308
312
super_run_server (** kwargs )
309
313
except SystemExit :
310
314
pass
315
+ except Exception as error :
316
+ err_q .put (error )
317
+ raise error
311
318
312
319
thread = StoppableThread (target = run )
313
320
thread .setDaemon (True )
@@ -320,31 +327,55 @@ def run():
320
327
host = host , port = port , token = JupyterDash ._token
321
328
)
322
329
330
+ def _get_error ():
331
+ try :
332
+ err = err_q .get_nowait ()
333
+ if err :
334
+ raise err
335
+ except queue .Empty :
336
+ pass
337
+
323
338
# Wait for app to respond to _alive endpoint
324
339
@retry (
325
340
stop_max_attempt_number = 15 ,
326
341
wait_exponential_multiplier = 10 ,
327
342
wait_exponential_max = 1000
328
343
)
329
344
def wait_for_app ():
330
- res = requests .get (alive_url ).content .decode ()
331
- if res != "Alive" :
332
- url = "http://{host}:{port}" .format (
333
- host = host , port = port , token = JupyterDash ._token
334
- )
335
- raise OSError (
336
- "Address '{url}' already in use.\n "
337
- " Try passing a different port to run_server." .format (
338
- url = url
345
+ _get_error ()
346
+ try :
347
+ req = requests .get (alive_url )
348
+ res = req .content .decode ()
349
+ if req .status_code != 200 :
350
+ raise Exception (res )
351
+
352
+ if res != "Alive" :
353
+ url = "http://{host}:{port}" .format (
354
+ host = host , port = port , token = JupyterDash ._token
339
355
)
340
- )
356
+ raise OSError (
357
+ "Address '{url}' already in use.\n "
358
+ " Try passing a different port to run_server." .format (
359
+ url = url
360
+ )
361
+ )
362
+ except requests .ConnectionError as err :
363
+ _get_error ()
364
+ raise err
341
365
342
- wait_for_app ()
366
+ try :
367
+ wait_for_app ()
343
368
344
- if JupyterDash ._in_colab :
345
- self ._display_in_colab (dashboard_url , port , mode , width , height )
346
- else :
347
- self ._display_in_jupyter (dashboard_url , port , mode , width , height )
369
+ if JupyterDash ._in_colab :
370
+ self ._display_in_colab (dashboard_url , port , mode , width , height )
371
+ else :
372
+ self ._display_in_jupyter (dashboard_url , port , mode , width , height )
373
+ except Exception as final_error :
374
+ msg = str (final_error )
375
+ if msg .startswith ('<!' ):
376
+ display (HTML (msg ))
377
+ else :
378
+ raise final_error
348
379
349
380
def _display_in_colab (self , dashboard_url , port , mode , width , height ):
350
381
from google .colab import output
0 commit comments