Skip to content

Commit 6089357

Browse files
committed
add new api to reuse static exporter
Signed-off-by: Andrei Gherghescu <[email protected]>
1 parent 9f03a53 commit 6089357

File tree

11 files changed

+534
-286
lines changed

11 files changed

+534
-286
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "static_export_example"
3+
version = "0.1.0"
4+
authors = ["Andrei Gherghescu [email protected]"]
5+
edition = "2021"
6+
7+
[dependencies]
8+
plotly = { path = "../../plotly", features = ["static_export_default"] }
9+
env_logger = "0.10"
10+
log = "0.4"
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use env_logger;
2+
use log::info;
3+
use plotly::plotly_static::{ImageFormat, StaticExporterBuilder};
4+
use plotly::{Plot, Scatter};
5+
6+
fn main() -> Result<(), Box<dyn std::error::Error>> {
7+
// Initialize logging
8+
env_logger::init();
9+
10+
// Create multiple plots
11+
let mut plot1 = Plot::new();
12+
plot1.add_trace(Scatter::new(vec![1, 2, 3, 4], vec![10, 15, 13, 17]).name("trace1"));
13+
14+
let mut plot2 = Plot::new();
15+
plot2.add_trace(Scatter::new(vec![2, 3, 4, 5], vec![16, 5, 11, 9]).name("trace2"));
16+
17+
let mut plot3 = Plot::new();
18+
plot3.add_trace(Scatter::new(vec![1, 2, 3, 4], vec![12, 9, 15, 12]).name("trace3"));
19+
20+
// Create a single StaticExporter to reuse across all plots
21+
let mut exporter = StaticExporterBuilder::default()
22+
.build()
23+
.expect("Failed to create StaticExporter");
24+
25+
info!("Exporting multiple plots using a single StaticExporter...");
26+
27+
// Export all plots using the same exporter (more efficient)
28+
plot1.write_image_with_exporter(&mut exporter, "plot1", ImageFormat::PNG, 800, 600, 1.0)?;
29+
plot2.write_image_with_exporter(&mut exporter, "plot2", ImageFormat::JPEG, 800, 600, 1.0)?;
30+
plot3.write_image_with_exporter(&mut exporter, "plot3", ImageFormat::SVG, 800, 600, 1.0)?;
31+
32+
// Export to PDF
33+
plot1.write_image_with_exporter(&mut exporter, "plot1", ImageFormat::PDF, 800, 600, 1.0)?;
34+
35+
// Get base64 data
36+
let base64_data =
37+
plot1.to_base64_with_exporter(&mut exporter, ImageFormat::PNG, 400, 300, 1.0)?;
38+
info!("Base64 data length: {}", base64_data.len());
39+
40+
// Get SVG data
41+
let svg_data = plot1.to_svg_with_exporter(&mut exporter, 400, 300, 1.0)?;
42+
info!("SVG data starts with: {}", &svg_data[..50]);
43+
44+
info!("All exports completed successfully!");
45+
info!("Generated files:");
46+
info!(" - plot1.png");
47+
info!(" - plot2.jpeg");
48+
info!(" - plot3.svg");
49+
info!(" - plot1.pdf");
50+
51+
Ok(())
52+
}

plotly/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ kaleido = ["plotly_kaleido"]
1919
# DEPRECATED: kaleido_download feature will be removed in version 0.14.0. Use plotly_static_download instead.
2020
kaleido_download = ["plotly_kaleido/download"]
2121

22-
plotly_static_download = ["plotly_static/download"]
23-
plotly_static_chromedriver = ["plotly_static", "plotly_static/chromedriver"]
24-
plotly_static_geckodriver = ["plotly_static", "plotly_static/geckodriver"]
25-
plotly_static = []
22+
static_export_downloader = ["plotly_static/webdriver_download"]
23+
static_export_chromedriver = ["plotly_static", "plotly_static/chromedriver"]
24+
static_export_geckodriver = ["plotly_static", "plotly_static/geckodriver"]
25+
static_export_default = ["plotly_static", "plotly_static/geckodriver", "plotly_static/webdriver_download"]
2626

2727
plotly_ndarray = ["ndarray"]
2828
plotly_image = ["image"]

plotly/src/lib.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,26 @@
44
//!
55
//! ## Feature Deprecation Notice
66
//!
7-
//! The `kaleido` and `kaleido_download` features are deprecated since version 0.13.0
8-
//! and will be removed in version 0.14.0. Please migrate to the `plotly_static` and
9-
//! `plotly_static_download` features instead.
7+
//! The `kaleido` and `kaleido_download` features are deprecated since version
8+
//! 0.13.0 and will be removed in version 0.14.0. Please migrate to the
9+
//! `plotly_static` and `plotly_static_download` features instead.
1010
#![recursion_limit = "256"] // lets us use a large serde_json::json! macro for testing crate::layout::Axis
1111
extern crate askama;
1212
extern crate rand;
1313
extern crate serde;
1414

1515
#[cfg(feature = "kaleido")]
16-
#[deprecated(since = "0.13.0", note = "kaleido feature is deprecated and will be removed in version 0.14.0. Use plotly_static feature instead")]
16+
#[deprecated(
17+
since = "0.13.0",
18+
note = "kaleido feature is deprecated and will be removed in version 0.14.0. Use plotly_static feature instead"
19+
)]
1720
const _KALEIDO_DEPRECATED: () = ();
1821

1922
#[cfg(feature = "kaleido_download")]
20-
#[deprecated(since = "0.13.0", note = "kaleido_download feature is deprecated and will be removed in version 0.14.0. Use plotly_static_download feature instead")]
23+
#[deprecated(
24+
since = "0.13.0",
25+
note = "kaleido_download feature is deprecated and will be removed in version 0.14.0. Use plotly_static_download feature instead"
26+
)]
2127
const _KALEIDO_DOWNLOAD_DEPRECATED: () = ();
2228

2329
#[cfg(all(feature = "kaleido", target_family = "wasm"))]
@@ -51,13 +57,10 @@ pub use common::color;
5157
pub use configuration::Configuration;
5258
pub use layout::Layout;
5359
pub use plot::{Plot, Trace};
54-
5560
#[cfg(feature = "kaleido")]
5661
pub use plotly_kaleido::ImageFormat;
57-
5862
#[cfg(feature = "plotly_static")]
59-
pub use plotly_static::{StaticExporterBuilder, ImageFormat};
60-
63+
pub use plotly_static;
6164
// Also provide easy access to modules which contain additional trace-specific types
6265
pub use traces::{
6366
box_plot, contour, heat_map, histogram, image, mesh3d, sankey, scatter, scatter3d,

0 commit comments

Comments
 (0)