Exporting Static Images

This documentation is for Dash Enterprise.
Dash Enterprise is the fastest way to write & deploy Dash apps and
Jupyter notebooks.
10% of the Fortune 500 uses Dash Enterprise to productionize AI and
data science apps. Find out if your company is using Dash Enterprise.

Kaleido is a library for exporting static images from Plotly figures. In this chapter, we will go over exporting static images from your Jupyter Notebook with Kaleido.

Installing Kaleido

You can install Kaleido with:

$ pip install --upgrade "plotly[kaleido]"

Kaleido V1 in Workspaces

Check which version of Kaleido you have installed by running pip show kaleido in the terminal.

Kaleido uses Chrome for image export. Versions of Kaleido before v1 included Chrome by default. With v1 and later, Chrome is no longer included.

Dash Enterprise workspaces v1.6.0 and later include Chrome and everything it requires to run. If you are using Dash Enterprise v5.4 or later in an internet-connected environment, update your workspace version to v1.6.0 or later to use Kaleido.

If you are using an earlier version of workspaces, or can’t update your workspace version, you’ll need to install Chrome and the libraries it requires, or use Kaleido v0.2.1. To use Kaleido v1 in workspaces v1.5.1 or earlier:

  1. Create a file called install_chrome_deps.sh with the following content. These are the libraries Chrome requires to run.

    ```sh
    apt-get update

    apt-get install -y \
    libnss3 \
    libatk-bridge2.0-0 \
    libcups2 \
    libxcomposite1 \
    libxdamage1 \
    libxfixes3 \
    libxrandr2 \
    libgbm1 \
    libxkbcommon0 \
    libpango-1.0-0 \
    libcairo2 \
    libasound2
    ```

  2. Create a project.toml file if your workspace doesn’t have one.

  3. Add a predeploy script to the project.toml file:
    toml [scripts] predeploy = "install_chrome_deps.sh"

  4. Rebuild the workspace.

  5. In the workspace terminal, run:
    sh plotly_get_chrome
    or install Chrome from within a notebook or Python script:
    python import plotly plotly.io.get_chrome()

Exporting Images

Plotly figures provide methods for exporting images. Use write_image to export a figure to a file.

import plotly.express as px
data_canada = px.data.gapminder().query("country == 'Canada'")
fig = px.bar(data_canada, x='year', y='pop')
fig.write_image("fig1.png")

Or use to_image to export an image to a bytes object. When using to_image, you need to specify an image format:

import plotly.express as px
data_canada = px.data.gapminder().query("country == 'Canada'")
fig = px.bar(data_canada, x='year', y='pop')
img_bytes = fig.to_image(format="png")

See the Plotly.py static image export documentation for complete documentation.

Supported Image Formats

Kaleido supports converting Plotly figures to PNG, JPG, WebP, SVG, and PDF formats.

Kaleido versions before v1 also supported EPS. To export to EPS, you will need to install the optional python-poppler library.

Troubleshooting

Additional Resources