-
-
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
Changes from 21 commits
1c18fe4
7467329
b546fcc
7f65f5f
556ef96
ed71d6a
da4cb83
3e1cbd4
299165d
411ffb4
de44623
c5f4e48
4c5fe76
011ec72
b5fc3db
f8bff56
827af91
ef6e68d
fc7d856
aee0e0d
32f6e7f
00987c0
a25f5b2
a033df0
d1fc9a8
2bdcfe2
70c11ac
e350025
967b728
81a0bc7
74c4274
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -133,12 +133,27 @@ jupyter labextension install @jupyter-widgets/jupyterlab-manager plotlywidget@4. | |
|
||
Please check out our [Troubleshooting guide](https://plotly.com/python/troubleshooting/) if you run into any problems with JupyterLab. | ||
|
||
### Static Image Export | ||
### Static Image Export with Kaleido | ||
|
||
plotly.py supports static image export using the `to_image` and `write_image` | ||
functions in the `plotly.io` package. This functionality requires the | ||
installation of the plotly [orca](https://github.com/plotly/orca) command line utility and the | ||
[`psutil`](https://github.com/giampaolo/psutil) Python package. | ||
functions in the `plotly.io` module. This functionality requires the | ||
[`kaleido`](https://github.com/plotly/Kaleido) package which can be installed | ||
using pip... | ||
|
||
``` | ||
$ pip install -U kaleido | ||
``` | ||
|
||
or conda. | ||
``` | ||
$ conda install -c plotly python-kaleido | ||
``` | ||
|
||
### Static Image Export with Orca | ||
|
||
While Kaleido is now the recommended approach, image export can also be supported | ||
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. I would mention that Orca is the older, harder to install option. 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. Update in 00987c0 |
||
by the [orca](https://github.com/plotly/orca) command line utility and the | ||
[`psutil`](https://github.com/giampaolo/psutil) Python package. | ||
|
||
These dependencies can both be installed using conda: | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -34,13 +34,65 @@ 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. Please refer to the [Static Image Export](/python/static-image-export/) section for general information on creating static images from plotly.py figures. | ||||||
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. I would add a section above this saying "Orca is no longer the recommended way to do static export, and we now recommend Kaleido" with a link back to the static image export page. 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. Update in a25f5b2 |
||||||
|
||||||
### What is Orca? | ||||||
### 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. | ||||||
jonmmease marked this conversation as resolved.
Show resolved
Hide resolved
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.
Suggested change
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. done |
||||||
|
@@ -207,4 +259,4 @@ In addition to the `executable` property, the `plotly.io.orca.config` object can | |||||
|
||||||
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 253 should be 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 8731ecfe3402002d1eac53e74e7d7ce821b2c821 |
||||||
|
||||||
### 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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,31 +41,20 @@ Plotly figures are interactive when viewed in a web browser: you can hover over | |
|
||
|
||
<!-- #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: | ||
#### Install Dependencies (Kaleido) | ||
Static image generation requires the [`kaleido`](https://github.com/plotly/Kaleido) package which can be installed using pip... | ||
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. strictly speaking this isn't correct, it requires "either Kaleido (recommended) or Orca (deprecated)" or something like that, no? Just to avoid confusing people who already know Orca? (same comment applies in other places in this PR where we've said Kaleido is required) 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. It's not in the diffs but under 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. I can't select it but on line 40 JEPG should be JPEG. 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. |
||
``` | ||
$ 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`. | ||
#### Install Dependencies (Orca) | ||
While Kaleido is now the recommended approach, image export can also be supported by the [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 |
||
|
@@ -157,33 +146,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 +177,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 facor applied on image export. | ||
jonmmease marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- **`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). |
Uh oh!
There was an error while loading. Please reload this page.