Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
57 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
0f61cc3
remove to_images tests
emilykl Apr 13, 2025
fe12aec
remove to_images function
emilykl Apr 13, 2025
692842f
refactor write_images to use kaleido write_fig function (WIP)
emilykl Apr 14, 2025
aac9c20
refactor write_images() to use kaleido.write_fig_from_object_sync()
emilykl Apr 14, 2025
ad57031
update kaleido tests
emilykl Apr 14, 2025
b9e5f7a
Merge branch 'main' into upgrade-kaleido
emilykl Apr 14, 2025
720ada5
add and fix type hints
emilykl Apr 14, 2025
53d486a
add mathjax and topojson config options
emilykl Apr 15, 2025
c990736
format
emilykl Apr 15, 2025
96bb17a
fix a few bugs with setting topojson default
emilykl Apr 24, 2025
f736f4c
remove 'import choreographer' and fix SyntaxWarning
emilykl Apr 24, 2025
aca1620
update kaleido rc version for tests
emilykl Apr 24, 2025
564aa51
clean up deprecation messages, fix bug with passing mathjax
emilykl Apr 28, 2025
2b45f47
add --path argument for plotly_get_chrome command
emilykl Apr 28, 2025
691fce6
format
emilykl Apr 28, 2025
0799bf5
merge main
emilykl Apr 28, 2025
651eafd
print exe path after installing Chrome
emilykl Apr 29, 2025
0413341
format
emilykl Apr 29, 2025
0c1b1c1
format
emilykl Apr 29, 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
Prev Previous commit
Next Next commit
refactor write_images() to use kaleido.write_fig_from_object_sync()
  • Loading branch information
emilykl committed Apr 14, 2025
commit aac9c203d5172b9f88c8c2ef0d63a7dbccd8d3cf
101 changes: 57 additions & 44 deletions plotly/io/_kaleido.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@
import warnings

import plotly
from plotly.io._utils import validate_coerce_fig_to_dict, as_individual_args
from plotly.io._utils import validate_coerce_fig_to_dict, broadcast_args_to_dicts
from plotly.io import defaults

ENGINE_SUPPORT_TIMELINE = "September 2025"

PLOTLY_GET_CHROME_ERROR_MSG = """

Kaleido requires Google Chrome to be installed. Install it by running:
$ plotly_get_chrome
"""


# TODO: Remove --pre flag once Kaleido v1 full release is available
KALEIDO_DEPRECATION_MSG = f"""
Expand Down Expand Up @@ -158,6 +164,7 @@ def as_path_object(file: str | Path) -> Path | None:
path = None
return path


def infer_format(path: Path | None, format: str | None) -> str | None:
if path is not None and format is None:
ext = path.suffix
Expand All @@ -177,7 +184,6 @@ def infer_format(path: Path | None, format: str | None) -> str | None:
return format



def to_image(
fig,
format=None,
Expand Down Expand Up @@ -331,13 +337,7 @@ def to_image(
),
)
except choreographer.errors.ChromeNotFoundError:
raise RuntimeError(
"""

Kaleido requires Google Chrome to be installed. Install it by running:
$ plotly_get_chrome
"""
)
raise RuntimeError(PLOTLY_GET_CHROME_ERROR_MSG)

else:
# Kaleido v0
Expand Down Expand Up @@ -481,7 +481,7 @@ def write_image(


def write_images(
figs,
fig,
file,
format=None,
scale=None,
Expand All @@ -494,13 +494,17 @@ def write_images(
calling write_image() multiple times. This function can only be used with the Kaleido
engine, v1.0.0 or greater.

This function accepts the same arguments as write_image() (minus the `engine` argument),
except that any of the arguments may be either a single value or an iterable of values.
If multiple arguments are iterable, they must all have the same length.

Parameters
----------
figs:
fig:
Iterable of figure objects or dicts representing a figure

directory: str or writeable
A string or pathlib.Path object representing a local directory path.
file: str or writeable
Iterables of strings or pathlib.Path objects representing local file paths to write to.

format: str or None
The desired image format. One of
Expand Down Expand Up @@ -562,39 +566,48 @@ def write_images(
"""
)

# Try to cast `file` as a pathlib object `path`.
path = as_path_object(file)

# Infer image format if not specified
format = infer_format(path, format)

