-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Add Kaleido image export support #2613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 2 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
1c18fe4
Add kaleido image export option behind the kaleido_export future flag
jonmmease 7467329
Add CHANGELOG entry
jonmmease b546fcc
Add eps/emf formats
jonmmease 7f65f5f
Replace future flag with engine kwarg
jonmmease 556ef96
emf only available with kaleido
jonmmease ed71d6a
import kaleido submodule when plotly.io is imported
jonmmease da4cb83
Update Image renderers with engine parameter
jonmmease 3e1cbd4
Added kaleido engine tests
jonmmease 299165d
Merge remote-tracking branch 'origin/master' into kaleido
jonmmease 411ffb4
Python 2.7 mock compatibility
jonmmease de44623
Python 2.7 mock compatibility
jonmmease c5f4e48
Update CHANGELOG.md
jonmmease 4c5fe76
Add kaleido test that actually calls Kaleido and checks that the
jonmmease 011ec72
Remove broken EMF format as option
jonmmease b5fc3db
Merge remote-tracking branch 'origin/master' into kaleido
jonmmease f8bff56
Remove broken EMF format as option
jonmmease 827af91
Update image export documentation to recommend and describe Kaleido
jonmmease ef6e68d
Add engine docstring to figure image export methods
jonmmease fc7d856
Change kaleido conda channel to plotly since it most likely won't be …
jonmmease aee0e0d
Conda package renamed from kaleido -> python-kaleido
jonmmease 32f6e7f
Merge remote-tracking branch 'origin/master' into kaleido
jonmmease 00987c0
in README: indicate that Keleido is new and improved and orca is legacy
jonmmease a25f5b2
Add Kaleido note to orca-management section
jonmmease a033df0
JPEG typo
jonmmease d1fc9a8
Merge "Install Dependency" sections and better explain that Kaleido i…
jonmmease 2bdcfe2
Replace Orca with plotly.py when discussing supported image export fo…
jonmmease 70c11ac
varying type
jonmmease e350025
factor typo [ci skip]
jonmmease 967b728
Update CHANGELOG.md
jonmmease 81a0bc7
Update doc/python/static-image-export.md
jonmmease 74c4274
Update doc/python/orca-management.md
jonmmease File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from __future__ import absolute_import | ||
from _plotly_future_ import _future_flags, _assert_plotly_not_imported | ||
|
||
_assert_plotly_not_imported() | ||
_future_flags.add("kaleido_export") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
from six import string_types | ||
import os | ||
from plotly.io._utils import validate_coerce_fig_to_dict | ||
|
||
try: | ||
from kaleido.scopes.plotly import PlotlyScope | ||
|
||
scope = PlotlyScope() | ||
except ImportError: | ||
PlotlyScope = None | ||
scope = None | ||
|
||
|
||
def to_image(fig, format=None, width=None, height=None, scale=None, validate=True): | ||
""" | ||
Convert a figure to a static image bytes string | ||
|
||
Parameters | ||
---------- | ||
fig: | ||
Figure object or dict representing a figure | ||
|
||
format: str or None | ||
The desired image format. One of | ||
- 'png' | ||
- 'jpg' or 'jpeg' | ||
- 'webp' | ||
- 'svg' | ||
- 'pdf' | ||
|
||
If not specified, will default to `plotly.io.kaleido.scope.default_format` | ||
|
||
width: int or None | ||
The width of the exported image in layout pixels. If the `scale` | ||
property is 1.0, this will also be the width of the exported image | ||
in physical pixels. | ||
|
||
If not specified, will default to `plotly.io.kaleido.scope.default_width` | ||
|
||
height: int or None | ||
The height of the exported image in layout pixels. If the `scale` | ||
property is 1.0, this will also be the height of the exported image | ||
in physical pixels. | ||
|
||
If not specified, will default to `plotly.io.kaleido.scope.default_height` | ||
|
||
scale: int or float or None | ||
The scale factor to use when exporting the figure. A scale factor | ||
larger than 1.0 will increase the image resolution with respect | ||
to the figure's layout pixel dimensions. Whereas as scale factor of | ||
less than 1.0 will decrease the image resolution. | ||
|
||
If not specified, will default to `plotly.io.kaleido.scope.default_scale` | ||
|
||
validate: bool | ||
True if the figure should be validated before being converted to | ||
an image, False otherwise. | ||
|
||
Returns | ||
------- | ||
bytes | ||
The image data | ||
""" | ||
# Raise informative error message if Kaleido is not installed | ||
if scope is None: | ||
raise ValueError( | ||
""" | ||
Image export requires the kaleido package, which can be installed using pip: | ||
$ pip install -U kaleido | ||
""" | ||
) | ||
|
||
# Validate figure | ||
# --------------- | ||
fig_dict = validate_coerce_fig_to_dict(fig, validate) | ||
img_bytes = scope.transform( | ||
fig_dict, format=format, width=width, height=height, scale=scale | ||
) | ||
|
||
return img_bytes | ||
|
||
|
||
def write_image( | ||
fig, file, format=None, scale=None, width=None, height=None, validate=True | ||
): | ||
""" | ||
Convert a figure to a static image and write it to a file or writeable | ||
object | ||
|
||
Parameters | ||
---------- | ||
fig: | ||
Figure object or dict representing a figure | ||
|
||
file: str or writeable | ||
A string representing a local file path or a writeable object | ||
(e.g. an open file descriptor) | ||
|
||
format: str or None | ||
The desired image format. One of | ||
- 'png' | ||
- 'jpg' or 'jpeg' | ||
- 'webp' | ||
- 'svg' | ||
- 'pdf' | ||
- 'eps' (Requires the poppler library to be installed) | ||
|
||
If not specified and `file` is a string then this will default to the | ||
file extension. If not specified and `file` is not a string then this | ||
will default to `plotly.io.config.default_format` | ||
|
||
width: int or None | ||
The width of the exported image in layout pixels. If the `scale` | ||
property is 1.0, this will also be the width of the exported image | ||
in physical pixels. | ||
|
||
If not specified, will default to `plotly.io.config.default_width` | ||
|
||
height: int or None | ||
The height of the exported image in layout pixels. If the `scale` | ||
property is 1.0, this will also be the height of the exported image | ||
in physical pixels. | ||
|
||
If not specified, will default to `plotly.io.config.default_height` | ||
|
||
scale: int or float or None | ||
The scale factor to use when exporting the figure. A scale factor | ||
larger than 1.0 will increase the image resolution with respect | ||
to the figure's layout pixel dimensions. Whereas as scale factor of | ||
less than 1.0 will decrease the image resolution. | ||
|
||
If not specified, will default to `plotly.io.config.default_scale` | ||
|
||
validate: bool | ||
True if the figure should be validated before being converted to | ||
an image, False otherwise. | ||
|
||
Returns | ||
------- | ||
None | ||
""" | ||
|
||
# Check if file is a string | ||
# ------------------------- | ||
file_is_str = isinstance(file, string_types) | ||
|
||
# Infer format if not specified | ||
# ----------------------------- | ||
if file_is_str and format is None: | ||
_, ext = os.path.splitext(file) | ||
if ext: | ||
format = ext.lstrip(".") | ||
else: | ||
raise ValueError( | ||
""" | ||
Cannot infer image type from output path '{file}'. | ||
Please add a file extension or specify the type using the format parameter. | ||
For example: | ||
|
||
>>> import plotly.io as pio | ||
>>> pio.write_image(fig, file_path, format='png') | ||
""".format( | ||
file=file | ||
) | ||
) | ||
|
||
# Request image | ||
# ------------- | ||
# Do this first so we don't create a file if image conversion fails | ||
img_data = to_image( | ||
fig, format=format, scale=scale, width=width, height=height, validate=validate | ||
) | ||
|
||
# Open file | ||
# --------- | ||
if file_is_str: | ||
with open(file, "wb") as f: | ||
f.write(img_data) | ||
else: | ||
file.write(img_data) | ||
|
||
|
||
__all__ = ["to_image", "write_image", "scope"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from ._kaleido import to_image, write_image, scope |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.