-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Add Kaleido image export support #2613
Merged
Changes from all 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 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 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 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 |
---|---|---|
|
@@ -34,16 +34,72 @@ jupyter: | |
--- | ||
|
||
### Overview | ||
This section covers the lower-level details of how plotly.py uses orca to perform static image generation. Please refer to the [Static Image Export](/python/static-image-export/) section for general information on creating static images from plotly.py figures. | ||
This section covers the lower-level details of how plotly.py can use orca to perform static image generation. | ||
|
||
### What is Orca? | ||
> Orca is no longer the recommended way to do static image export. We now recommend Kaleido, as described in the [Static Image Export](/python/static-image-export/) section . | ||
|
||
Please refer to the [Static Image Export](/python/static-image-export/) section for general information on creating static images from plotly.py figures. | ||
|
||
### What is orca? | ||
Orca is an [Electron](https://electronjs.org/) application that inputs plotly figure specifications and converts them into static images. Orca can run as a command-line utility or as a long-running server process. In order to provide the fastest possible image export experience, plotly.py launches orca in server mode, and communicates with it over a local port. See https://github.com/plotly/orca for more information. | ||
|
||
By default, plotly.py launches the orca server process the first time an image export operation is performed, and then leaves it running until the main Python process exits. Because of this, the first image export operation in an interactive session will typically take a couple of seconds, but then all subsequent export operations will be significantly faster, since the server is already running. | ||
|
||
### Installing orca | ||
There are 3 general approaches to installing orca and its Python dependencies. | ||
|
||
##### conda | ||
Using the [conda](https://conda.io/docs/) package manager, you can install these dependencies in a single command: | ||
``` | ||
$ conda install -c plotly plotly-orca==1.2.1 psutil requests | ||
``` | ||
|
||
**Note:** Even if you do not want to use conda to manage your Python dependencies, it is still useful as a cross platform tool for managing native libraries and command-line utilities (e.g. git, wget, graphviz, boost, gcc, nodejs, cairo, etc.). For this use-case, start with [Miniconda](https://conda.io/miniconda.html) (~60MB) and tell the installer to add itself to your system `PATH`. Then run `conda install plotly-orca==1.2.1` and the orca executable will be available system wide. | ||
|
||
##### npm + pip | ||
You can use the [npm](https://www.npmjs.com/get-npm) package manager to install `orca` (and its `electron` dependency), and then use pip to install `psutil`: | ||
|
||
``` | ||
$ npm install -g [email protected] orca | ||
$ pip install psutil requests | ||
``` | ||
|
||
##### Standalone Binaries + pip | ||
If you are unable to install conda or npm, you can install orca as a precompiled binary for your operating system. Follow the instructions in the orca [README](https://github.com/plotly/orca) to install orca and add it to your system `PATH`. Then use pip to install `psutil`. | ||
|
||
``` | ||
$ pip install psutil requests | ||
``` | ||
|
||
<!-- #region --> | ||
### Install orca on Google Colab | ||
``` | ||
!pip install plotly>=4.7.1 | ||
!wget https://github.com/plotly/orca/releases/download/v1.2.1/orca-1.2.1-x86_64.AppImage -O /usr/local/bin/orca | ||
!chmod +x /usr/local/bin/orca | ||
!apt-get install xvfb libgtk2.0-0 libgconf-2-4 | ||
``` | ||
|
||
Once this is done you can use this code to make, show and export a figure: | ||
|
||
```python | ||
import plotly.graph_objects as go | ||
fig = go.Figure( go.Scatter(x=[1,2,3], y=[1,3,2] ) ) | ||
fig.write_image("fig1.svg") | ||
fig.write_image("fig1.png") | ||
``` | ||
|
||
The files can then be downloaded with: | ||
|
||
```python | ||
from google.colab import files | ||
files.download('fig1.svg') | ||
files.download('fig1.png') | ||
``` | ||
<!-- #endregion --> | ||
|
||
### Create a Figure | ||
Now let's create a simple scatter plot with 100 random points of variying color and size. | ||
Now let's create a simple scatter plot with 100 random points of varying color and size. | ||
|
||
```python | ||
import plotly.graph_objects as go | ||
|
@@ -198,7 +254,7 @@ In addition to the `executable` property, the `plotly.io.orca.config` object can | |
- **`timeout`**: The number of seconds of inactivity required before the orca server is shut down. For example, if timeout is set to 20, then the orca server will shutdown once is has not been used for at least 20 seconds. If timeout is set to `None` (the default), then the server will not be automatically shut down due to inactivity. | ||
- **`default_width`**: The default pixel width to use on image export. | ||
- **`default_height`**: The default pixel height to use on image export. | ||
- **`default_scale`**: The default image scale facor applied on image export. | ||
- **`default_scale`**: The default image scale factor applied on image export. | ||
- **`default_format`**: The default image format used on export. One of `"png"`, `"jpeg"`, `"webp"`, `"svg"`, `"pdf"`, or `"eps"`. | ||
- **`mathjax`**: Location of the MathJax bundle needed to render LaTeX characters. Defaults to a CDN location. If fully offline export is required, set this to a local MathJax bundle. | ||
- **`topojson`**: Location of the topojson files needed to render choropleth traces. Defaults to a CDN location. If fully offline export is required, set this to a local directory containing the [Plotly.js topojson files](https://github.com/plotly/plotly.js/tree/master/dist/topojson). | ||
|
@@ -207,4 +263,4 @@ In addition to the `executable` property, the `plotly.io.orca.config` object can | |
|
||
|
||
### Saving Configuration Settings | ||
Configuration options can optionally be saved to the `~/.plotly/` directory by calling the `plotly.io.config.save()` method. Saved setting will be automatically loaded at the start of future sessions. | ||
Configuration options can optionally be saved to the `~/.plotly/` directory by calling the `plotly.io.config.save()` method. Saved setting will be automatically loaded at the start of future sessions. |
This file contains 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 |
---|---|---|
|
@@ -37,39 +37,27 @@ jupyter: | |
|
||
### Interactive vs Static Export | ||
|
||
Plotly figures are interactive when viewed in a web browser: you can hover over data points, pan and zoom axes, and show and hide traces by clicking or double-clicking on the legend. You can export figures either to static image file formats like PNG, JEPG, SVG or PDF or you can [export them to HTML files which can be opened in a browser and remain interactive](/python/interactive-html-export/). This page explains how to do the former. | ||
Plotly figures are interactive when viewed in a web browser: you can hover over data points, pan and zoom axes, and show and hide traces by clicking or double-clicking on the legend. You can export figures either to static image file formats like PNG, JPEG, SVG or PDF or you can [export them to HTML files which can be opened in a browser and remain interactive](/python/interactive-html-export/). This page explains how to do the former. | ||
|
||
|
||
<!-- #region --> | ||
#### Install Dependencies | ||
Static image generation requires the [orca](https://github.com/plotly/orca) commandline utility and the [psutil](https://github.com/giampaolo/psutil) and [requests](https://2.python-requests.org/en/master/) Python libraries. There are 3 general approach to installing these dependencies. | ||
|
||
##### conda | ||
Using the [conda](https://conda.io/docs/) package manager, you can install these dependencies in a single command: | ||
Static image generation requires either [Kaleido](https://github.com/plotly/Kaleido) (recommended) or [orca](https://github.com/plotly/orca) (legacy). The `kaleido` package can be installed using pip... | ||
``` | ||
$ conda install -c plotly plotly-orca==1.2.1 psutil requests | ||
$ pip install -U kaleido | ||
``` | ||
|
||
**Note:** Even if you do not want to use conda to manage your Python dependencies, it is still useful as a cross platform tool for managing native libraries and command-line utilities (e.g. git, wget, graphviz, boost, gcc, nodejs, cairo, etc.). For this use-case, start with [Miniconda](https://conda.io/miniconda.html) (~60MB) and tell the installer to add itself to your system `PATH`. Then run `conda install plotly-orca==1.2.1` and the orca executable will be available system wide. | ||
|
||
##### npm + pip | ||
You can use the [npm](https://www.npmjs.com/get-npm) package manager to install `orca` (and its `electron` dependency), and then use pip to install `psutil`: | ||
|
||
``` | ||
$ npm install -g [email protected] orca | ||
$ pip install psutil requests | ||
or conda. | ||
``` | ||
$ conda install -c plotly python-kaleido | ||
``` | ||
|
||
##### Standalone Binaries + pip | ||
If you are unable to install conda or npm, you can install orca as a precompiled binary for your operating system. Follow the instructions in the orca [README](https://github.com/plotly/orca) to install orca and add it to your system `PATH`. Then use pip to install `psutil`. | ||
While Kaleido is now the recommended approach, image export can also be supported by the legacy [orca](https://github.com/plotly/orca) command line utility. See the [Orca Management](/python/orca-management/) section for instructions on installing, configuring, and troubleshooting orca. | ||
|
||
``` | ||
$ pip install psutil requests | ||
``` | ||
<!-- #endregion --> | ||
|
||
### Create a Figure | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line 61 should be "Now let's create a simple scatter plot with 100 random points of varying color and size." There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 70c11ac |
||
Now let's create a simple scatter plot with 100 random points of variying color and size. | ||
Now let's create a simple scatter plot with 100 random points of varying color and size. | ||
|
||
```python | ||
import plotly.graph_objects as go | ||
|
@@ -116,7 +104,7 @@ If you are running this notebook live, click to [open the output directory](./im | |
#### Raster Formats: PNG, JPEG, and WebP | ||
|
||
|
||
Orca can output figures to several raster image formats including **PNG**, ... | ||
plotly.py can output figures to several raster image formats including **PNG**, ... | ||
|
||
```python | ||
fig.write_image("images/fig1.png") | ||
|
@@ -137,7 +125,7 @@ fig.write_image("images/fig1.webp") | |
#### Vector Formats: SVG and PDF... | ||
|
||
|
||
Orca can also output figures in several vector formats including **SVG**, ... | ||
plotly.py can also output figures in several vector formats including **SVG**, ... | ||
|
||
```python | ||
fig.write_image("images/fig1.svg") | ||
|
@@ -157,33 +145,6 @@ fig.write_image("images/fig1.eps") | |
|
||
**Note:** It is important to note that any figures containing WebGL traces (i.e. of type `scattergl`, `heatmapgl`, `contourgl`, `scatter3d`, `surface`, `mesh3d`, `scatterpolargl`, `cone`, `streamtube`, `splom`, or `parcoords`) that are exported in a vector format will include encapsulated rasters, instead of vectors, for some parts of the image. | ||
|
||
<!-- #region --> | ||
### Install orca on Google Colab | ||
``` | ||
!pip install plotly>=4.7.1 | ||
!wget https://github.com/plotly/orca/releases/download/v1.2.1/orca-1.2.1-x86_64.AppImage -O /usr/local/bin/orca | ||
!chmod +x /usr/local/bin/orca | ||
!apt-get install xvfb libgtk2.0-0 libgconf-2-4 | ||
``` | ||
|
||
Once this is done you can use this code to make, show and export a figure: | ||
|
||
```python | ||
import plotly.graph_objects as go | ||
fig = go.Figure( go.Scatter(x=[1,2,3], y=[1,3,2] ) ) | ||
fig.write_image("fig1.svg") | ||
fig.write_image("fig1.png") | ||
``` | ||
|
||
The files can then be downloaded with: | ||
|
||
```python | ||
from google.colab import files | ||
files.download('fig1.svg') | ||
files.download('fig1.png') | ||
``` | ||
<!-- #endregion --> | ||
|
||
### Get Image as Bytes | ||
The `plotly.io.to_image` function is used to return an image as a bytes object. You can also use the `.to_image` graph object figure method. | ||
|
||
|
@@ -215,7 +176,45 @@ img_bytes = fig.to_image(format="png", width=600, height=350, scale=2) | |
Image(img_bytes) | ||
``` | ||
|
||
### Summary | ||
In summary, to export high-quality static images from plotly.py, all you need to do is install orca, psutil, and requests and then use the `plotly.io.write_image` and `plotly.io.to_image` functions (or the `.write_image` and `.to_image` graph object figure methods). | ||
<!-- #region --> | ||
### Specify Image Export Engine | ||
If `kaleido` is installed, it will automatically be used to perform image export. If it is not installed, plotly.py will attempt to use orca instead. The `engine` argument to the `to_image` and `write_image` functions can be used to override this default behavior. | ||
|
||
Here is an example of specifying that orca should be used: | ||
```python | ||
fig.to_image(format="png", engine="orca") | ||
``` | ||
|
||
And, here is an example of specifying that Kaleido should be used: | ||
```python | ||
fig.to_image(format="png", engine="kaleido") | ||
``` | ||
|
||
<!-- #endregion --> | ||
|
||
If you want to know more about how the orca integration works, or if you need to troubleshoot an issue, please check out the [Orca Management](/python/orca-management/) section. | ||
<!-- #region --> | ||
### Image Export Settings (Kaleido) | ||
Various image export settings can be configured using the `plotly.io.kaleido.scope` object. For example, the `default_format` property can be used to specify that the default export format should be `svg` instead of `png` | ||
|
||
```python | ||
import plotly.io as pio | ||
pio.kaleido.scope.default_format = "svg" | ||
``` | ||
|
||
Here is a complete listing of the available image export settings: | ||
|
||
- **`default_width`**: The default pixel width to use on image export. | ||
- **`default_height`**: The default pixel height to use on image export. | ||
- **`default_scale`**: The default image scale factor applied on image export. | ||
- **`default_format`**: The default image format used on export. One of `"png"`, `"jpeg"`, `"webp"`, `"svg"`, `"pdf"`, or `"eps"`. | ||
- **`mathjax`**: Location of the MathJax bundle needed to render LaTeX characters. Defaults to a CDN location. If fully offline export is required, set this to a local MathJax bundle. | ||
- **`topojson`**: Location of the topojson files needed to render choropleth traces. Defaults to a CDN location. If fully offline export is required, set this to a local directory containing the [Plotly.js topojson files](https://github.com/plotly/plotly.js/tree/master/dist/topojson). | ||
- **`mapbox_access_token`**: The default Mapbox access token. | ||
|
||
<!-- #endregion --> | ||
|
||
### Image Export Settings (Orca) | ||
See the [Orca Management](/python/orca-management/) section for information on how to specify image export settings when using orca. | ||
|
||
### Summary | ||
In summary, to export high-quality static images from plotly.py, all you need to do is install the `kaleido` package and then use the `plotly.io.write_image` and `plotly.io.to_image` functions (or the `.write_image` and `.to_image` graph object figure methods). |
This file contains 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 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line 253 should be
- **
default_scale**: The default image scale factor applied on image export.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 8731ecfe3402002d1eac53e74e7d7ce821b2c821