# Convert figures to dicts (and validate if requested)
# TODO: Keep same iterable type
fig_dicts = [validate_coerce_fig_to_dict(fig, validate) for fig in figs]

kaleido.write_fig_sync(
fig_dicts,
directory=path,
opts=dict(
format=format or defaults.default_format,
width=width or defaults.default_width,
height=height or defaults.default_height,
scale=scale or defaults.default_scale,
),
# Broadcast arguments into correct format for passing to Kaleido
arg_dicts = broadcast_args_to_dicts(
fig=fig,
file=file,
format=format,
scale=scale,
width=width,
height=height,
validate=validate,
)

# # Get individual arguments
# individual_args, individual_kwargs = as_individual_args(*args, **kwargs)

# if kaleido_available() and kaleido_major() > 0:
# # Kaleido v1
# # TODO: Use a single shared kaleido instance for all images
# for a, kw in zip(individual_args, individual_kwargs):
# write_image(*a, **kw)
# else:
# # Kaleido v0, or orca
# for a, kw in zip(individual_args, individual_kwargs):
# write_image(*a, **kw)
# For each dict:
# - convert figures to dicts (and validate if requested)
# - try to cast `file` as a Path object
for d in arg_dicts:
d["fig"] = validate_coerce_fig_to_dict(d["fig"], d["validate"])
d["file"] = as_path_object(d["file"])

# Reshape arg_dicts into correct format for passing to Kaleido
# We call infer_format() here rather than above so that the `file` argument
# has already been cast to a Path object.
# Also insert defaults for any missing arguments as needed
kaleido_specs = [
{
"fig": d["fig"],
"path": d["file"],
"opts": dict(
format=infer_format(d["file"], d["format"]) or defaults.default_format,
width=d["width"] or defaults.default_width,
height=d["height"] or defaults.default_height,
scale=d["scale"] or defaults.default_scale,
),
}
for d in arg_dicts
]

import choreographer

try:
kaleido.write_fig_from_object_sync(kaleido_specs)
except choreographer.errors.ChromeNotFoundError:
raise RuntimeError(PLOTLY_GET_CHROME_ERROR_MSG)


def full_figure_for_development(fig, warn=True, as_dict=False):
Expand Down
28 changes: 10 additions & 18 deletions plotly/io/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,55 +43,47 @@ def validate_coerce_output_type(output_type):
return cls


def as_individual_args(*args, **kwargs):
def broadcast_args_to_dicts(**kwargs):
"""
Given one or more positional or keyword arguments which may be either a single value
or a list of values, return a list of lists and a list of dictionaries
by expanding the single values into lists.
Given one or more keyword arguments which may be either a single value or a list of values,
return a list of keyword dictionaries by broadcasting the single valuesacross all the dicts.
If more than one item in the input is a list, all lists must be the same length.

Parameters
----------
*args: list
The positional arguments
**kwargs: dict
The keyword arguments

Returns
-------
list of lists
A list of lists
list of dicts
A list of dictionaries

Raises
------
ValueError
If any of the input lists are not the same length
"""
# Check that all list arguments have the same length,
# and find out what that length is
# If there are no list arguments, length is 1
list_lengths = [
len(v) for v in args + tuple(kwargs.values()) if isinstance(v, list)
]
list_lengths = [len(v) for v in tuple(kwargs.values()) if isinstance(v, list)]
if list_lengths and len(set(list_lengths)) > 1:
raise ValueError("All list arguments must have the same length.")
list_length = list_lengths[0] if list_lengths else 1

# Expand all arguments to lists of the same length
expanded_args = [[v] * list_length if not isinstance(v, list) else v for v in args]
expanded_kwargs = {
k: [v] * list_length if not isinstance(v, list) else v
for k, v in kwargs.items()
}

# Reshape into a list of lists
# Each list represents the positional arguments for a single function call
list_of_args = [[v[i] for v in expanded_args] for i in range(list_length)]

# Reshape into a list of dictionaries
# Each dictionary represents the keyword arguments for a single function call
list_of_kwargs = [
{k: v[i] for k, v in expanded_kwargs.items()} for i in range(list_length)
]

return list_of_args, list_of_kwargs
return list_of_kwargs


def plotly_cdn_url(cdn_ver=get_plotlyjs_version()):
Expand Down