Overview
Kaleido is a cross-platform Python library for generating static images (e.g. png, svg, pdf, etc.) for Plotly.js, to be used by Plotly.py.
Installation
Kaleido can be installed from PyPI using pip:
$ pip install kaleido --upgrade
As of version 1.0.0, Kaleido requires Chrome to be installed. If you already have
Chrome on your system, Kaleido should find it; otherwise, you can install
a compatible Chrome version using the kaleido_get_chrome command:
$ kaleido_get_chrome
or function in Python:
import kaleido
kaleido.get_chrome_sync()
Migrating from v0 to v1
Kaleido v1 introduces a new API. If you're currently using v0, you'll need to make changes to your code and environment where you are running Kaleido.
- If using Kaleido v1 with Plotly.py, you will need to install Plotly.py v6.1.1 or later.
- Chrome is no longer included with Kaleido. Kaleido will look for an existing Chrome installation, but also provides commands for installing Chrome. If you don't have Chrome, you'll need to install it. See the installation section above for instructions.
- If your code uses Kaleido directly:
kaleido.scopes.plotlyhas been removed in v1. Kaleido v1 provideswrite_figandwrite_fig_syncfor exporting Plotly figures.
import kaleido
import plotly.graph_objects as go
fig = go.Figure(data=[go.Scatter(y=[1, 3, 2])])
kaleido.write_fig_sync(fig, path="figure.png")
Development guide
Below are examples of how to use Kaleido directly in your Python program.
If you want to export images of Plotly charts, it's not necessary to call Kaleido directly; you can use functions in the Plotly library. See the Plotly documentation for instructions.
Usage examples
import asyncio
import kaleido
import plotly.express as px
async def main():
# n is number of processes
async with kaleido.Kaleido(n=4, timeout=90) as k:
# fig is a plotly figure object
fig = px.scatter(x=[1, 2, 3, 4], y=[2, 1, 4, 3])
await k.write_fig(fig, path="./", opts={"format": "jpg"})
# You can also use Kaleido.write_fig_from_object, where fig_objects is
# an iterable of dicts each expanded to the write_fig arguments (fig,
# path, opts, topojson):
fig_objects = [
{
"fig": px.scatter(x=[1, 2, 3, 4], y=[2+i, 1+i, 4+i, 3+i]),
"path": f"fig_{i}.jpg",
"opts": {"format": "jpg"},
} for i in range(10)
]
await k.write_fig_from_object(fig_objects)
asyncio.run(main())
# other `kaleido.Kaleido` arguments:
# page_generator: Change library version (see PageGenerators below)
# `Kaleido.write_fig()` arguments:
# - fig: (required) A single plotly figure or an iterable.
# - path: (optional) A directory (names auto-generated based on title)
# or a single file.
# - opts: (optional) A dictionary with image options:
# `{"scale":..., "format":..., "width":..., "height":...}`
# - cancel_on_error: (optional) If False (default), errors during rendering are collected
# and returned as a tuple after all figures are attempted.
# If True, the first error is raised immediately and any
# remaining renders are cancelled.
There are shortcut functions which can be used to generate images without
creating a Kaleido() object:
import asyncio
import kaleido
# Pass `Kaleido()` constructor arguments (e.g. `n`, `timeout`) via `kopts`.
asyncio.run(
kaleido.write_fig(
fig,
path="./",
kopts={"n": 4},
)
)
Generate multiple images faster by reusing the same Chrome instance
By default, each call to kaleido.write_fig_sync, kaleido.calc_fig_sync,
or Plotly's fig.write_image() launches a fresh Chrome instance, renders
the figure, and shuts Chrome down again. If you're exporting many figures
in one script, the per-call Chrome startup and shutdown delay will
dominate runtime.
Call kaleido.start_sync_server() once at the top of your script to start
a single Chrome instance and reuse it across all subsequent sync
calls, including fig.write_image():
import kaleido
import plotly.graph_objects as go
kaleido.start_sync_server() # one-time; Chrome stays warm
for i, fig in enumerate(figures):
fig.write_image(f"fig_{i}.png")
Chrome is closed automatically when Python exits. To release it sooner
(for example, in a long-running service or Jupyter kernel), call
kaleido.stop_sync_server() explicitly.
PageGenerators
The page_generator argument takes a kaleido.PageGenerator() to customize which versions
of plotly.js, MathJax, and other scripts are used when generating an image.
If plotly is installed, kaleido defaults to the version of plotly.js contained in that package. You can pass
kaleido.PageGenerator(force_cdn=True) to force use of a CDN version of plotly (which is
the default behavior if plotly is not installed).
my_page = kaleido.PageGenerator(
plotly="https://cdn.plot.ly/plotly-3.7.0.js", # a fully qualified https:// or file:// link
mathjax=False, # False to disable, or a fully qualified link
others=["a list of other script links to include"],
)
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kaleido-1.4.0.tar.gz.
File metadata
- Download URL: kaleido-1.4.0.tar.gz
- Upload date:
- Size: 69.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.9.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b893e3e24e28f180844fcf1b0255d1bc3787377507b4669a555a74bcf1d1f92
|
|
| MD5 |
a753c4c69889909d940d0e463fdc83f7
|
|
| BLAKE2b-256 |
818fbce1fa4fb2aa30ffef9be03afd3d3dc2852e73a68e6cfe227fc478399830
|
File details
Details for the file kaleido-1.4.0-py3-none-any.whl.
File metadata
- Download URL: kaleido-1.4.0-py3-none-any.whl
- Upload date:
- Size: 56.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.9.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cd75a0b11ea64d313c2c73c58db9dc519eef9eb494830b414a8492b33108d192
|
|
| MD5 |
7ba50dec0b1092c50f12bb55ec733fd4
|
|
| BLAKE2b-256 |
aa91e3026cabbac6e4f724aab486d5593ff9cf2274b03416ac7f56064d7e0bf6
|