Struct StaticExporter

Source
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

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.

§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
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.

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
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.

Trait Implementations§

Source§

impl Drop for StaticExporter

Source§

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

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