7
7
8
8
import plotly
9
9
from plotly .io ._utils import validate_coerce_fig_to_dict , as_individual_args
10
+ from plotly .io import defaults
10
11
11
12
ENGINE_SUPPORT_TIMELINE = "September 2025"
12
13
14
+ kaleido_scope_default_getwarning = (
15
+ lambda x : f"""
16
+ Accessing plotly.io.kaleido.scope.{ x } is deprecated and will be removed after { ENGINE_SUPPORT_TIMELINE } .
17
+ Please use plotly.io.defaults.{ x } instead.
18
+ """
19
+ )
20
+
21
+ kaleido_scope_default_setwarning = (
22
+ lambda x : f"""
23
+ Setting plotly.io.kaleido.scope.{ x } is deprecated and will be removed after { ENGINE_SUPPORT_TIMELINE } . "
24
+ Please set plotly.io.defaults.{ x } instead.
25
+ """
26
+ )
27
+
28
+ bad_attribute_error = (
29
+ lambda x : f"""
30
+ Attribute plotly.io.defaults.{ x } is not valid.
31
+ Also, plotly.io.kaleido.scope.* is deprecated and will be removed after { ENGINE_SUPPORT_TIMELINE } . Please use plotly.io.defaults.* instead.
32
+ """
33
+ )
34
+
35
+
13
36
try :
14
37
import kaleido
15
38
20
43
# Kaleido v0
21
44
from kaleido .scopes .plotly import PlotlyScope
22
45
23
- scope = PlotlyScope ()
46
+ # Show a deprecation warning if the old method of setting defaults is used
47
+ class PlotlyScopeWithDeprecationWarnings (PlotlyScope ):
48
+ def __setattr__ (self , name , value ):
49
+ if name in defaults .__dict__ :
50
+ warnings .warn (
51
+ kaleido_scope_default_setwarning (name ),
52
+ DeprecationWarning ,
53
+ stacklevel = 2 ,
54
+ )
55
+ setattr (defaults , name , value )
56
+ super (PlotlyScopeWithDeprecationWarnings , self ).__setattr__ (name , value )
57
+
58
+ def __getattr__ (self , name ):
59
+ if name in defaults .__dict__ :
60
+ warnings .warn (
61
+ kaleido_scope_default_getwarning (name ),
62
+ DeprecationWarning ,
63
+ stacklevel = 2 ,
64
+ )
65
+ return super (PlotlyScopeWithDeprecationWarnings , self ).__getattr__ (name )
66
+
67
+ scope = PlotlyScopeWithDeprecationWarnings ()
24
68
# Compute absolute path to the 'plotly/package_data/' directory
25
69
root_dir = os .path .dirname (os .path .abspath (plotly .__file__ ))
26
70
package_dir = os .path .join (root_dir , "package_data" )
29
73
scope .mathjax = (
30
74
"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js"
31
75
)
76
+ else :
77
+ # Kaleido v1
78
+
79
+ # Show a deprecation warning if the old method of setting defaults is used
80
+ class DefaultsDeprecationWarning :
81
+ def __getattr__ (self , name ):
82
+ if name in defaults .__dict__ :
83
+ warnings .warn (
84
+ kaleido_scope_default_getwarning (name ),
85
+ DeprecationWarning ,
86
+ stacklevel = 2 ,
87
+ )
88
+ return getattr (defaults , name )
89
+ else :
90
+ raise AttributeError (bad_attribute_error (name ))
91
+
92
+ def __setattr__ (self , name , value ):
93
+ if name in defaults .__dict__ :
94
+ warnings .warn (
95
+ kaleido_scope_default_setwarning (name ),
96
+ DeprecationWarning ,
97
+ stacklevel = 2 ,
98
+ )
99
+ setattr (defaults , name , value )
100
+ else :
101
+ raise AttributeError (bad_attribute_error (name ))
102
+
103
+ scope = DefaultsDeprecationWarning ()
32
104
33
105
except ImportError as e :
34
106
kaleido_available = False
@@ -64,29 +136,37 @@ def to_image(
64
136
- 'pdf'
65
137
- 'eps' (Requires the poppler library to be installed and on the PATH)
66
138
67
- If not specified, will default to `plotly.io.kaleido.scope.default_format`
139
+ If not specified, will default to:
140
+ - `plotly.io.defaults.default_format` if engine is "kaleido"
141
+ - `plotly.io.orca.config.default_format` if engine is "orca" (deprecated)
68
142
69
143
width: int or None
70
144
The width of the exported image in layout pixels. If the `scale`
71
145
property is 1.0, this will also be the width of the exported image
72
146
in physical pixels.
73
147
74
- If not specified, will default to `plotly.io.kaleido.scope.default_width`
148
+ If not specified, will default to:
149
+ - `plotly.io.defaults.default_width` if engine is "kaleido"
150
+ - `plotly.io.orca.config.default_width` if engine is "orca" (deprecated)
75
151
76
152
height: int or None
77
153
The height of the exported image in layout pixels. If the `scale`
78
154
property is 1.0, this will also be the height of the exported image
79
155
in physical pixels.
80
156
81
- If not specified, will default to `plotly.io.kaleido.scope.default_height`
157
+ If not specified, will default to:
158
+ - `plotly.io.defaults.default_height` if engine is "kaleido"
159
+ - `plotly.io.orca.config.default_height` if engine is "orca" (deprecated)
82
160
83
161
scale: int or float or None
84
162
The scale factor to use when exporting the figure. A scale factor
85
163
larger than 1.0 will increase the image resolution with respect
86
164
to the figure's layout pixel dimensions. Whereas as scale factor of
87
165
less than 1.0 will decrease the image resolution.
88
166
89
- If not specified, will default to `plotly.io.kaleido.scope.default_scale`
167
+ If not specified, will default to:
168
+ - `plotly.io.defaults.default_scale` if engine is "kaliedo"
169
+ - `plotly.io.orca.config.default_scale` if engine is "orca" (deprecated)
90
170
91
171
validate: bool
92
172
True if the figure should be validated before being converted to
@@ -174,8 +254,8 @@ def to_image(
174
254
if format == "eps" :
175
255
raise ValueError (
176
256
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 } .
257
+ EPS export is not supported by Kaleido v1. Please use SVG or PDF instead.
258
+ You can also downgrade to Kaleido v0, but support for Kaleido v0 will be removed after { ENGINE_SUPPORT_TIMELINE } .
179
259
To downgrade to Kaleido v0, run:
180
260
$ pip install kaleido<1.0.0
181
261
"""
@@ -187,10 +267,10 @@ def to_image(
187
267
img_bytes = kaleido .calc_fig_sync (
188
268
fig_dict ,
189
269
opts = dict (
190
- format = format ,
191
- width = width ,
192
- height = height ,
193
- scale = scale ,
270
+ format = format or defaults . default_format ,
271
+ width = width or defaults . default_width ,
272
+ height = height or defaults . default_height ,
273
+ scale = scale or defaults . default_scale ,
194
274
),
195
275
)
196
276
except choreographer .errors .ChromeNotFoundError :
@@ -252,30 +332,36 @@ def write_image(
252
332
If not specified and `file` is a string then this will default to the
253
333
file extension. If not specified and `file` is not a string then this
254
334
will default to:
255
- - `plotly.io.kaleido.scope .default_format` if engine is "kaleido"
256
- - `plotly.io.orca.config.default_format` if engine is "orca"
335
+ - `plotly.io.defaults .default_format` if engine is "kaleido"
336
+ - `plotly.io.orca.config.default_format` if engine is "orca" (deprecated)
257
337
258
338
width: int or None
259
339
The width of the exported image in layout pixels. If the `scale`
260
340
property is 1.0, this will also be the width of the exported image
261
341
in physical pixels.
262
342
263
- If not specified, will default to`plotly.io.kaleido.scope.default_width`
343
+ If not specified, will default to:
344
+ - `plotly.io.defaults.default_width` if engine is "kaleido"
345
+ - `plotly.io.orca.config.default_width` if engine is "orca" (deprecated)
264
346
265
347
height: int or None
266
348
The height of the exported image in layout pixels. If the `scale`
267
349
property is 1.0, this will also be the height of the exported image
268
350
in physical pixels.
269
351
270
- If not specified, will default to `plotly.io.kaleido.scope.default_height`
352
+ If not specified, will default to:
353
+ - `plotly.io.defaults.default_height` if engine is "kaleido"
354
+ - `plotly.io.orca.config.default_height` if engine is "orca" (deprecated)
271
355
272
356
scale: int or float or None
273
357
The scale factor to use when exporting the figure. A scale factor
274
358
larger than 1.0 will increase the image resolution with respect
275
359
to the figure's layout pixel dimensions. Whereas as scale factor of
276
360
less than 1.0 will decrease the image resolution.
277
361
278
- If not specified, will default to `plotly.io.kaleido.scope.default_scale`
362
+ If not specified, will default to:
363
+ - `plotly.io.defaults.default_scale` if engine is "kaleido"
364
+ - `plotly.io.orca.config.default_scale` if engine is "orca" (deprecated)
279
365
280
366
validate: bool
281
367
True if the figure should be validated before being converted to
0 commit comments