Skip to content

Commit a0c8db6

Browse files
jvddjonasvdd
andauthored
✨ making flask_cors & kaleido optional (#177)
* ✨ making `flask_cors` optional * ♻️ generate poetry.lock * chore: inline_persistent optional dependencies - kaleido & flask-cors * ci: install all extra dependencies * 🙏 * 🔒 lock kaleido to 0.2.1 * docs: optional dependency install instructions --------- Co-authored-by: jonasvdd <[email protected]>
1 parent 80f8a06 commit a0c8db6

File tree

6 files changed

+64
-21
lines changed

6 files changed

+64
-21
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
key: ${{ runner.os }}-poetry-${{ hashFiles('poetry.lock') }}-python-${{ matrix.python-version }}
2828
- run: poetry --version
2929
- name: Install dependencies
30-
run: poetry install
30+
run: poetry install --all-extras
3131
# Do not use caching (anymore)
3232
# if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
3333
- name: Lint

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ jobs:
4343
key: ${{ runner.os }}-poetry-${{ hashFiles('poetry.lock') }}-python-${{ matrix.python-version }}
4444
- run: poetry --version
4545
- name: Install dependencies
46-
run: poetry install
46+
run: poetry install --all-extras
4747
# Do not use caching (anymore)
4848
# if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
4949

CONTRIBUTING.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ We use [`poetry`](https://python-poetry.org/) as dependency manager for this pro
5353

5454
To install the requirements
5555
```sh
56-
pip install poetry # install poetry (if you do use the venv option)
57-
poetry install # install all the dependencies
58-
poetry build # build the underlying C code
56+
pip install poetry # install poetry (if you do use the venv option)
57+
poetry install --all-extras # install all the dependencies
58+
poetry build # build the underlying C code
5959
```
6060

6161
<details>

plotly_resampler/figure_resampler/figure_resampler.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
__author__ = "Jonas Van Der Donckt, Jeroen Van Der Donckt, Emiel Deprost"
1212

1313
import base64
14+
import contextlib
1415
import uuid
1516
import warnings
1617
from typing import List, Tuple
1718

1819
import dash
1920
import plotly.graph_objects as go
20-
from flask_cors import cross_origin
2121
from jupyter_dash import JupyterDash
2222
from plotly.basedatatypes import BaseFigure
2323
from 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``

poetry.lock

Lines changed: 10 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@ include = [
1616
{path = "plotly_resampler/aggregation/algorithms/*.so", format = "wheel"},
1717
{path = "plotly_resampler/aggregation/algorithms/*.pyd", format = "wheel"}
1818
]
19+
classifiers = [
20+
"Development Status :: 5 - Production/Stable",
21+
"Intended Audience :: Developers",
22+
"License :: OSI Approved :: MIT License",
23+
"Programming Language :: Python",
24+
"Programming Language :: Python :: 3",
25+
"Programming Language :: Python :: 3.7",
26+
"Programming Language :: Python :: 3.8",
27+
"Programming Language :: Python :: 3.9",
28+
"Programming Language :: Python :: 3.10",
29+
"Programming Language :: Python :: 3.11",
30+
"Operating System :: POSIX :: Linux",
31+
"Operating System :: MacOS :: MacOS X",
32+
"Operating System :: Microsoft :: Windows",
33+
]
1934
build = "build.py"
2035

2136
[tool.poetry.dependencies]
@@ -29,8 +44,15 @@ numpy = [
2944
{ version = ">=1.14", python = "<3.11" },
3045
{ version = ">=1.24", python = ">=3.11" }
3146
]
32-
Flask-Cors = "^3.0.10"
3347
orjson = "^3.8.0" # Faster json serialization
48+
# Optional dependencies
49+
Flask-Cors = { version = "^3.0.10", optional = true }
50+
# Lock kaleido dependency until https://github.com/plotly/Kaleido/issues/156 is resolved
51+
kaleido = {version = "0.2.1", optional = true}
52+
53+
[tool.poetry.extras]
54+
# Optional dependencies
55+
inline_persistent = ["kaleido", "Flask-Cors"]
3456

3557
[tool.poetry.dev-dependencies]
3658
pytest = "^6.2.5"
@@ -46,10 +68,10 @@ sphinx-autodoc-typehints = "^1.17.0"
4668
ipywidgets = "^7.7.1"
4769
memory-profiler = "^0.60.0"
4870
line-profiler = "^4.0"
49-
kaleido = "0.2.1"
5071
ruff = "^0.0.173"
5172
black = "^22.6.0"
5273
isort = "^5.10.1"
74+
# Include the optional dependencies
5375
# yep = "^0.4" # c code profiling
5476

5577
# Linting

0 commit comments

Comments
 (0)