Skip to content

Commit 80a5fd0

Browse files
committed
refactor
Signed-off-by: Andrei Gherghescu <8067229+andrei-ng@users.noreply.github.com>
1 parent 252aeb4 commit 80a5fd0

File tree

10 files changed

+430
-241
lines changed

10 files changed

+430
-241
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ let svg_string = plot.to_svg(800, 600, 1.0)?;
121121

122122
**Note:** This feature requires a WebDriver-compatible browser (Chrome or Firefox) as well as a Webdriver (chromedriver/geckodriver) to be available on the system. For advanced usage, see the [`plotly_static` crate documentation](https://docs.rs/plotly_static/).
123123

124+
> Enabling any of the `plotly` features `static_export_chromedriver`, `static_export_geckodriver`, or `static_export_default` gives access to both the synchronous `StaticExporter` and the asynchronous `AsyncStaticExporter` (available via `plotly::plotly_static`). Use the async API in async contexts; avoid calling the sync API from async code.
125+
124126
## Exporting Static Images with Kaleido (legacy)
125127

126128
Enable the `kaleido` feature and opt in for automatic downloading of the `kaleido` binaries by doing the following

docs/book/src/fundamentals/static_image_export.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ plotly = { version = "0.13", features = ["static_export_chromedriver", "static_e
3434
plotly = { version = "0.13", features = ["static_export_default"] }
3535
```
3636

37+
> Enabling any of the static export features in `plotly` (`static_export_chromedriver`, `static_export_geckodriver`, or `static_export_default`) enables both APIs from `plotly_static`: the sync `StaticExporter` and the async `AsyncStaticExporter` (reachable as `plotly::plotly_static::AsyncStaticExporter`). Prefer the async API inside async code.
38+
3739
## Prerequisites
3840

3941
1. **WebDriver Installation**: You need either chromedriver or geckodriver installed
@@ -74,6 +76,7 @@ For better performance when exporting multiple plots, reuse a single `StaticExpo
7476
```rust
7577
use plotly::{Plot, Scatter};
7678
use plotly::plotly_static::{StaticExporterBuilder, ImageFormat};
79+
use plotly::prelude::*;
7780

7881
let mut plot1 = Plot::new();
7982
plot1.add_trace(Scatter::new(vec![1, 2, 3], vec![4, 5, 6]));
@@ -87,9 +90,9 @@ let mut exporter = StaticExporterBuilder::default()
8790
.expect("Failed to create StaticExporter");
8891

8992
// Export multiple plots using the same exporter
90-
plot1.write_image_with_exporter(&mut exporter, "plot1", ImageFormat::PNG, 800, 600, 1.0)
93+
exporter.write_image(&plot1, "plot1", ImageFormat::PNG, 800, 600, 1.0)
9194
.expect("Failed to export plot1");
92-
plot2.write_image_with_exporter(&mut exporter, "plot2", ImageFormat::JPEG, 800, 600, 1.0)
95+
exporter.write_image(&plot2, "plot2", ImageFormat::JPEG, 800, 600, 1.0)
9396
.expect("Failed to export plot2");
9497

9598
// Always close the exporter to ensure proper release of WebDriver resources
@@ -117,6 +120,7 @@ For web applications or APIs, you can export to strings:
117120
```rust
118121
use plotly::{Plot, Scatter};
119122
use plotly::plotly_static::{StaticExporterBuilder, ImageFormat};
123+
use plotly::prelude::*;
120124

121125
let mut plot = Plot::new();
122126
plot.add_trace(Scatter::new(vec![1, 2, 3], vec![4, 5, 6]));
@@ -126,11 +130,11 @@ let mut exporter = StaticExporterBuilder::default()
126130
.expect("Failed to create StaticExporter");
127131

128132
// Get base64 data (useful for embedding in HTML)
129-
let base64_data = plot.to_base64_with_exporter(&mut exporter, ImageFormat::PNG, 400, 300, 1.0)
133+
let base64_data = exporter.to_base64(&plot, ImageFormat::PNG, 400, 300, 1.0)
130134
.expect("Failed to export plot");
131135

132136
// Get SVG data (vector format, scalable)
133-
let svg_data = plot.to_svg_with_exporter(&mut exporter, 400, 300, 1.0)
137+
let svg_data = exporter.to_svg(&plot, 400, 300, 1.0)
134138
.expect("Failed to export plot");
135139

136140
// Always close the exporter to ensure proper release of WebDriver resources
@@ -191,7 +195,9 @@ exporter.close();
191195

192196
### Async support
193197

194-
`plotly_static` package offers an `async` API which can is exposed in `plotly` as well but only if the user passes an `AsyncStaticExporter` to the `write_image_async`, `to_base64_async` and `to_svg_async` functions. The `AsyncStaticExporter` can be built using the `StaticExportBuilder`'s `build_async` method.
198+
`plotly_static` package offers an `async` API which is exposed in `plotly` via the `write_image_async`, `to_base64_async` and `to_svg_async` functions. However, the user must pass an `AsyncStaticExporter` asynchronous exporter insted of a synchronous one by building it via `StaticExportBuilder`'s `build_async` method.
199+
200+
> Note: Both sync and async exporters are available whenever a `static_export_*` feature is enabled in `plotly`.
195201
196202
For more details check the [`plotly_static` API Documentation](https://docs.rs/plotly_static/)
197203

examples/customization/consistent_static_format_export/src/main.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use plotly::color::{NamedColor, Rgb};
33
use plotly::common::{Anchor, Font, Line, Marker, MarkerSymbol, Mode, Title};
44
use plotly::layout::{Axis, ItemSizing, Legend, Margin, Shape, ShapeLine, ShapeType};
55
use plotly::plotly_static::{ImageFormat, StaticExporterBuilder};
6+
use plotly::prelude::*;
67
use plotly::{Layout, Plot, Scatter};
78

89
fn line_and_scatter_plot(
@@ -149,13 +150,16 @@ fn line_and_scatter_plot(
149150
.unwrap();
150151

151152
info!("Exporting to PNG format...");
152-
plot.write_image_with_exporter(&mut exporter, file_name, ImageFormat::PNG, 1280, 960, 1.0)
153+
exporter
154+
.write_image(&plot, file_name, ImageFormat::PNG, 1280, 960, 1.0)
153155
.unwrap();
154156
info!("Exporting to SVG format...");
155-
plot.write_image_with_exporter(&mut exporter, file_name, ImageFormat::SVG, 1280, 960, 1.0)
157+
exporter
158+
.write_image(&plot, file_name, ImageFormat::SVG, 1280, 960, 1.0)
156159
.unwrap();
157160
info!("Exporting to PDF format...");
158-
plot.write_image_with_exporter(&mut exporter, file_name, ImageFormat::PDF, 1280, 960, 1.0)
161+
exporter
162+
.write_image(&plot, file_name, ImageFormat::PDF, 1280, 960, 1.0)
159163
.unwrap();
160164

161165
info!("Export complete. Check the output files:");

examples/static_export/README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ The `plotly_static` provides a interface for converting Plotly plots into variou
66

77
In this example it is shown how to use the `StaticExporter` with the old style Kaleido API and also with the new style API. Using the former API is fine for one time static exports, but that API will crate an instance of the `StaticExporter` for each `write_image` call. The new style API is recommended for performance as the same instance of the `StaticExporter` can be reused across multiple exports.
88

9-
There are both a sync and an async StaticExporter version which can be build with `build` and `build_async` methods.
10-
11-
Refer to the [`plotly_static` API Documentation](https://docs.rs/plotly_static/) a more detailed description.
9+
When any of the `plotly` static export features are enabled (`static_export_chromedriver`, `static_export_geckodriver`, or `static_export_default`), both `StaticExporter` (sync) and `AsyncStaticExporter` (async) are available via `plotly::plotly_static`. This example includes separate `sync` and `async` bins demonstrating both. Refer to the [`plotly_static` API Documentation](https://docs.rs/plotly_static/) a more detailed description.
1210

1311
## Overview
1412

examples/static_export/src/bin/async.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use log::info;
22
use plotly::plotly_static::{ImageFormat, StaticExporterBuilder};
3+
use plotly::prelude::*;
34
use plotly::{Plot, Scatter};
45

56
#[tokio::main]
@@ -22,39 +23,39 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
2223
.expect("Failed to create AsyncStaticExporter");
2324

2425
info!("Exporting multiple plots using a single AsyncStaticExporter...");
25-
plot1
26-
.write_image_async(
27-
&mut exporter,
26+
exporter
27+
.write_image(
28+
&plot1,
2829
"./output/plot1_async_api",
2930
ImageFormat::PNG,
3031
800,
3132
600,
3233
1.0,
3334
)
3435
.await?;
35-
plot1
36-
.write_image_async(
37-
&mut exporter,
36+
exporter
37+
.write_image(
38+
&plot1,
3839
"./output/plot1_async_api",
3940
ImageFormat::JPEG,
4041
800,
4142
600,
4243
1.0,
4344
)
4445
.await?;
45-
plot2
46-
.write_image_async(
47-
&mut exporter,
46+
exporter
47+
.write_image(
48+
&plot2,
4849
"./output/plot2_async_api",
4950
ImageFormat::SVG,
5051
800,
5152
600,
5253
1.0,
5354
)
5455
.await?;
55-
plot2
56-
.write_image_async(
57-
&mut exporter,
56+
exporter
57+
.write_image(
58+
&plot2,
5859
"./output/plot2_async_api",
5960
ImageFormat::PDF,
6061
800,
@@ -64,10 +65,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
6465
.await?;
6566

6667
info!("Exporting to base64 and SVG strings with async API...");
67-
let _base64_data = plot1
68-
.to_base64_async(&mut exporter, ImageFormat::PNG, 400, 300, 1.0)
68+
let _base64_data = exporter
69+
.to_base64(&plot1, ImageFormat::PNG, 400, 300, 1.0)
6970
.await?;
70-
let _svg_data = plot1.to_svg_async(&mut exporter, 400, 300, 1.0).await?;
71+
let _svg_data = exporter.to_svg(&plot1, 400, 300, 1.0).await?;
7172

7273
// Always close the exporter to ensure proper release of WebDriver resources
7374
exporter.close().await;

examples/static_export/src/bin/sync.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use log::info;
22
use plotly::plotly_static::{ImageFormat, StaticExporterBuilder};
3+
use plotly::prelude::*;
34
use plotly::{Plot, Scatter};
45

56
fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -41,34 +42,35 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
4142
.expect("Failed to create StaticExporter");
4243

4344
info!("Exporting multiple plots using a single StaticExporter...");
44-
// Export all plots using the same exporter
45-
plot1.write_image_with_exporter(
46-
&mut exporter,
45+
// Export all plots using the same exporter (new unified naming via extension
46+
// trait)
47+
exporter.write_image(
48+
&plot1,
4749
"./output/plot1_new_api",
4850
ImageFormat::PNG,
4951
800,
5052
600,
5153
1.0,
5254
)?;
53-
plot2.write_image_with_exporter(
54-
&mut exporter,
55+
exporter.write_image(
56+
&plot2,
5557
"./output/plot2_new_api",
5658
ImageFormat::JPEG,
5759
800,
5860
600,
5961
1.0,
6062
)?;
61-
plot3.write_image_with_exporter(
62-
&mut exporter,
63+
exporter.write_image(
64+
&plot3,
6365
"./output/plot3_new_api",
6466
ImageFormat::SVG,
6567
800,
6668
600,
6769
1.0,
6870
)?;
6971

70-
plot1.write_image_with_exporter(
71-
&mut exporter,
72+
exporter.write_image(
73+
&plot1,
7274
"./output/plot1_new_api",
7375
ImageFormat::PDF,
7476
800,
@@ -79,11 +81,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
7981
// Demonstrate string-based export
8082
info!("Exporting to base64 and SVG strings...");
8183
// Get base64 data (useful for embedding in HTML or APIs)
82-
let base64_data =
83-
plot1.to_base64_with_exporter(&mut exporter, ImageFormat::PNG, 400, 300, 1.0)?;
84+
let base64_data = exporter.to_base64(&plot1, ImageFormat::PNG, 400, 300, 1.0)?;
8485
info!("Base64 data length: {}", base64_data.len());
8586

86-
let svg_data = plot1.to_svg_with_exporter(&mut exporter, 400, 300, 1.0)?;
87+
let svg_data = exporter.to_svg(&plot1, 400, 300, 1.0)?;
8788
info!("SVG data starts with: {}", &svg_data[..50]);
8889

8990
info!("All exports completed successfully!");

plotly/Cargo.toml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,22 @@ keywords = ["plot", "chart", "plotly"]
1717
exclude = ["target/*"]
1818

1919
[features]
20-
static_export_chromedriver = ["plotly_static", "plotly_static/chromedriver"]
21-
static_export_geckodriver = ["plotly_static", "plotly_static/geckodriver"]
20+
static_export_chromedriver = [
21+
"plotly_static",
22+
"plotly_static/chromedriver",
23+
"async-trait",
24+
]
25+
static_export_geckodriver = [
26+
"plotly_static",
27+
"plotly_static/geckodriver",
28+
"async-trait",
29+
]
2230
static_export_wd_download = ["plotly_static/webdriver_download"]
2331
static_export_default = [
2432
"plotly_static",
2533
"plotly_static/chromedriver",
2634
"plotly_static/webdriver_download",
35+
"async-trait",
2736
]
2837

2938
plotly_ndarray = ["ndarray"]
@@ -64,6 +73,7 @@ rand = { version = "0.9", default-features = false, features = [
6473
"small_rng",
6574
"alloc",
6675
] }
76+
async-trait = { version = "0.1", optional = true }
6777

6878
[target.'cfg(target_arch = "wasm32")'.dependencies]
6979
wasm-bindgen-futures = { version = "0.4" }

0 commit comments

Comments
 (0)