Skip to content

Commit 827af91

Browse files
committed
Update image export documentation to recommend and describe Kaleido
1 parent f8bff56 commit 827af91

File tree

2 files changed

+104
-52
lines changed

2 files changed

+104
-52
lines changed

doc/python/orca-management.md

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,65 @@ jupyter:
3434
---
3535

3636
### Overview
37-
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.
37+
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.
3838

39-
### What is Orca?
39+
### What is orca?
4040
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.
4141

4242
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.
4343

44+
### Installing orca
45+
There are 3 general approaches to installing orca and its Python dependencies.
46+
47+
##### conda
48+
Using the [conda](https://conda.io/docs/) package manager, you can install these dependencies in a single command:
49+
```
50+
$ conda install -c plotly plotly-orca==1.2.1 psutil requests
51+
```
52+
53+
**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.
54+
55+
##### npm + pip
56+
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`:
57+
58+
```
59+
$ npm install -g electron@1.8.4 orca
60+
$ pip install psutil requests
61+
```
62+
63+
##### Standalone Binaries + pip
64+
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`.
65+
66+
```
67+
$ pip install psutil requests
68+
```
69+
70+
<!-- #region -->
71+
### Install orca on Google Colab
72+
```
73+
!pip install plotly>=4.7.1
74+
!wget https://github.com/plotly/orca/releases/download/v1.2.1/orca-1.2.1-x86_64.AppImage -O /usr/local/bin/orca
75+
!chmod +x /usr/local/bin/orca
76+
!apt-get install xvfb libgtk2.0-0 libgconf-2-4
77+
```
78+
79+
Once this is done you can use this code to make, show and export a figure:
80+
81+
```python
82+
import plotly.graph_objects as go
83+
fig = go.Figure( go.Scatter(x=[1,2,3], y=[1,3,2] ) )
84+
fig.write_image("fig1.svg")
85+
fig.write_image("fig1.png")
86+
```
87+
88+
The files can then be downloaded with:
89+
90+
```python
91+
from google.colab import files
92+
files.download('fig1.svg')
93+
files.download('fig1.png')
94+
```
95+
<!-- #endregion -->
4496

4597
### Create a Figure
4698
Now let's create a simple scatter plot with 100 random points of variying color and size.
@@ -207,4 +259,4 @@ In addition to the `executable` property, the `plotly.io.orca.config` object can
207259

208260

209261
### Saving Configuration Settings
210-
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.
262+
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.

doc/python/static-image-export.md

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,20 @@ Plotly figures are interactive when viewed in a web browser: you can hover over
4141

4242

4343
<!-- #region -->
44-
#### Install Dependencies
45-
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.
46-
47-
##### conda
48-
Using the [conda](https://conda.io/docs/) package manager, you can install these dependencies in a single command:
44+
#### Install Dependencies (Kaleido)
45+
Static image generation requires the [`kaleido`](https://github.com/plotly/Kaleido) package which can be installed using pip...
4946
```
50-
$ conda install -c plotly plotly-orca==1.2.1 psutil requests
47+
$ pip install -U kaleido
5148
```
5249

53-
**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.
54-
55-
##### npm + pip
56-
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`:
57-
58-
```
59-
$ npm install -g electron@1.8.4 orca
60-
$ pip install psutil requests
50+
or conda.
6151
```
52+
$ conda install -c conda-forge kaleido
53+
```
6254

63-
##### Standalone Binaries + pip
64-
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`.
55+
#### Install Dependencies (Orca)
56+
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.
6557

66-
```
67-
$ pip install psutil requests
68-
```
6958
<!-- #endregion -->
7059

7160
### Create a Figure
@@ -157,33 +146,6 @@ fig.write_image("images/fig1.eps")
157146

158147
**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.
159148

160-
<!-- #region -->
161-
### Install orca on Google Colab
162-
```
163-
!pip install plotly>=4.7.1
164-
!wget https://github.com/plotly/orca/releases/download/v1.2.1/orca-1.2.1-x86_64.AppImage -O /usr/local/bin/orca
165-
!chmod +x /usr/local/bin/orca
166-
!apt-get install xvfb libgtk2.0-0 libgconf-2-4
167-
```
168-
169-
Once this is done you can use this code to make, show and export a figure:
170-
171-
```python
172-
import plotly.graph_objects as go
173-
fig = go.Figure( go.Scatter(x=[1,2,3], y=[1,3,2] ) )
174-
fig.write_image("fig1.svg")
175-
fig.write_image("fig1.png")
176-
```
177-
178-
The files can then be downloaded with:
179-
180-
```python
181-
from google.colab import files
182-
files.download('fig1.svg')
183-
files.download('fig1.png')
184-
```
185-
<!-- #endregion -->
186-
187149
### Get Image as Bytes
188150
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.
189151

@@ -215,7 +177,45 @@ img_bytes = fig.to_image(format="png", width=600, height=350, scale=2)
215177
Image(img_bytes)
216178
```
217179

218-
### Summary
219-
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).
180+
<!-- #region -->
181+
### Specify Image Export Engine
182+
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.
183+
184+
Here is an example of specifying that orca should be used:
185+
```python
186+
fig.to_image(format="png", engine="orca")
187+
```
188+
189+
And, here is an example of specifying that Kaleido should be used:
190+
```python
191+
fig.to_image(format="png", engine="kaleido")
192+
```
193+
194+
<!-- #endregion -->
220195

221-
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.
196+
<!-- #region -->
197+
### Image Export Settings (Kaleido)
198+
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`
199+
200+
```python
201+
import plotly.io as pio
202+
pio.kaleido.scope.default_format = "svg"
203+
```
204+
205+
Here is a complete listing of the available image export settings:
206+
207+
- **`default_width`**: The default pixel width to use on image export.
208+
- **`default_height`**: The default pixel height to use on image export.
209+
- **`default_scale`**: The default image scale facor applied on image export.
210+
- **`default_format`**: The default image format used on export. One of `"png"`, `"jpeg"`, `"webp"`, `"svg"`, `"pdf"`, or `"eps"`.
211+
- **`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.
212+
- **`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).
213+
- **`mapbox_access_token`**: The default Mapbox access token.
214+
215+
<!-- #endregion -->
216+
217+
### Image Export Settings (Orca)
218+
See the [Orca Management](/python/orca-management/) section for information on how to specify image export settings when using orca.
219+
220+
### Summary
221+
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).

0 commit comments

Comments
 (0)