22import os
33import requests
44import flask .cli
5+ from IPython .core .display import HTML
56from retrying import retry
67import io
78import re
89import sys
910import inspect
1011import traceback
1112import warnings
13+ import queue
1214
1315from IPython import get_ipython
1416from IPython .display import IFrame , display
@@ -298,6 +300,8 @@ def run(
298300 except ImportError :
299301 pass
300302
303+ err_q = queue .Queue ()
304+
301305 @retry (
302306 stop_max_attempt_number = 15 ,
303307 wait_exponential_multiplier = 100 ,
@@ -308,6 +312,9 @@ def run():
308312 super_run_server (** kwargs )
309313 except SystemExit :
310314 pass
315+ except Exception as error :
316+ err_q .put (error )
317+ raise error
311318
312319 thread = StoppableThread (target = run )
313320 thread .setDaemon (True )
@@ -320,31 +327,55 @@ def run():
320327 host = host , port = port , token = JupyterDash ._token
321328 )
322329
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+
323338 # Wait for app to respond to _alive endpoint
324339 @retry (
325340 stop_max_attempt_number = 15 ,
326341 wait_exponential_multiplier = 10 ,
327342 wait_exponential_max = 1000
328343 )
329344 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
339355 )
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
341365
342- wait_for_app ()
366+ try :
367+ wait_for_app ()
343368
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
348379
349380 def _display_in_colab (self , dashboard_url , port , mode , width , height ):
350381 from google .colab import output
0 commit comments