Skip to content

✨ making flask_cors & kaleido optional #177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
key: ${{ runner.os }}-poetry-${{ hashFiles('poetry.lock') }}-python-${{ matrix.python-version }}
- run: poetry --version
- name: Install dependencies
run: poetry install
run: poetry install --all-extras
# Do not use caching (anymore)
# if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
- name: Lint
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
key: ${{ runner.os }}-poetry-${{ hashFiles('poetry.lock') }}-python-${{ matrix.python-version }}
- run: poetry --version
- name: Install dependencies
run: poetry install
run: poetry install --all-extras
# Do not use caching (anymore)
# if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'

Expand Down
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ We use [`poetry`](https://python-poetry.org/) as dependency manager for this pro

To install the requirements
```sh
pip install poetry # install poetry (if you do use the venv option)
poetry install # install all the dependencies
poetry build # build the underlying C code
pip install poetry # install poetry (if you do use the venv option)
poetry install --all-extras # install all the dependencies
poetry build # build the underlying C code
```

<details>
Expand Down
32 changes: 25 additions & 7 deletions plotly_resampler/figure_resampler/figure_resampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
__author__ = "Jonas Van Der Donckt, Jeroen Van Der Donckt, Emiel Deprost"

import base64
import contextlib
import uuid
import warnings
from typing import List, Tuple

import dash
import plotly.graph_objects as go
from flask_cors import cross_origin
from jupyter_dash import JupyterDash
from plotly.basedatatypes import BaseFigure
from trace_updater import TraceUpdater
Expand Down Expand Up @@ -48,18 +48,26 @@ class JupyterDashPersistentInlineOutput(JupyterDash):
the :func:`FigureResampler.show_dash <plotly_resampler.figure_resampler.FigureResampler.show_dash>`
method. However, the mode should be passed as ``"inline"`` since this subclass
overwrites the inline behavior.

.. Note::
This subclass utilizes the optional ``flask_cors`` package to detect whether the
server is alive or not.

"""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self._uid = str(uuid.uuid4()) # A new unique id for each app

# Mimic the _alive_{token} endpoint but with cors
@self.server.route(f"/_is_alive_{self._uid}", methods=["GET"])
@cross_origin(origin=["*"], allow_headers=["Content-Type"])
def broadcast_alive():
return "Alive"
with contextlib.suppress(ImportWarning, ModuleNotFoundError):
from flask_cors import cross_origin

# Mimic the _alive_{token} endpoint but with cors
@self.server.route(f"/_is_alive_{self._uid}", methods=["GET"])
@cross_origin(origin=["*"], allow_headers=["Content-Type"])
def broadcast_alive():
return "Alive"

def _display_inline_output(self, dashboard_url, width, height):
"""Display the dash app persistent inline in the notebook.
Expand All @@ -71,6 +79,14 @@ def _display_inline_output(self, dashboard_url, width, height):
# TODO: add option to opt out of this
from IPython.display import display

try:
import flask_cors # noqa: F401
except (ImportError, ModuleNotFoundError):
warnings.warn(
"'flask_cors' is not installed. The persistent inline output will "
+ " not be able to detect whether the server is alive or not."
)

# Get the image from the dashboard and encode it as base64
fig = self.layout.children[0].figure # is stored there in the show_dash method
f_width = 1000 if fig.layout.width is None else fig.layout.width
Expand Down Expand Up @@ -348,7 +364,9 @@ def show_dash(
environments, browsers, etc.

.. note::
This mode requires the ``kaleido`` package.
This mode requires the ``kaleido`` and ``flask_cors`` package.
Install them : ``pip install plotly_resampler[inline_persistent]``
or ``pip install kaleido flask_cors``.

* ``"jupyterlab"``: The app will be displayed in a dedicated tab in the
JupyterLab interface. Requires JupyterLab and the ``jupyterlab-dash``
Expand Down
17 changes: 10 additions & 7 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 24 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ include = [
{path = "plotly_resampler/aggregation/algorithms/*.so", format = "wheel"},
{path = "plotly_resampler/aggregation/algorithms/*.pyd", format = "wheel"}
]
classifiers = [
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Operating System :: POSIX :: Linux",
"Operating System :: MacOS :: MacOS X",
"Operating System :: Microsoft :: Windows",
]
build = "build.py"

[tool.poetry.dependencies]
Expand All @@ -29,8 +44,15 @@ numpy = [
{ version = ">=1.14", python = "<3.11" },
{ version = ">=1.24", python = ">=3.11" }
]
Flask-Cors = "^3.0.10"
orjson = "^3.8.0" # Faster json serialization
# Optional dependencies
Flask-Cors = { version = "^3.0.10", optional = true }
# Lock kaleido dependency until https://github.com/plotly/Kaleido/issues/156 is resolved
kaleido = {version = "0.2.1", optional = true}

[tool.poetry.extras]
# Optional dependencies
inline_persistent = ["kaleido", "Flask-Cors"]

[tool.poetry.dev-dependencies]
pytest = "^6.2.5"
Expand All @@ -46,10 +68,10 @@ sphinx-autodoc-typehints = "^1.17.0"
ipywidgets = "^7.7.1"
memory-profiler = "^0.60.0"
line-profiler = "^4.0"
kaleido = "0.2.1"
ruff = "^0.0.173"
black = "^22.6.0"
isort = "^5.10.1"
# Include the optional dependencies
# yep = "^0.4" # c code profiling

# Linting
Expand Down