pub struct StaticExporter { /* private fields */ }Expand description
Synchronous exporter for exporting Plotly plots to static images.
This object provides methods to convert Plotly JSON plots into various
static image formats using a headless browser via WebDriver.
The synchronous API is blocking and should not be used in async contexts.
Use build_async instead and the associated AsyncStaticExporter object.
Always call close when you are done with the exporter to ensure proper
cleanup of the WebDriver process.
§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");
// Close the exporter
exporter.close();§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.
The file extension is automatically added based on the format
§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();
// Creates "my_plot.png" with 1200x800 pixels at 2x scale
exporter.write_fig(
Path::new("my_plot"),
&plot,
ImageFormat::PNG,
1200,
800,
2.0
).expect("Failed to export plot");
// Close the exporter
exporter.close();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.
Renders the provided Plotly JSON plot and returns the result as a string. or an error if the export fails.
The format of the string depends on the image format. For ImageFormat::SVG the function will generate plain SVG text, for other formats it will return base64-encoded data.
§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");
// Close the exporter
exporter.close();
// svg_data contains the SVG markup as a string
assert!(svg_data.starts_with("<svg"));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.