Skip to content
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

Support Kaleido v1 in Plotly.py #5062

Open
wants to merge 38 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
f0a78a6
small io refactor to support kaleido v1
emilykl Mar 4, 2025
a681c84
update kaleido tests to support kaleido v1
emilykl Mar 4, 2025
a7a6f24
Merge branch 'main' into upgrade-kaleido
emilykl Mar 4, 2025
2e9c8af
update test requirements
emilykl Mar 4, 2025
60bb748
simplify to_image
emilykl Mar 4, 2025
b87f752
use k.calc_fig() instead of k.write_fig() in pio.full_figure_for_deve…
emilykl Mar 6, 2025
e203623
merge main
emilykl Mar 7, 2025
8054331
merge main
emilykl Mar 11, 2025
1a195a7
add ci job to test with kaleido v1
emilykl Mar 11, 2025
577d3ca
remove -y option
emilykl Mar 11, 2025
89209ad
run test_kaleido instead of test_io
emilykl Mar 11, 2025
96bf9a0
re-add orca, add deprecation warnings for orca and kaleido-v0 (exact …
emilykl Mar 13, 2025
350dd48
error message for 'eps' with kaleido v1
emilykl Mar 20, 2025
08c1d4e
pin kaleido v1 (prerelease) in test requirements
emilykl Mar 20, 2025
8b47a0a
format
emilykl Mar 21, 2025
249cc9f
deprecation warning wording
emilykl Mar 24, 2025
ad9dbd9
fix if/else for when engine not specified
emilykl Mar 24, 2025
e75a5df
add flow and endpoint for installing chrome via plotly
emilykl Mar 25, 2025
a2b4f3c
add write_images and to_images functions (not yet using shared Kaleid…
emilykl Mar 25, 2025
2549299
format
emilykl Mar 25, 2025
ab3b700
fix test_image_renderer() test to work with both kaleido v0 and v1
emilykl Mar 27, 2025
de473e1
support pos args in to_images and write_images; rename plotly_install…
emilykl Mar 27, 2025
71696fe
mising pos args in write_images()
emilykl Mar 27, 2025
100b955
add tests for write_images() and to_images()
emilykl Mar 27, 2025
5541a79
add new API for setting image generation defaults
emilykl Mar 27, 2025
95a05db
format
emilykl Mar 27, 2025
0a73d0e
Merge branch 'main' into upgrade-kaleido
emilykl Mar 27, 2025
4d3dd56
install kaleido 1.0.0rc11 from PyPI
emilykl Mar 28, 2025
d871e74
remove extra import
emilykl Mar 28, 2025
b3e8d36
add [kaleido] install extra to pyproject.toml to help with installing…
emilykl Mar 31, 2025
ef5f520
fix deprecation warnings
emilykl Mar 31, 2025
611e2e4
Merge branch 'main' into upgrade-kaleido
emilykl Mar 31, 2025
c92a1ee
small updates to deprecation warning text
emilykl Mar 31, 2025
b56d5ec
Merge branch 'upgrade-kaleido' of https://github.com/plotly/plotly.py…
emilykl Mar 31, 2025
c01cb8a
add missing changelog entries
emilykl Mar 31, 2025
bcd40f3
update changelog for 6.1.0b0
emilykl Mar 31, 2025
54985b8
update pyproject.toml for 6.1.0b0
emilykl Mar 31, 2025
b4af0d5
Merge pull request #5121 from plotly/release-6.1.0b0
emilykl Mar 31, 2025
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
45 changes: 45 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,35 @@ commands:
source .venv/bin/activate
python -m pytest -x test_init/test_lazy_imports.py

test_io_kaleido_v0:
steps:
- checkout
- browser-tools/install-chrome
- browser-tools/install-chromedriver
- run:
name: Install dependencies
command: |
curl -LsSf https://astral.sh/uv/install.sh | sh
uv venv
source .venv/bin/activate
uv pip install .
uv pip install -r ./test_requirements/requirements_optional.txt
# Install Kaleido v0 instead of the v1 specified in requirements_optional.txt
uv pip uninstall kaleido
uv pip install kaleido==0.2.1
- run:
name: List installed packages and python version
command: |
source .venv/bin/activate
uv pip list
python --version
- run:
name: Test plotly.io image output with Kaleido v0
command: |
source .venv/bin/activate
python -m pytest tests/test_optional/test_kaleido
no_output_timeout: 20m

jobs:
check-code-formatting:
docker:
Expand Down Expand Up @@ -166,6 +195,17 @@ jobs:
pandas_version: <<parameters.pandas_version>>
numpy_version: <<parameters.numpy_version>>

test_kaleido_v0:
parameters:
python_version:
default: "3.12"
type: string
executor:
name: docker-container
python_version: <<parameters.python_version>>
steps:
- test_io_kaleido_v0

# Percy
python_311_percy:
docker:
Expand Down Expand Up @@ -448,5 +488,10 @@ workflows:
python_version: "3.9"
pandas_version: "1.2.4"
numpy_version: "1.26.4"
- test_kaleido_v0:
matrix:
parameters:
python_version:
- "3.12"
- python_311_percy
- build-doc
170 changes: 109 additions & 61 deletions plotly/io/_kaleido.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,43 @@
import os
import json
from pathlib import Path
import importlib.metadata as importlib_metadata
from packaging.version import Version
import warnings

import plotly
from plotly.io._utils import validate_coerce_fig_to_dict

try:
from kaleido.scopes.plotly import PlotlyScope

scope = PlotlyScope()
ENGINE_SUPPORT_TIMELINE = "September 2025"

# Compute absolute path to the 'plotly/package_data/' directory
root_dir = os.path.dirname(os.path.abspath(plotly.__file__))
package_dir = os.path.join(root_dir, "package_data")
scope.plotlyjs = os.path.join(package_dir, "plotly.min.js")
if scope.mathjax is None:
scope.mathjax = (
"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js"
)
except ImportError:
try:
import kaleido

kaleido_available = True
kaleido_major = Version(importlib_metadata.version("kaleido")).major

if kaleido_major < 1:
# Kaleido v0
from kaleido.scopes.plotly import PlotlyScope

scope = PlotlyScope()
# Compute absolute path to the 'plotly/package_data/' directory
root_dir = os.path.dirname(os.path.abspath(plotly.__file__))
package_dir = os.path.join(root_dir, "package_data")
scope.plotlyjs = os.path.join(package_dir, "plotly.min.js")
if scope.mathjax is None:
scope.mathjax = (
"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js"
)
except ImportError as e:
kaleido_available = False
kaleido_major = -1
PlotlyScope = None
scope = None


def to_image(
fig, format=None, width=None, height=None, scale=None, validate=True, engine="auto"
fig, format=None, width=None, height=None, scale=None, validate=True, engine=None
):
"""
Convert a figure to a static image bytes string
Expand All @@ -35,65 +49,61 @@ def to_image(

format: str or None
The desired image format. One of
- 'png'
- 'jpg' or 'jpeg'
- 'webp'
- 'svg'
- 'pdf'
- 'eps' (Requires the poppler library to be installed and on the PATH)
- 'png'
- 'jpg' or 'jpeg'
- 'webp'
- 'svg'
- 'pdf'
- 'eps' (Requires the poppler library to be installed and on the PATH)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we still handle EPS?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gvwilson Following up on this — Kaleido v1 does not support EPS yet. So either we drop support for EPS entirely, or document that EPS is only available with Kaleido v0 and add an informative error message.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please do the latter - thanks


If not specified, will default to:
- `plotly.io.kaleido.scope.default_format` if engine is "kaleido"
- `plotly.io.orca.config.default_format` if engine is "orca"
If not specified, will default to `plotly.io.kaleido.scope.default_format`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these references to plotly.io.kaleido.scope.. need to be something else if that's no longer imported here
https://github.com/plotly/plotly.py/pull/5062/files#diff-40813ac13aafeaa7135da57a86bec52cae638135b0acf9324677c9c4eee2ba2bR1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, those docstrings should be changed -- I need to research where those defaults are defined in Kaleido v1. (@ayjayt do you know off the top of your head?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/plotly/Kaleido/blob/d493b9dc0c04940721dc2d67e8b342d31fc6bb18/src/py/kaleido/_fig_tools.py#L9-L13

The order is png default overridden by user-specified output path w/ extension overriden by user-specified format in opts


width: int or None
The width of the exported image in layout pixels. If the `scale`
property is 1.0, this will also be the width of the exported image
in physical pixels.

If not specified, will default to:
- `plotly.io.kaleido.scope.default_width` if engine is "kaleido"
- `plotly.io.orca.config.default_width` if engine is "orca"
If not specified, will default to `plotly.io.kaleido.scope.default_width`

height: int or None
The height of the exported image in layout pixels. If the `scale`
property is 1.0, this will also be the height of the exported image
in physical pixels.

If not specified, will default to:
- `plotly.io.kaleido.scope.default_height` if engine is "kaleido"
- `plotly.io.orca.config.default_height` if engine is "orca"
If not specified, will default to `plotly.io.kaleido.scope.default_height`

scale: int or float or None
The scale factor to use when exporting the figure. A scale factor
larger than 1.0 will increase the image resolution with respect
to the figure's layout pixel dimensions. Whereas as scale factor of
less than 1.0 will decrease the image resolution.

If not specified, will default to:
- `plotly.io.kaleido.scope.default_scale` if engine is "kaleido"
- `plotly.io.orca.config.default_scale` if engine is "orca"

If not specified, will default to `plotly.io.kaleido.scope.default_scale`

validate: bool
True if the figure should be validated before being converted to
an image, False otherwise.

engine: str
Image export engine to use:
- "kaleido": Use Kaleido for image export
- "orca": Use Orca for image export
- "auto" (default): Use Kaleido if installed, otherwise use orca
engine (deprecated): str
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we print a deprecation warning if this argument is used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not yet, assuming we are in agreement about removing this argument, I'll add one

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm in agreement that we should remove this argument

No longer used. Kaleido is the only supported engine.

Returns
-------
bytes
The image data
"""

# Handle engine
# -------------
if engine is not None:
warnings.warn(
f"DeprecationWarning: The 'engine' argument is deprecated. Kaleido will be the only supported engine after {ENGINE_SUPPORT_TIMELINE}.",
DeprecationWarning,
)
engine = "auto"

if engine == "auto":
if scope is not None:
if kaleido_available:
# Default to kaleido if available
engine = "kaleido"
else:
Expand All @@ -109,6 +119,11 @@ def to_image(
engine = "kaleido"

if engine == "orca":
warnings.warn(
f"Support for the orca engine is deprecated and will be removed after {ENGINE_SUPPORT_TIMELINE}. "
+ "Please install Kaleido (`pip install kaleido`) to use the Kaleido engine.",
DeprecationWarning,
)
# Fall back to legacy orca image export path
from ._orca import to_image as to_image_orca

Expand All @@ -128,7 +143,7 @@ def to_image(
)

# Raise informative error message if Kaleido is not installed
if scope is None:
if not kaleido_available:
raise ValueError(
"""
Image export using the "kaleido" engine requires the kaleido package,
Expand All @@ -137,12 +152,40 @@ def to_image(
"""
)

# Validate figure
# ---------------
# Convert figure to dict (and validate if requested)
fig_dict = validate_coerce_fig_to_dict(fig, validate)
img_bytes = scope.transform(
fig_dict, format=format, width=width, height=height, scale=scale
)

# Request image bytes
if kaleido_major > 0:
# Kaleido v1
# Check if trying to export to EPS format, which is not supported in Kaleido v1
if format == "eps":
raise ValueError(
"""
EPS export is not supported with Kaleido v1.
Please downgrade to Kaleido v0 to use EPS export:
$ pip install kaleido==0.2.1
"""
)
img_bytes = kaleido.calc_fig_sync(
fig_dict,
path=None,
opts=dict(
format=format,
width=width,
height=height,
scale=scale,
),
)
else:
# Kaleido v0
warnings.warn(
f"Support for Kaleido versions less than 1.0.0 is deprecated and will be removed after {ENGINE_SUPPORT_TIMELINE}. Please upgrade Kaleido to version 1.0.0 or greater (`pip install --upgrade kaleido`).",
DeprecationWarning,
)
img_bytes = scope.transform(
fig_dict, format=format, width=width, height=height, scale=scale
)

return img_bytes

Expand Down Expand Up @@ -190,38 +233,29 @@ def write_image(
property is 1.0, this will also be the width of the exported image
in physical pixels.

If not specified, will default to:
- `plotly.io.kaleido.scope.default_width` if engine is "kaleido"
- `plotly.io.orca.config.default_width` if engine is "orca"
If not specified, will default to`plotly.io.kaleido.scope.default_width`

height: int or None
The height of the exported image in layout pixels. If the `scale`
property is 1.0, this will also be the height of the exported image
in physical pixels.

If not specified, will default to:
- `plotly.io.kaleido.scope.default_height` if engine is "kaleido"
- `plotly.io.orca.config.default_height` if engine is "orca"
If not specified, will default to `plotly.io.kaleido.scope.default_height`

scale: int or float or None
The scale factor to use when exporting the figure. A scale factor
larger than 1.0 will increase the image resolution with respect
to the figure's layout pixel dimensions. Whereas as scale factor of
less than 1.0 will decrease the image resolution.

If not specified, will default to:
- `plotly.io.kaleido.scope.default_scale` if engine is "kaleido"
- `plotly.io.orca.config.default_scale` if engine is "orca"
If not specified, will default to `plotly.io.kaleido.scope.default_scale`

validate: bool
True if the figure should be validated before being converted to
an image, False otherwise.

engine: str
Image export engine to use:
- "kaleido": Use Kaleido for image export
- "orca": Use Orca for image export
- "auto" (default): Use Kaleido if installed, otherwise use orca
engine (deprecated): str
No longer used. Kaleido is the only supported engine.

Returns
-------
Expand Down Expand Up @@ -323,7 +357,7 @@ def full_figure_for_development(fig, warn=True, as_dict=False):
"""

# Raise informative error message if Kaleido is not installed
if scope is None:
if not kaleido_available:
raise ValueError(
"""
Full figure generation requires the kaleido package,
Expand All @@ -341,7 +375,21 @@ def full_figure_for_development(fig, warn=True, as_dict=False):
"To suppress this warning, set warn=False"
)

fig = json.loads(scope.transform(fig, format="json").decode("utf-8"))
if kaleido_major > 0:
# Kaleido v1
bytes = kaleido.calc_fig_sync(
fig,
opts=dict(format="json"),
)
fig = json.loads(bytes.decode("utf-8"))
else:
# Kaleido v0
warnings.warn(
f"Support for Kaleido versions less than 1.0.0 is deprecated and will be removed after {ENGINE_SUPPORT_TIMELINE}. Please upgrade Kaleido to version 1.0.0 or greater (`pip install --upgrade kaleido`).",
DeprecationWarning,
)
fig = json.loads(scope.transform(fig, format="json").decode("utf-8"))

if as_dict:
return fig
else:
Expand Down
2 changes: 1 addition & 1 deletion plotly/io/kaleido.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from ._kaleido import to_image, write_image, scope
from ._kaleido import write_image, to_image
4 changes: 3 additions & 1 deletion test_requirements/requirements_optional.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ pyshp
matplotlib
scikit-image
psutil
kaleido
# kaleido>=1.0.0 # Uncomment and delete line below once Kaleido v1 is released
git+https://github.com/plotly/[email protected]#subdirectory=src/py
orjson
polars[timezone]
pyarrow
plotly-geo
vaex;python_version<="3.9"
pdfrw
Loading