6
6
import warnings
7
7
8
8
import plotly
9
- from plotly .io ._utils import validate_coerce_fig_to_dict
9
+ from plotly .io ._utils import validate_coerce_fig_to_dict , as_individual_kwargs
10
10
11
11
ENGINE_SUPPORT_TIMELINE = "September 2025"
12
12
29
29
scope .mathjax = (
30
30
"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js"
31
31
)
32
+
32
33
except ImportError as e :
33
34
kaleido_available = False
34
35
kaleido_major = - 1
37
38
38
39
39
40
def to_image (
40
- fig , format = None , width = None , height = None , scale = None , validate = True , engine = None
41
+ fig ,
42
+ format = None ,
43
+ width = None ,
44
+ height = None ,
45
+ scale = None ,
46
+ validate = True ,
47
+ engine = None ,
48
+ kaleido_instance = None ,
41
49
):
42
50
"""
43
51
Convert a figure to a static image bytes string
@@ -87,6 +95,9 @@ def to_image(
87
95
engine (deprecated): str
88
96
No longer used. Kaleido is the only supported engine.
89
97
98
+ kaleido_instance: kaleido.Kaleido or None
99
+ An instance of the Kaleido class. If None, a new instance will be created.
100
+
90
101
Returns
91
102
-------
92
103
bytes
@@ -162,15 +173,17 @@ def to_image(
162
173
# Check if trying to export to EPS format, which is not supported in Kaleido v1
163
174
if format == "eps" :
164
175
raise ValueError (
165
- """
166
- EPS export is not supported with Kaleido v1.
167
- Please downgrade to Kaleido v0 to use EPS export:
168
- $ pip install kaleido==0.2.1
176
+ f"""
177
+ EPS export is not supported with Kaleido v1. Please use SVG or PDF instead.
178
+ You can also downgrade to Kaleido v0, but support for v0 will be removed after { ENGINE_SUPPORT_TIMELINE } .
179
+ To downgrade to Kaleido v0, run:
180
+ $ pip install kaleido<1.0.0
169
181
"""
170
182
)
171
183
import choreographer
172
184
173
185
try :
186
+ # TODO: Actually use provided kaleido_instance here
174
187
img_bytes = kaleido .calc_fig_sync (
175
188
fig_dict ,
176
189
path = None ,
@@ -204,35 +217,6 @@ def to_image(
204
217
return img_bytes
205
218
206
219
207
- def install_chrome ():
208
- """
209
- Install Google Chrome for Kaleido
210
- This function can be run from the command line using the command plotly_install_chrome
211
- defined in pyproject.toml
212
- """
213
- if not kaleido_available or kaleido_major < 1 :
214
- raise ValueError (
215
- "This command requires Kaleido v1.0.0 or greater. Install it using `pip install kaleido`."
216
- )
217
- import choreographer
218
- import sys
219
-
220
- cli_yes = len (sys .argv ) > 1 and sys .argv [1 ] == "-y"
221
- if not cli_yes :
222
- print (
223
- "\n Plotly will install a copy of Google Chrome to be used for generating static images of plots.\n "
224
- )
225
- # TODO: Print path where Chrome will be installed
226
- # print(f"Chrome will be installed at {chrome_download_path}\n")
227
- response = input ("Do you want to proceed? [y/n] " )
228
- if not response or response [0 ].lower () != "y" :
229
- print ("Cancelled" )
230
- return
231
- print ("Installing Chrome for Plotly..." )
232
- kaleido .get_chrome_sync ()
233
- print ("Chrome installed successfully." )
234
-
235
-
236
220
def write_image (
237
221
fig ,
238
222
file ,
@@ -242,6 +226,7 @@ def write_image(
242
226
height = None ,
243
227
validate = True ,
244
228
engine = "auto" ,
229
+ kaleido_instance = None ,
245
230
):
246
231
"""
247
232
Convert a figure to a static image and write it to a file or writeable
@@ -300,6 +285,9 @@ def write_image(
300
285
engine (deprecated): str
301
286
No longer used. Kaleido is the only supported engine.
302
287
288
+ kaleido_instance: kaleido.Kaleido or None
289
+ An instance of the Kaleido class. If None, a new instance will be created.
290
+
303
291
Returns
304
292
-------
305
293
None
@@ -348,6 +336,7 @@ def write_image(
348
336
height = height ,
349
337
validate = validate ,
350
338
engine = engine ,
339
+ kaleido_instance = kaleido_instance ,
351
340
)
352
341
353
342
# Open file
@@ -373,6 +362,69 @@ def write_image(
373
362
path .write_bytes (img_data )
374
363
375
364
365
+ def to_images (** kwargs ):
366
+ """
367
+ Convert multiple figures to static images and return a list of image bytes
368
+
369
+ Parameters
370
+ ----------
371
+ Accepts the same parameters as pio.to_image(), but any parameter may be either
372
+ a single value or a list of values. If more than one parameter is a list,
373
+ all must be the same length.
374
+
375
+ Returns
376
+ -------
377
+ list of bytes
378
+ The image data
379
+ """
380
+ individual_kwargs = as_individual_kwargs (** kwargs )
381
+
382
+ if kaleido_available and kaleido_major > 0 :
383
+ # Kaleido v1
384
+ # TODO: Use a single shared kaleido instance for all images
385
+ return [to_image (** kw ) for kw in individual_kwargs ]
386
+ else :
387
+ # Kaleido v0, or orca
388
+ return [to_image (** kw ) for kw in individual_kwargs ]
389
+
390
+
391
+ def write_images (** kwargs ):
392
+ """
393
+ Write multiple images to files or writeable objects. This is much faster than
394
+ calling write_image() multiple times.
395
+
396
+ Parameters
397
+ ----------
398
+ Accepts the same parameters as pio.write_image(), but any parameter may be either
399
+ a single value or a list of values. If more than one parameter is a list,
400
+ all must be the same length.
401
+
402
+ Returns
403
+ -------
404
+ None
405
+ """
406
+
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
+ ]
416
+
417
+ if kaleido_available and kaleido_major > 0 :
418
+ # Kaleido v1
419
+ # 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 )
422
+ else :
423
+ # Kaleido v0, or orca
424
+ for f , kw in zip (files , individual_kwargs ):
425
+ write_image (file = f , ** kw )
426
+
427
+
376
428
def full_figure_for_development (fig , warn = True , as_dict = False ):
377
429
"""
378
430
Compute default values for all attributes not specified in the input figure and
@@ -442,4 +494,32 @@ def full_figure_for_development(fig, warn=True, as_dict=False):
442
494
return go .Figure (fig , skip_invalid = True )
443
495
444
496
497
+ def install_chrome ():
498
+ """
499
+ Install Google Chrome for Kaleido
500
+ This function can be run from the command line using the command `plotly_install_chrome`
501
+ defined in pyproject.toml
502
+ """
503
+ if not kaleido_available or kaleido_major < 1 :
504
+ raise ValueError (
505
+ "This command requires Kaleido v1.0.0 or greater. Install it using `pip install kaleido`."
506
+ )
507
+ import sys
508
+
509
+ cli_yes = len (sys .argv ) > 1 and sys .argv [1 ] == "-y"
510
+ if not cli_yes :
511
+ print (
512
+ "\n Plotly will install a copy of Google Chrome to be used for generating static images of plots.\n "
513
+ )
514
+ # TODO: Print path where Chrome will be installed
515
+ # print(f"Chrome will be installed at {chrome_download_path}\n")
516
+ response = input ("Do you want to proceed? [y/n] " )
517
+ if not response or response [0 ].lower () != "y" :
518
+ print ("Cancelled" )
519
+ return
520
+ print ("Installing Chrome for Plotly..." )
521
+ kaleido .get_chrome_sync ()
522
+ print ("Chrome installed successfully." )
523
+
524
+
445
525
__all__ = ["to_image" , "write_image" , "scope" , "full_figure_for_development" ]
0 commit comments