1
1
import os
2
2
import json
3
3
from pathlib import Path
4
+ from typing import Union , List
4
5
import importlib .metadata as importlib_metadata
5
6
from packaging .version import Version
6
7
import warnings
50
51
)
51
52
52
53
53
- def kaleido_available ():
54
+ def kaleido_available () -> bool :
55
+ """
56
+ Returns True if any version of Kaleido is installed, otherwise False.
57
+ """
54
58
global _KALEIDO_AVAILABLE
55
59
global _KALEIDO_MAJOR
56
60
if _KALEIDO_AVAILABLE is not None :
@@ -64,7 +68,11 @@ def kaleido_available():
64
68
return _KALEIDO_AVAILABLE
65
69
66
70
67
- def kaleido_major ():
71
+ def kaleido_major () -> int :
72
+ """
73
+ Returns the major version number of Kaleido if it is installed,
74
+ otherwise raises a ValueError.
75
+ """
68
76
global _KALEIDO_MAJOR
69
77
if _KALEIDO_MAJOR is not None :
70
78
return _KALEIDO_MAJOR
@@ -146,7 +154,7 @@ def __setattr__(self, name, value):
146
154
scope = None
147
155
148
156
149
- def as_path_object (file : str | Path ) -> Path | None :
157
+ def as_path_object (file : Union [ str , Path ] ) -> Union [ Path , None ] :
150
158
"""
151
159
Cast the `file` argument, which may be either a string or a Path object,
152
160
to a Path object.
@@ -185,14 +193,15 @@ def infer_format(path: Path | None, format: str | None) -> str | None:
185
193
186
194
187
195
def to_image (
188
- fig ,
189
- format = None ,
190
- width = None ,
191
- height = None ,
192
- scale = None ,
193
- validate = True ,
194
- engine = None ,
195
- ):
196
+ fig : Union [dict , plotly .graph_objects .Figure ],
197
+ format : Union [str , None ]= None ,
198
+ width : Union [int , None ]= None ,
199
+ height : Union [int , None ]= None ,
200
+ scale : Union [int , float , None ]= None ,
201
+ validate : bool = True ,
202
+ # Deprecated
203
+ engine : Union [str , None ]= None ,
204
+ ) -> bytes :
196
205
"""
197
206
Convert a figure to a static image bytes string
198
207
@@ -350,14 +359,15 @@ def to_image(
350
359
351
360
352
361
def write_image (
353
- fig ,
354
- file ,
355
- format = None ,
356
- scale = None ,
357
- width = None ,
358
- height = None ,
359
- validate = True ,
360
- engine = "auto" ,
362
+ fig : Union [dict , plotly .graph_objects .Figure ],
363
+ file : Union [str , Path ],
364
+ format : Union [str , None ]= None ,
365
+ scale : Union [int , float , None ]= None ,
366
+ width : Union [int , None ]= None ,
367
+ height : Union [int , None ]= None ,
368
+ validate : bool = True ,
369
+ # Deprecated
370
+ engine : Union [str , None ]= "auto" ,
361
371
):
362
372
"""
363
373
Convert a figure to a static image and write it to a file or writeable
@@ -481,14 +491,14 @@ def write_image(
481
491
482
492
483
493
def write_images (
484
- fig ,
485
- file ,
486
- format = None ,
487
- scale = None ,
488
- width = None ,
489
- height = None ,
490
- validate = True ,
491
- ):
494
+ fig : Union [ List [ Union [ dict , plotly . graph_objects . Figure ]], Union [ dict , plotly . graph_objects . Figure ]] ,
495
+ file : Union [ List [ Union [ str , Path ]], Union [ str , Path ]] ,
496
+ format : Union [ List [ Union [ str , None ]], Union [ str , None ]] = None ,
497
+ scale : Union [ List [ Union [ int , float , None ]], Union [ int , float , None ]] = None ,
498
+ width : Union [ List [ Union [ int , None ]], Union [ int , None ]] = None ,
499
+ height : Union [ List [ Union [ int , None ]], Union [ int , None ]] = None ,
500
+ validate : Union [ List [ bool ], bool ] = True ,
501
+ ) -> None :
492
502
"""
493
503
Write multiple images to files or writeable objects. This is much faster than
494
504
calling write_image() multiple times. This function can only be used with the Kaleido
@@ -610,7 +620,11 @@ def write_images(
610
620
raise RuntimeError (PLOTLY_GET_CHROME_ERROR_MSG )
611
621
612
622
613
- def full_figure_for_development (fig , warn = True , as_dict = False ):
623
+ def full_figure_for_development (
624
+ fig : Union [dict , plotly .graph_objects .Figure ],
625
+ warn : bool = True ,
626
+ as_dict : bool = False ,
627
+ ) -> Union [plotly .graph_objects .Figure , dict ]:
614
628
"""
615
629
Compute default values for all attributes not specified in the input figure and
616
630
returns the output as a "full" figure. This function calls Plotly.js via Kaleido
@@ -677,7 +691,7 @@ def full_figure_for_development(fig, warn=True, as_dict=False):
677
691
return go .Figure (fig , skip_invalid = True )
678
692
679
693
680
- def get_chrome ():
694
+ def get_chrome () -> None :
681
695
"""
682
696
Install Google Chrome for Kaleido
683
697
This function can be run from the command line using the command `plotly_get_chrome`
0 commit comments