| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
parent directory.. | ||||
Export Plotly plots to static images using WebDriver and headless browsers.
plotly_static provides a Rust interface for converting Plotly plots from JSON data into various static image formats (PNG, JPEG, WEBP, SVG, PDF) using WebDriver and headless browsers.
use plotly_static::{StaticExporterBuilder, ImageFormat};
use serde_json::json;
use std::path::Path;
// Create a simple plot as JSON
let plot = json!({
"data": [{
"type": "scatter",
"x": [1, 2, 3, 4],
"y": [10, 11, 12, 13]
}],
"layout": {
"title": "Simple Scatter Plot"
}
});
// Build and use StaticExporter
let mut exporter = StaticExporterBuilder::default()
.build()
.expect("Failed to build StaticExporter");
// Export to PNG
exporter.write_fig(
Path::new("my_plot"),
&plot,
ImageFormat::PNG,
800,
600,
1.0
).expect("Failed to export plot");Add to your Cargo.toml:
[dependencies]
plotly_static = { version = "0.1", features = ["chromedriver", "webdriver_download"] }
serde_json = "1.0"The library supports async operations. To use the async API you need to call build_async instead of build on the StaticExporterBuilder . This will return an AsyncStaticExporter instance where the write_fig and write_to_string methods are async.
use plotly_static::StaticExporterBuilder;
let exporter = StaticExporterBuilder::default()
.build_async()
.expect("Failed to build AsyncStaticExporter");Never use the sync API in async contexts. The sync API wraps the async API and uses a tokio::runtime::Runtime instance internally. Using the sync API in an async context will cause runtime errors such as e.g., "Cannot drop a runtime in a context where blocking is not allowed. This happens when a runtime is dropped from within an asynchronous context." or similar ones.
use plotly_static::{StaticExporterBuilder, ImageFormat};
use serde_json::json;
use std::path::Path;
let plot1 = json!({
"data": [{"type": "scatter", "x": [1,2,3], "y": [4,5,6]}],
"layout": {"title": "Plot 1"}
});
let plot2 = json!({
"data": [{"type": "scatter", "x": [2,3,4], "y": [5,6,7]}],
"layout": {"title": "Plot 2"}
});
let mut exporter = StaticExporterBuilder::default()
.build()
.expect("Failed to create StaticExporter");
// Reuse for multiple exports
exporter.write_fig(Path::new("plot1"), &plot1, ImageFormat::PNG, 800, 600, 1.0)?;
exporter.write_fig(Path::new("plot2"), &plot2, ImageFormat::JPEG, 800, 600, 1.0)?;use plotly_static::{StaticExporterBuilder, ImageFormat};
use serde_json::json;
let plot = json!({
"data": [{"type": "scatter", "x": [1,2,3], "y": [4,5,6]}],
"layout": {}
});
let mut exporter = StaticExporterBuilder::default()
.build()
.expect("Failed to create StaticExporter");
// Get base64 data for web embedding
let base64_data = exporter.write_to_string(&plot, ImageFormat::PNG, 400, 300, 1.0)?;
// Get SVG data (vector format)
let svg_data = exporter.write_to_string(&plot, ImageFormat::SVG, 400, 300, 1.0)?;use plotly_static::StaticExporterBuilder;
let mut exporter = StaticExporterBuilder::default()
.webdriver_port(4445) // Unique port for parallel usage
.offline_mode(true) // Use bundled JavaScript
.webdriver_browser_caps(vec![
"--headless".to_string(),
"--no-sandbox".to_string(),
])
.build()?;Check the self contained examples in the examples folder.
Similar examples are available in the Plotly.rs package, in Plotly.rs Book as well as the example in Plotly.rs examples/static_export.
This package is licensed under the MIT License.
| Back | FazBrowse Home | New Git URL |