This example demonstrates how to use the plotly_static crate for exporting Plotly plots to static images.
The plotly_static provides a interface for converting Plotly plots into various static image formats (PNG, JPEG, WEBP, SVG, PDF) using WebDriver and headless browsers.
In this example it is shown how to use the StaticExporter with the old style Kaleido API and also with the new style API. Using the former API is fine for one time static exports, but that API will crate an instance of the StaticExporter for each write_image call. The new style API is recommended for performance as the same instance of the StaticExporter can be reused across multiple exports.
When any of the plotly static export features are enabled (static_export_chromedriver, static_export_geckodriver, or static_export_default), both StaticExporter (sync) and AsyncStaticExporter (async) are available via plotly::plotly_static. This example includes separate sync and async bins demonstrating both. Refer to the plotly_static API Documentation a more detailed description.
- Async/Sync API
- Multiple Export Formats: PNG, JPEG, SVG, PDF
- Exporter Reuse (new API): Efficient reuse of a single
StaticExporterinstance - String Export: Base64 and SVG string output for web applications
- Logging: Debug information and progress monitoring
- Error Handling: Error handling with
Resulttypes
- Browser Installation: You need either Firefox (for geckodriver) or Chrome/Chromium (for chromedriver)
- WebDriver: Chromedriver automatically downloaded with the
static_export_defaultfeature - Internet Connection: Required for WebDriver download (if using
webdriver_downloadfeature)
The example uses static_export_default which includes:
plotly_static: Core static export functionalityplotly_static/chromedriver: Chrome WebDriver supportplotly_static/webdriver_download: Automatic WebDriver download
# Use Firefox instead of Chrome/Chromium
plotly = { version = "0.13", features = ["static_export_geckodriver", "static_export_wd_download"] }
# Manual Geckodriver installation (no automatic download)
plotly = { version = "0.13", features = ["static_export_geckodriver"] }
# Manual Chromedriver installation (no automatic download)
plotly = { version = "0.13", features = ["static_export_chromedriver"] }To run the sync API example
# Basic run
cargo run --bin sync
# With debug logging
RUST_LOG=debug cargo run --bin sync
# With custom WebDriver path
WEBDRIVER_PATH=/path/to/chromedriver cargo run --bin syncTo run the async API example
# Basic run
cargo run --bin async
# With debug logging
RUST_LOG=debug cargo run --bin async
# With custom WebDriver path
WEBDRIVER_PATH=/path/to/chromedriver cargo run --bin asyncThe example generates several files:
plot1.png: Raster format, good for web/screensplot2.jpeg: Compressed raster, smaller file sizeplot3.svg: Vector format, scalable, good for webplot1.pdf: Vector format, good for printing
The example includes commented code showing advanced configuration options:
- Custom WebDriver ports for parallel usage
- Offline mode for bundled JavaScript
- Custom browser capabilities
- Explicit WebDriver spawning
- WebDriver not found: Ensure the browser is installed and WebDriver is available
- Port conflicts: Use unique ports for parallel operations
Set RUST_LOG=debug orRUST_LOG=traceto see detailed WebDriver operations and troubleshooting information.