1
+ from __future__ import absolute_import
1
2
from six import string_types
2
3
import os
4
+ import plotly
3
5
from plotly .io ._utils import validate_coerce_fig_to_dict
4
6
5
7
try :
6
8
from kaleido .scopes .plotly import PlotlyScope
7
9
8
10
scope = PlotlyScope ()
11
+
12
+ # Compute absolute path to the 'plotly/package_data/' directory
13
+ root_dir = os .path .dirname (os .path .abspath (plotly .__file__ ))
14
+ package_dir = os .path .join (root_dir , "package_data" )
15
+ scope .plotlyjs = os .path .join (package_dir , "plotly.min.js" )
16
+ scope .mathjax = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js"
9
17
except ImportError :
10
18
PlotlyScope = None
11
19
scope = None
12
20
13
21
14
- def to_image (fig , format = None , width = None , height = None , scale = None , validate = True ):
22
+ def to_image (
23
+ fig , format = None , width = None , height = None , scale = None , validate = True , engine = "auto"
24
+ ):
15
25
"""
16
26
Convert a figure to a static image bytes string
17
27
@@ -30,44 +40,87 @@ def to_image(fig, format=None, width=None, height=None, scale=None, validate=Tru
30
40
- 'eps' (Requires the poppler library to be installed and on the PATH)
31
41
- 'emf' (Requires inkscape application to be installed and on the PATH)
32
42
33
- If not specified, will default to `plotly.io.kaleido.scope.default_format`
43
+ If not specified, will default to:
44
+ - `plotly.io.kaleido.scope.default_format` if engine is "kaleido"
45
+ - `plotly.io.orca.config.default_format` if engine is "orca"
34
46
35
47
width: int or None
36
48
The width of the exported image in layout pixels. If the `scale`
37
49
property is 1.0, this will also be the width of the exported image
38
50
in physical pixels.
39
51
40
- If not specified, will default to `plotly.io.kaleido.scope.default_width`
52
+ If not specified, will default to:
53
+ - `plotly.io.kaleido.scope.default_width` if engine is "kaleido"
54
+ - `plotly.io.orca.config.default_width` if engine is "orca"
41
55
42
56
height: int or None
43
57
The height of the exported image in layout pixels. If the `scale`
44
58
property is 1.0, this will also be the height of the exported image
45
59
in physical pixels.
46
60
47
- If not specified, will default to `plotly.io.kaleido.scope.default_height`
61
+ If not specified, will default to:
62
+ - `plotly.io.kaleido.scope.default_height` if engine is "kaleido"
63
+ - `plotly.io.orca.config.default_height` if engine is "orca"
48
64
49
65
scale: int or float or None
50
66
The scale factor to use when exporting the figure. A scale factor
51
67
larger than 1.0 will increase the image resolution with respect
52
68
to the figure's layout pixel dimensions. Whereas as scale factor of
53
69
less than 1.0 will decrease the image resolution.
54
70
55
- If not specified, will default to `plotly.io.kaleido.scope.default_scale`
71
+ If not specified, will default to:
72
+ - `plotly.io.kaleido.scope.default_scale` if engine is "kaleido"
73
+ - `plotly.io.orca.config.default_scale` if engine is "orca"
74
+
56
75
57
76
validate: bool
58
77
True if the figure should be validated before being converted to
59
78
an image, False otherwise.
60
79
80
+ engine: str
81
+ Image export engine to use:
82
+ - "kaleido": Use Kaleido for image export
83
+ - "orca": Use Orca for image export
84
+ - "auto" (default): Use Kaleido if installed, otherwise use orca
85
+
61
86
Returns
62
87
-------
63
88
bytes
64
89
The image data
65
90
"""
91
+ # Handle engine
92
+ # -------------
93
+ if engine == "auto" :
94
+ if scope is not None :
95
+ engine = "kaleido"
96
+ else :
97
+ engine = "orca"
98
+
99
+ if engine == "orca" :
100
+ # Fall back to legacy orca image export path
101
+ from ._orca import to_image as to_image_orca
102
+
103
+ return to_image_orca (
104
+ fig ,
105
+ format = format ,
106
+ width = width ,
107
+ height = height ,
108
+ scale = scale ,
109
+ validate = validate ,
110
+ )
111
+ elif engine != "kaleido" :
112
+ raise ValueError (
113
+ "Invalid image export engine specified: {engine}" .format (
114
+ engine = repr (engine )
115
+ )
116
+ )
117
+
66
118
# Raise informative error message if Kaleido is not installed
67
119
if scope is None :
68
120
raise ValueError (
69
121
"""
70
- Image export requires the kaleido package, which can be installed using pip:
122
+ Image export using the "kaleido" engine requires the kaleido package,
123
+ which can be installed using pip:
71
124
$ pip install -U kaleido
72
125
"""
73
126
)
@@ -83,7 +136,14 @@ def to_image(fig, format=None, width=None, height=None, scale=None, validate=Tru
83
136
84
137
85
138
def write_image (
86
- fig , file , format = None , scale = None , width = None , height = None , validate = True
139
+ fig ,
140
+ file ,
141
+ format = None ,
142
+ scale = None ,
143
+ width = None ,
144
+ height = None ,
145
+ validate = True ,
146
+ engine = "auto" ,
87
147
):
88
148
"""
89
149
Convert a figure to a static image and write it to a file or writeable
@@ -110,39 +170,52 @@ def write_image(
110
170
111
171
If not specified and `file` is a string then this will default to the
112
172
file extension. If not specified and `file` is not a string then this
113
- will default to `plotly.io.config.default_format`
173
+ will default to:
174
+ - `plotly.io.kaleido.scope.default_format` if engine is "kaleido"
175
+ - `plotly.io.orca.config.default_format` if engine is "orca"
114
176
115
177
width: int or None
116
178
The width of the exported image in layout pixels. If the `scale`
117
179
property is 1.0, this will also be the width of the exported image
118
180
in physical pixels.
119
181
120
- If not specified, will default to `plotly.io.config.default_width`
182
+ If not specified, will default to:
183
+ - `plotly.io.kaleido.scope.default_width` if engine is "kaleido"
184
+ - `plotly.io.orca.config.default_width` if engine is "orca"
121
185
122
186
height: int or None
123
187
The height of the exported image in layout pixels. If the `scale`
124
188
property is 1.0, this will also be the height of the exported image
125
189
in physical pixels.
126
190
127
- If not specified, will default to `plotly.io.config.default_height`
191
+ If not specified, will default to:
192
+ - `plotly.io.kaleido.scope.default_height` if engine is "kaleido"
193
+ - `plotly.io.orca.config.default_height` if engine is "orca"
128
194
129
195
scale: int or float or None
130
196
The scale factor to use when exporting the figure. A scale factor
131
197
larger than 1.0 will increase the image resolution with respect
132
198
to the figure's layout pixel dimensions. Whereas as scale factor of
133
199
less than 1.0 will decrease the image resolution.
134
200
135
- If not specified, will default to `plotly.io.config.default_scale`
201
+ If not specified, will default to:
202
+ - `plotly.io.kaleido.scope.default_scale` if engine is "kaleido"
203
+ - `plotly.io.orca.config.default_scale` if engine is "orca"
136
204
137
205
validate: bool
138
206
True if the figure should be validated before being converted to
139
207
an image, False otherwise.
140
208
209
+ engine: str
210
+ Image export engine to use:
211
+ - "kaleido": Use Kaleido for image export
212
+ - "orca": Use Orca for image export
213
+ - "auto" (default): Use Kaleido if installed, otherwise use orca
214
+
141
215
Returns
142
216
-------
143
217
None
144
218
"""
145
-
146
219
# Check if file is a string
147
220
# -------------------------
148
221
file_is_str = isinstance (file , string_types )
@@ -171,7 +244,13 @@ def write_image(
171
244
# -------------
172
245
# Do this first so we don't create a file if image conversion fails
173
246
img_data = to_image (
174
- fig , format = format , scale = scale , width = width , height = height , validate = validate
247
+ fig,
248
+ format = format ,
249
+ scale = scale ,
250
+ width = width ,
251
+ height = height ,
252
+ validate = validate ,
253
+ engine = engine ,
175
254
)
176
255
177
256
# Open file
0 commit comments