Struct StaticExporter

Source
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

Source

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();
Source

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"));
Source

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.

Source

pub fn close(&mut self)

Explicitly close the WebDriver session and stop the driver.

Always call close to ensure proper cleanup.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T