Skip to content

Commit de473e1

Browse files
committed
support pos args in to_images and write_images; rename plotly_install_chrome to plotly_get_chrome
1 parent ab3b700 commit de473e1

File tree

3 files changed

+37
-31
lines changed

3 files changed

+37
-31
lines changed

plotly/io/_kaleido.py

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import warnings
77

88
import plotly
9-
from plotly.io._utils import validate_coerce_fig_to_dict, as_individual_kwargs
9+
from plotly.io._utils import validate_coerce_fig_to_dict, as_individual_args
1010

1111
ENGINE_SUPPORT_TIMELINE = "September 2025"
1212

@@ -199,7 +199,7 @@ def to_image(
199199
"""
200200
201201
Kaleido requires Google Chrome to be installed. Install it by running:
202-
$ plotly_install_chrome
202+
$ plotly_get_chrome
203203
"""
204204
)
205205

@@ -362,7 +362,7 @@ def write_image(
362362
path.write_bytes(img_data)
363363

364364

365-
def to_images(**kwargs):
365+
def to_images(*args, **kwargs):
366366
"""
367367
Convert multiple figures to static images and return a list of image bytes
368368
@@ -377,18 +377,18 @@ def to_images(**kwargs):
377377
list of bytes
378378
The image data
379379
"""
380-
individual_kwargs = as_individual_kwargs(**kwargs)
380+
individual_args, individual_kwargs = as_individual_args(*args, **kwargs)
381381

382382
if kaleido_available and kaleido_major > 0:
383383
# Kaleido v1
384384
# TODO: Use a single shared kaleido instance for all images
385-
return [to_image(**kw) for kw in individual_kwargs]
385+
return [to_image(*a, **kw) for a, kw in zip(individual_args, individual_kwargs)]
386386
else:
387387
# Kaleido v0, or orca
388-
return [to_image(**kw) for kw in individual_kwargs]
388+
return [to_image(*a, **kw) for a, kw in zip(individual_args, individual_kwargs)]
389389

390390

391-
def write_images(**kwargs):
391+
def write_images(*args, **kwargs):
392392
"""
393393
Write multiple images to files or writeable objects. This is much faster than
394394
calling write_image() multiple times.
@@ -404,25 +404,18 @@ def write_images(**kwargs):
404404
None
405405
"""
406406

407-
if "file" not in kwargs:
408-
raise ValueError("'file' argument is required")
409-
410-
# Get individual arguments, and separate out the 'file' argument
411-
individual_kwargs = as_individual_kwargs(**kwargs)
412-
files = [kw["file"] for kw in individual_kwargs]
413-
individual_kwargs = [
414-
{k: v for k, v in kw.items() if k != "file"} for kw in individual_kwargs
415-
]
407+
# Get individual arguments
408+
individual_args, individual_kwargs = as_individual_args(*args, **kwargs)
416409

417410
if kaleido_available and kaleido_major > 0:
418411
# Kaleido v1
419412
# TODO: Use a single shared kaleido instance for all images
420-
for f, kw in zip(files, individual_kwargs):
421-
write_image(file=f, **kw)
413+
for a, kw in zip(individual_args, individual_kwargs):
414+
write_image(**kw)
422415
else:
423416
# Kaleido v0, or orca
424-
for f, kw in zip(files, individual_kwargs):
425-
write_image(file=f, **kw)
417+
for a, kw in zip(individual_args, individual_kwargs):
418+
write_image(*a, **kw)
426419

427420

428421
def full_figure_for_development(fig, warn=True, as_dict=False):
@@ -494,10 +487,10 @@ def full_figure_for_development(fig, warn=True, as_dict=False):
494487
return go.Figure(fig, skip_invalid=True)
495488

496489

497-
def install_chrome():
490+
def get_chrome():
498491
"""
499492
Install Google Chrome for Kaleido
500-
This function can be run from the command line using the command `plotly_install_chrome`
493+
This function can be run from the command line using the command `plotly_get_chrome`
501494
defined in pyproject.toml
502495
"""
503496
if not kaleido_available or kaleido_major < 1:

plotly/io/_utils.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,40 +43,53 @@ def validate_coerce_output_type(output_type):
4343
return cls
4444

4545

46-
def as_individual_kwargs(**kwargs):
46+
def as_individual_args(*args, **kwargs):
4747
"""
48-
Given one or more keyword arguments which may be either a single value or a list of values,
49-
return a list of dictionaries where each dictionary has only single values for each keyword
48+
Given one or more positional or keyword arguments which may be either a single value
49+
or a list of values, return a list of lists and a list of dictionaries
5050
by expanding the single values into lists.
51-
If more than one keyword is a list, all lists must be the same length.
51+
If more than one item in the input is a list, all lists must be the same length.
5252
5353
Parameters
5454
----------
55-
kwargs: dict
55+
*args: list
56+
The positional arguments
57+
**kwargs: dict
5658
The keyword arguments
5759
5860
Returns
5961
-------
62+
list of lists
63+
A list of lists
6064
list of dicts
6165
A list of dictionaries
6266
"""
6367
# Check that all list arguments have the same length,
6468
# and find out what that length is
6569
# If there are no list arguments, length is 1
66-
list_lengths = [len(v) for v in kwargs.values() if isinstance(v, list)]
70+
list_lengths = [len(v) for v in args + tuple(kwargs.values()) if isinstance(v, list)]
6771
if list_lengths and len(set(list_lengths)) > 1:
6872
raise ValueError("All list arguments must have the same length.")
6973
list_length = list_lengths[0] if list_lengths else 1
7074

7175
# Expand all arguments to lists of the same length
76+
expanded_args = [
77+
[v] * list_length if not isinstance(v, list) else v for v in args
78+
]
7279
expanded_kwargs = {
7380
k: [v] * list_length if not isinstance(v, list) else v
7481
for k, v in kwargs.items()
7582
}
7683

84+
# Reshape into a list of lists
85+
# Each list represents the positional arguments for a single function call
86+
list_of_args = [[v[i] for v in expanded_args] for i in range(list_length)]
87+
7788
# Reshape into a list of dictionaries
78-
# Each dictionary represents the arguments for a single call to to_image
79-
return [{k: v[i] for k, v in expanded_kwargs.items()} for i in range(list_length)]
89+
# Each dictionary represents the keyword arguments for a single function call
90+
list_of_kwargs = [{k: v[i] for k, v in expanded_kwargs.items()} for i in range(list_length)]
91+
92+
return list_of_args, list_of_kwargs
8093

8194

8295
def plotly_cdn_url(cdn_ver=get_plotlyjs_version()):

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ dependencies = [
4747
express = ["numpy"]
4848

4949
[project.scripts]
50-
plotly_install_chrome = "plotly.io._kaleido:install_chrome"
50+
plotly_get_chrome = "plotly.io._kaleido:get_chrome"
5151

5252
[tool.setuptools.packages.find]
5353
where = ["."]

0 commit comments

Comments
 (0)