Skip to content

Commit 6341686

Browse files
committed
cleanup usage of imageformat
Signed-off-by: Andrei Gherghescu <[email protected]>
1 parent 44c7eb6 commit 6341686

File tree

5 files changed

+130
-106
lines changed

5 files changed

+130
-106
lines changed

plotly/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ compile_error!(
1111
r#"The "kaleido" feature is not available on "wasm" targets. Please compile without this feature for the wasm target family."#
1212
);
1313

14+
#[cfg(all(feature = "kaleido", feature = "plotly_static"))]
15+
compile_error!(
16+
r#"The "kaleido" feature and "plotly_static" are conflictings. Please select only one of them."#
17+
);
18+
1419
#[cfg(feature = "plotly_ndarray")]
1520
pub mod ndarray;
1621
#[cfg(feature = "plotly_ndarray")]
@@ -26,7 +31,6 @@ pub mod common;
2631
pub mod configuration;
2732
pub mod layout;
2833
pub mod plot;
29-
pub(crate) mod static_format;
3034
pub mod traces;
3135

3236
pub use common::color;
@@ -35,9 +39,7 @@ pub use layout::Layout;
3539
pub use plot::{Plot, Trace};
3640

3741
#[cfg(feature = "kaleido")]
38-
pub use static_format::ImageFormat;
39-
#[cfg(not(feature = "kaleido"))]
40-
pub use static_format::ImageFormat;
42+
pub use plotly_kaleido::ImageFormat;
4143

4244
#[cfg(feature = "plotly_static")]
4345
pub use plotly_static::{StaticExporterBuilder, ImageFormat};

plotly/src/plot.rs

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,20 @@ use serde::Serialize;
1111

1212
use crate::{Configuration, Layout};
1313

14+
#[cfg(feature = "kaleido")]
15+
use plotly_kaleido::ImageFormat;
16+
17+
#[cfg(feature = "plotly_static")]
18+
use plotly_static::ImageFormat;
19+
1420
#[derive(Template)]
1521
#[template(path = "plot.html", escape = "none")]
1622
struct PlotTemplate<'a> {
1723
plot: &'a Plot,
1824
js_scripts: &'a str,
1925
}
2026

27+
#[cfg(any(feature = "kaleido", feature = "plotly_static"))]
2128
#[derive(Template)]
2229
#[template(path = "static_plot.html", escape = "none")]
2330
#[cfg(all(not(target_family = "wasm"), not(target_os = "android")))]
@@ -45,7 +52,7 @@ struct JupyterNotebookPlotTemplate<'a> {
4552

4653
#[cfg(all(not(target_family = "wasm"), not(target_os = "android")))]
4754
const DEFAULT_HTML_APP_NOT_FOUND: &str = r#"Could not find default application for HTML files.
48-
Consider using the `to_html` method obtain a string representation instead. If using the `kaleido` feature the
55+
Consider using the `to_html` method obtain a string representation instead. If using the `kaleido` or `plotly_static` feature the
4956
`write_image` method can be used to produce a static image in one of the following formats:
5057
- ImageFormat::PNG
5158
- ImageFormat::JPEG
@@ -65,33 +72,7 @@ plot.write_image("filename", ImageFormat::PNG, width, height, scale);
6572
See https://plotly.github.io/plotly.rs/content/getting_started.html for further details.
6673
"#;
6774

68-
/// Image format for static image export.
69-
#[derive(Debug, Clone, Serialize)]
70-
pub enum ImageFormat {
71-
PNG,
72-
JPEG,
73-
WEBP,
74-
SVG,
75-
PDF,
76-
EPS,
77-
}
7875

79-
impl std::fmt::Display for ImageFormat {
80-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81-
write!(
82-
f,
83-
"{}",
84-
match self {
85-
Self::PNG => "png",
86-
Self::JPEG => "jpeg",
87-
Self::WEBP => "webp",
88-
Self::SVG => "svg",
89-
Self::PDF => "pdf",
90-
Self::EPS => "eps",
91-
}
92-
)
93-
}
94-
}
9576

9677
/// A struct that implements `Trace` can be serialized to json format that is
9778
/// understood by Plotly.js.
@@ -289,6 +270,7 @@ impl Plot {
289270
/// Display the fully rendered `Plot` as a static image of the given format
290271
/// in the default system browser.
291272
#[cfg(all(not(target_family = "wasm"), not(target_os = "android")))]
273+
#[cfg(any(feature = "kaleido", feature = "plotly_static"))]
292274
pub fn show_image(&self, format: ImageFormat, width: usize, height: usize) {
293275
use std::env;
294276

@@ -409,7 +391,7 @@ impl Plot {
409391
.save(
410392
filename.as_ref(),
411393
&serde_json::to_value(self).unwrap(),
412-
&format.to_string(),
394+
format,
413395
width,
414396
height,
415397
scale,
@@ -434,7 +416,7 @@ impl Plot {
434416
kaleido
435417
.image_to_string(
436418
&serde_json::to_value(self).unwrap(),
437-
&format.to_string(),
419+
format,
438420
width,
439421
height,
440422
scale,
@@ -455,7 +437,7 @@ impl Plot {
455437
kaleido
456438
.image_to_string(
457439
&serde_json::to_value(self).unwrap(),
458-
"svg",
440+
ImageFormat::SVG,
459441
width,
460442
height,
461443
scale,
@@ -472,6 +454,7 @@ impl Plot {
472454
}
473455

474456
#[cfg(all(not(target_family = "wasm"), not(target_os = "android")))]
457+
#[cfg(any(feature = "kaleido", feature = "plotly_static"))]
475458
pub fn render_static(&self, format: &ImageFormat, width: usize, height: usize) -> String {
476459
let tmpl = StaticPlotTemplate {
477460
plot: self,
@@ -585,6 +568,12 @@ mod tests {
585568
use super::*;
586569
use crate::Scatter;
587570

571+
#[cfg(feature = "kaleido")]
572+
use plotly_kaleido::ImageFormat;
573+
574+
#[cfg(feature = "plotly_static")]
575+
use plotly_static::ImageFormat;
576+
588577
fn create_test_plot() -> Plot {
589578
let trace1 = Scatter::new(vec![0, 1, 2], vec![6, 10, 2]).name("trace1");
590579
let mut plot = Plot::new();
@@ -725,14 +714,6 @@ mod tests {
725714
assert!(plot1 == plot2);
726715
}
727716

728-
#[test]
729-
#[ignore] // Don't really want it to try and open a browser window every time we run a test.
730-
#[cfg(not(target_family = "wasm"))]
731-
fn show_image() {
732-
let plot = create_test_plot();
733-
plot.show_image(ImageFormat::PNG, 1024, 680);
734-
}
735-
736717
#[test]
737718
fn save_html() {
738719
let plot = create_test_plot();

plotly/src/static_format.rs

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)