8000 Replace future flag with engine kwarg · plotly/plotly.py@7f65f5f · GitHub
[go: up one dir, main page]

Skip to content

Commit 7f65f5f

Browse files
committed
Replace future flag with engine kwarg
1 parent b546fcc commit 7f65f5f

File tree

3 files changed

+100
-41
lines changed

3 files changed

+100
-41
lines changed

packages/python/plotly/_plotly_future_/kaleido_export.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/python/plotly/plotly/io/__init__.py

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
11
from _plotly_utils.importers import relative_import
2-
from _plotly_future_ import _future_flags
32
import sys
43

54
if sys.version_info < (3, 7):
6-
if "kaleido_export" in _future_flags:
7-
from .kaleido import to_image, write_image
8-
from . import kaleido
9-
10-
extra_modules = ["kaleido"]
11-
else:
12-
from ._orca import to_image, write_image
13-
from . import orca
14-
15-
extra_modules = ["orca"]
16-
5+
from ._kaleido import to_image, write_image
6+
from . import orca
177
from ._json import to_json, from_json, read_json, write_json
188
from ._templates import templates, to_templated
199
from ._html import to_html, write_html
@@ -23,6 +13,7 @@
2313
__all__ = [
2414
"to_image",
2515
"write_image",
16+
"orca",
2617
"to_json",
2718
"from_json",
2819
"read_json",
@@ -34,19 +25,14 @@
3425
"renderers",
3526
"show",
3627
"base_renderers",
37-
] + extra_modules
28+
]
3829
else:
39-
if "kaleido_export" in _future_flags:
40-
extra_modules = [".kaleido"]
41-
extra_objects = [".kaleido.to_image", ".kaleido.write_image"]
42-
else:
43-
extra_modules = [".orca"]
44-
extra_objects = ["._orca.to_image", "._orca.write_image"]
45-
4630
__all__, __getattr__, __dir__ = relative_import(
4731
__name__,
48-
[".base_renderers"] + extra_modules,
32+
[".orca", ".base_renderers"],
4933
[
34+
"._kaleido.to_image",
35+
"._kaleido.write_image",
5036
"._json.to_json",
5137
"._json.from_json",
5238
"._json.read_json",
@@ -57,8 +43,7 @@
5743
"._html.write_html",
5844
"._renderers.renderers",
5945
"._renderers.show",
60-
]
61-
+ extra_objects,
46+
],
6247
)
6348

6449
# Set default template (for < 3.7 this is done in ploty/__init__.py)

packages/python/plotly/plotly/io/_kaleido.py

Lines changed: 92 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
1+
from __future__ import absolute_import
12
from six import string_types
23
import os
4+
import plotly
35
from plotly.io._utils import validate_coerce_fig_to_dict
46

57
try:
68
from kaleido.scopes.plotly import PlotlyScope
79

810
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"
917
except ImportError:
1018
PlotlyScope = None
1119
scope = None
1220

1321

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+
):
1525
"""
1626
Convert a figure to a static image bytes string
1727
@@ -30,44 +40,87 @@ def to_image(fig, format=None, width=None, height=None, scale=None, validate=Tru
3040
- 'eps' (Requires the poppler library to be installed and on the PATH)
3141
- 'emf' (Requires inkscape application to be installed and on the PATH)
3242
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"
3446
3547
width: int or None
3648
The width of the exported image in layout pixels. If the `scale`
3749
property is 1.0, this will also be the width of the exported image
3850
in physical pixels.
3951
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"
4155
4256
height: int or None
4357
The height of the exported image in layout pixels. If the `scale`
4458
property is 1.0, this will also be the height of the exported image
4559
in physical pixels.
4660
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"
4864
4965
scale: int or float or None
5066
The scale factor to use when exporting the figure. A scale factor
5167
larger than 1.0 will increase the image resolution with respect
5268
to the figure's layout pixel dimensions. Whereas as scale factor of
5369
less than 1.0 will decrease the image resolution.
5470
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+
5675
5776
validate: bool
5877
True if the figure should be validated before being converted to
5978
an image, False otherwise.
6079
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+
6186
Returns
6287
-------
6388
bytes
6489
The image data
6590
"""
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+
66118
# Raise informative error message if Kaleido is not installed
67119
if scope is None:
68120
raise ValueError(
69121
"""
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:
71124
$ pip install -U kaleido
72125
"""
73126
)
@@ -83,7 +136,14 @@ def to_image(fig, format=None, width=None, height=None, scale=None, validate=Tru
83136

84137

85138
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",
87147
):
88148
"""
89149
Convert a figure to a static image and write it to a file or writeable
@@ -110,39 +170,52 @@ def write_image(
110170
111171
If not specified and `file` is a string then this will default to the
112172
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"
114176
115177
width: int or None
116178
The width of the exported image in layout pixels. If the `scale`
117179
property is 1.0, this will also be the width of the exported image
118180
in physical pixels.
119181
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"
121185
122186
height: int or None
123187
The height of the exported image in layout pixels. If the `scale`
124188
property is 1.0, this will also be the height of the exported image
125189
in physical pixels.
126190
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"
128194
129195
scale: int or float or None
130196
The scale factor to use when exporting the figure. A scale factor
131197
larger than 1.0 will increase the image resolution with respect
132198
to the figure's layout pixel dimensions. Whereas as scale factor of
133199
less than 1.0 will decrease the image resolution.
134200
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"
136204
137205
validate: bool
138206
True if the figure should be validated before being converted to
139207
an image, False otherwise.
140208
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+
141215
Returns
142216
-------
143217
None
144218
"""
145-
146219
# Check if file is a string
147220
# -------------------------
148221
file_is_str = isinstance(file, string_types)
@@ -171,7 +244,13 @@ def write_image(
171244
# -------------
172245
# Do this first so we don't create a file if image conversion fails
173246
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,
175254
)
176255

177256
# Open file

0 commit comments

Comments
 (0)
0