1111__author__ = "Jonas Van Der Donckt, Jeroen Van Der Donckt, Emiel Deprost"
1212
1313import base64
14+ import contextlib
1415import uuid
1516import warnings
1617from typing import List , Tuple
1718
1819import dash
1920import plotly .graph_objects as go
20- from flask_cors import cross_origin
2121from jupyter_dash import JupyterDash
2222from plotly .basedatatypes import BaseFigure
2323from trace_updater import TraceUpdater
@@ -48,18 +48,26 @@ class JupyterDashPersistentInlineOutput(JupyterDash):
4848 the :func:`FigureResampler.show_dash <plotly_resampler.figure_resampler.FigureResampler.show_dash>`
4949 method. However, the mode should be passed as ``"inline"`` since this subclass
5050 overwrites the inline behavior.
51+
52+ .. Note::
53+ This subclass utilizes the optional ``flask_cors`` package to detect whether the
54+ server is alive or not.
55+
5156 """
5257
5358 def __init__ (self , * args , ** kwargs ):
5459 super ().__init__ (* args , ** kwargs )
5560
5661 self ._uid = str (uuid .uuid4 ()) # A new unique id for each app
5762
58- # Mimic the _alive_{token} endpoint but with cors
59- @self .server .route (f"/_is_alive_{ self ._uid } " , methods = ["GET" ])
60- @cross_origin (origin = ["*" ], allow_headers = ["Content-Type" ])
61- def broadcast_alive ():
62- return "Alive"
63+ with contextlib .suppress (ImportWarning , ModuleNotFoundError ):
64+ from flask_cors import cross_origin
65+
66+ # Mimic the _alive_{token} endpoint but with cors
67+ @self .server .route (f"/_is_alive_{ self ._uid } " , methods = ["GET" ])
68+ @cross_origin (origin = ["*" ], allow_headers = ["Content-Type" ])
69+ def broadcast_alive ():
70+ return "Alive"
6371
6472 def _display_inline_output (self , dashboard_url , width , height ):
6573 """Display the dash app persistent inline in the notebook.
@@ -71,6 +79,14 @@ def _display_inline_output(self, dashboard_url, width, height):
7179 # TODO: add option to opt out of this
7280 from IPython .display import display
7381
82+ try :
83+ import flask_cors # noqa: F401
84+ except (ImportError , ModuleNotFoundError ):
85+ warnings .warn (
86+ "'flask_cors' is not installed. The persistent inline output will "
87+ + " not be able to detect whether the server is alive or not."
88+ )
89+
7490 # Get the image from the dashboard and encode it as base64
7591 fig = self .layout .children [0 ].figure # is stored there in the show_dash method
7692 f_width = 1000 if fig .layout .width is None else fig .layout .width
@@ -348,7 +364,9 @@ def show_dash(
348364 environments, browsers, etc.
349365
350366 .. note::
351- This mode requires the ``kaleido`` package.
367+ This mode requires the ``kaleido`` and ``flask_cors`` package.
368+ Install them : ``pip install plotly_resampler[inline_persistent]``
369+ or ``pip install kaleido flask_cors``.
352370
353371 * ``"jupyterlab"``: The app will be displayed in a dedicated tab in the
354372 JupyterLab interface. Requires JupyterLab and the ``jupyterlab-dash``
0 commit comments