1010import sys
1111import inspect
1212
13+ from IPython import get_ipython
1314from IPython .display import IFrame , display
1415from IPython .core .ultratb import FormattedTB
1516from ansi2html import Ansi2HTMLConverter
@@ -32,6 +33,7 @@ class JupyterDash(dash.Dash):
3233 default_mode = 'external'
3334 default_requests_pathname_prefix = None
3435 default_server_url = None
36+ _in_ipython = get_ipython () is not None
3537
3638 @classmethod
3739 def infer_jupyter_config (cls ):
@@ -57,10 +59,21 @@ def infer_jupyter_config(cls):
5759 see what JupyterLab extensions are installed by running the following command:
5860 $ jupyter labextension list
5961 """
62+ if not JupyterDash ._in_ipython :
63+ # No op when not running in a Jupyter context
64+ return
65+
6066 _request_jupyter_config ()
6167
6268 def __init__ (self , name = None , server_url = None , ** kwargs ):
6369 """"""
70+ # Call superclass constructor
71+ super (JupyterDash , self ).__init__ (name = name , ** kwargs )
72+
73+ if not JupyterDash ._in_ipython :
74+ # Nothing else to do when not running in a Jupyter context
75+ return
76+
6477 # See if jupyter_server_proxy is installed
6578 try :
6679 import jupyter_server_proxy
@@ -82,9 +95,6 @@ def __init__(self, name=None, server_url=None, **kwargs):
8295
8396 self ._input_pathname_prefix = kwargs .get ('requests_pathname_prefix' , None )
8497
85- # Call superclass constructor
86- super (JupyterDash , self ).__init__ (name = name , ** kwargs )
87-
8898 # Infer server_url
8999 if server_url is None :
90100 domain_base = os .environ .get ('PLOTLY_DASH_DOMAIN_BASE' , None )
@@ -136,6 +146,10 @@ def run_server(
136146 :param kwargs: Additional keyword arguments to pass to the superclass
137147 ``Dash.run_server`` method.
138148 """
149+ if not JupyterDash ._in_ipython :
150+ # No op else to do when not running in a Jupyter context
151+ return
152+
139153 # Get host and port
140154 host = kwargs .get ("host" , os .getenv ("HOST" , "127.0.0.1" ))
141155 port = kwargs .get ("port" , os .getenv ("PORT" , "8050" ))
@@ -301,7 +315,7 @@ def _wrap_errors(_):
301315 # Customized formatargvalues function so we can place function parameters
302316 # on separate lines
303317 original_formatargvalues = inspect .formatargvalues
304- inspect .formatargvalues = custom_formatargvalues
318+ inspect .formatargvalues = _custom_formatargvalues
305319 try :
306320 # Use IPython traceback formatting to build colored ANSI traceback
307321 # string
@@ -350,7 +364,7 @@ def _terminate_server_for_port(cls, host, port):
350364 pass
351365
352366
353- def custom_formatargvalues (
367+ def _custom_formatargvalues (
354368 args , varargs , varkw , locals ,
355369 formatarg = str ,
356370 formatvarargs = lambda name : '*' + name ,
0 commit comments