pub struct StaticExporter { /* private fields */ }Expand description
Main struct for exporting Plotly plots to static images.
This struct provides methods to convert Plotly JSON plots into various static image formats using a headless browser via WebDriver.
§Examples
// This example requires a running WebDriver (chromedriver/geckodriver) and a browser.
// It cannot be run as a doc test.
use plotly_static::{StaticExporterBuilder, ImageFormat};
use serde_json::json;
use std::path::Path;
// Create a simple plot
let plot = json!({
"data": [{
"type": "scatter",
"x": [1, 2, 3],
"y": [4, 5, 6]
}],
"layout": {}
});
// Build StaticExporter instance
let mut exporter = StaticExporterBuilder::default()
.build()
.expect("Failed to build StaticExporter");
// Export to PNG
exporter.write_fig(
Path::new("output"),
&plot,
ImageFormat::PNG,
800,
600,
1.0
).expect("Failed to export plot");§Features
- Supports multiple image formats (PNG, JPEG, WEBP, SVG, PDF)
- Uses headless browser for rendering
- Configurable dimensions and scale
- Offline mode support
- Automatic WebDriver management
Implementations§
Source§impl StaticExporter
impl StaticExporter
Sourcepub fn write_fig(
&mut self,
dst: &Path,
plot: &Value,
format: ImageFormat,
width: usize,
height: usize,
scale: f64,
) -> Result<(), Box<dyn Error>>
pub fn write_fig( &mut self, dst: &Path, plot: &Value, format: ImageFormat, width: usize, height: usize, scale: f64, ) -> Result<(), Box<dyn Error>>
Exports a Plotly plot to a static image file.
This method renders the provided Plotly JSON plot using a headless browser and saves the result as an image file in the specified format.
Returns Ok(()) on success, or an error if the export fails.
§Examples
// This example requires a running WebDriver (chromedriver/geckodriver) and a browser.
// It cannot be run as a doc test.
use plotly_static::{StaticExporterBuilder, ImageFormat};
use serde_json::json;
use std::path::Path;
let plot = json!({
"data": [{"type": "scatter", "x": [1,2,3], "y": [4,5,6]}],
"layout": {}
});
let mut exporter = StaticExporterBuilder::default().build().unwrap();
exporter.write_fig(
Path::new("my_plot"),
&plot,
ImageFormat::PNG,
1200,
800,
2.0
).expect("Failed to export plot");
// Creates "my_plot.png" with 1200x800 pixels at 2x scale§Notes
- The file extension is automatically added based on the format
- SVG format outputs plain text, others output binary data
- PDF format uses browser JavaScript for generation
Sourcepub fn write_to_string(
&mut self,
plot: &Value,
format: ImageFormat,
width: usize,
height: usize,
scale: f64,
) -> Result<String, Box<dyn Error>>
pub fn write_to_string( &mut self, plot: &Value, format: ImageFormat, width: usize, height: usize, scale: f64, ) -> Result<String, Box<dyn Error>>
Exports a Plotly plot to a string representation.
This method renders the provided Plotly JSON plot and returns the result as a string. The format of the string depends on the image format:
- SVG: Returns plain SVG text
- PNG/JPEG/WEBP/PDF: Returns base64-encoded data
Returns the image data as a string on success, or an error if the export fails.
§Examples
// This example requires a running WebDriver (chromedriver/geckodriver) and a browser.
// It cannot be run as a doc test.
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().unwrap();
let svg_data = exporter.write_to_string(
&plot,
ImageFormat::SVG,
800,
600,
1.0
).expect("Failed to export plot");
// svg_data contains the SVG markup as a string
assert!(svg_data.starts_with("<svg"));§Notes
- SVG format returns plain text that can be embedded in HTML
- Other formats return base64-encoded data that can be used in data URLs
- This method is useful when you need the image data as a string rather than a file
Sourcepub fn get_webdriver_diagnostics(&self) -> String
pub fn get_webdriver_diagnostics(&self) -> String
Get diagnostic information about the underlying WebDriver process.
This method provides detailed information about the WebDriver process for debugging purposes, including process status, port information, and connection details.
Trait Implementations§
Source§impl Drop for StaticExporter
impl Drop for StaticExporter
Source§fn drop(&mut self)
fn drop(&mut self)
Automatically cleans up WebDriver resources when the StaticExporter
instance is dropped.
This ensures that the WebDriver process is properly terminated and resources are released, even if the instance goes out of scope unexpectedly.
- Only terminates WebDriver processes that were spawned by this instance
- Leaves externally managed WebDriver sessions running
- Logs errors but doesn’t panic if cleanup fails