Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

Commit 03cafdc

Browse files
committed
Disable development features when not running in ipython context.
This makes it possible to export notebook as a script and run using gunicorn
1 parent 5d74b25 commit 03cafdc

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

jupyter_dash/comms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def _jupyter_comm_response_received():
3333
def _request_jupyter_config(timeout=2):
3434
# Heavily inspired by implementation of CaptureExecution in the
3535
if _dash_comm.kernel is None:
36-
# Not in jupyter server setting
36+
# Not in jupyter setting
3737
return
3838

3939
_send_jupyter_config_comm_request()

jupyter_dash/jupyter_app.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import sys
1111
import inspect
1212

13+
from IPython import get_ipython
1314
from IPython.display import IFrame, display
1415
from IPython.core.ultratb import FormattedTB
1516
from 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

Comments
 (0)