Skip to content

Commit f42fa87

Browse files
committed
adding static export functionality with webdriver
- allow user to reuse the exporter object - use data uri instead of saving to plot html file - added build.rs to download webdriver chromedriver/geckodriver - include example on how to use static export for json data - allow user to set browser capabilities via setter Signed-off-by: Andrei Gherghescu <[email protected]>
1 parent 81c70f2 commit f42fa87

23 files changed

+6067
-830
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[workspace]
22
resolver = "2"
3-
members = ["plotly", "plotly_derive", "plotly_kaleido"]
3+
members = ["plotly", "plotly_derive", "plotly_kaleido", "plotly_static"]

plotly/src/common/color.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,7 @@ impl FromStr for Rgba {
189189
let prefix: &[_] = &['r', 'g', 'b', 'a', '('];
190190
let trimmed = rgba.trim_start_matches(prefix).trim_end_matches(')');
191191
let fields: Vec<&str> = trimmed.split(',').collect();
192-
dbg!(&fields);
193-
println!("{:?}", &fields);
192+
// println!("{:?}", &fields);
194193
if fields.len() != 4 {
195194
Err(ParseError::new("Invalid string length of for RGBA color"))
196195
} else {

plotly/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,17 @@ pub mod common;
2626
pub mod configuration;
2727
pub mod layout;
2828
pub mod plot;
29+
pub(crate) mod static_format;
2930
pub mod traces;
3031

3132
pub use common::color;
3233
pub use configuration::Configuration;
3334
pub use layout::Layout;
34-
pub use plot::{ImageFormat, Plot, Trace};
35+
pub use plot::{Plot, Trace};
36+
#[cfg(feature = "kaleido")]
37+
pub use static_format::ImageFormat;
38+
#[cfg(not(feature = "kaleido"))]
39+
pub use static_format::ImageFormat;
3540
// Also provide easy access to modules which contain additional trace-specific types
3641
pub use traces::{
3742
box_plot, contour, heat_map, histogram, image, mesh3d, sankey, scatter, scatter3d,

plotly/src/plot.rs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ See https://plotly.github.io/plotly.rs/content/getting_started.html for further
6666
"#;
6767

6868
/// Image format for static image export.
69-
#[derive(Debug)]
69+
#[derive(Debug, Clone, Serialize)]
7070
pub enum ImageFormat {
7171
PNG,
7272
JPEG,
@@ -292,7 +292,7 @@ impl Plot {
292292
pub fn show_image(&self, format: ImageFormat, width: usize, height: usize) {
293293
use std::env;
294294

295-
let rendered = self.render_static(format, width, height);
295+
let rendered = self.render_static(&format, width, height);
296296

297297
// Set up the temp file with a unique filename.
298298
let mut temp = env::temp_dir();
@@ -472,10 +472,10 @@ impl Plot {
472472
}
473473

474474
#[cfg(all(not(target_family = "wasm"), not(target_os = "android")))]
475-
fn render_static(&self, format: ImageFormat, width: usize, height: usize) -> String {
475+
pub fn render_static(&self, format: &ImageFormat, width: usize, height: usize) -> String {
476476
let tmpl = StaticPlotTemplate {
477477
plot: self,
478-
format,
478+
format: format.clone(),
479479
js_scripts: &self.js_scripts,
480480
width,
481481
height,
@@ -501,34 +501,22 @@ impl Plot {
501501

502502
fn offline_js_sources() -> String {
503503
// tex-mml-chtml conflicts with tex-svg when generating Latex Titles
504-
// let local_tex_mml_js = include_str!("../templates/tex-mml-chtml-3.2.0.js");
505-
let local_tex_svg_js = include_str!("../templates/tex-svg-3.2.2.js");
506-
let local_plotly_js = include_str!("../templates/plotly.min.js");
504+
// let local_tex_mml_js = include_str!("../resource/tex-mml-chtml-3.2.0.js");
505+
let local_tex_svg_js = include_str!("../resource/tex-svg-3.2.2.js");
506+
let local_plotly_js = include_str!("../resource/plotly.min.js");
507507

508508
format!(
509509
"<script type=\"text/javascript\">{}</script>\n
510-
<script type=\"text/javascript\">
511-
/**
512-
* tex-svg JS script
513-
**/
514-
{}
515-
</script>\n
516-
<script type=\"text/javascript\">
517-
/**
518-
* tex-mml-chtml JS script
519-
**/
520-
{}
521-
</script>\n
522-
",
523-
local_plotly_js, local_tex_svg_js, ""
510+
<script type=\"text/javascript\">{}</script>\n",
511+
local_plotly_js, local_tex_svg_js,
524512
)
525513
.to_string()
526514
}
527515

528516
fn online_cdn_js() -> String {
529-
// Removed tex-mml-chtml as it conflicts with tex-svg when generating Latex
530-
// Titles
531-
r##"<script src="https://cdn.jsdelivr.net/npm/[email protected]/es5/tex-svg.js"></script>
517+
// tex-mml-chtml conflicts with tex-svg when generating Latex Titles
518+
r##"
519+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/es5/tex-svg.js"></script>
532520
<script src="https://cdn.plot.ly/plotly-3.0.1.min.js"></script>
533521
"##
534522
.to_string()

plotly/src/static_format.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#![cfg(not(feature = "kaleido"))]
2+
use serde::Serialize;
3+
4+
/// Image format for static image export.
5+
/// Redifined from `plotly_static` package to be used when `kaleido` feature is
6+
/// not enabled
7+
#[derive(Debug, Clone, Serialize)]
8+
pub enum ImageFormat {
9+
PNG,
10+
JPEG,
11+
WEBP,
12+
SVG,
13+
PDF,
14+
EPS,
15+
}
16+
17+
impl std::fmt::Display for ImageFormat {
18+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19+
write!(
20+
f,
21+
"{}",
22+
match self {
23+
Self::PNG => "png",
24+
Self::JPEG => "jpeg",
25+
Self::WEBP => "webp",
26+
Self::SVG => "svg",
27+
Self::PDF => "pdf",
28+
Self::EPS => "eps",
29+
}
30+
)
31+
}
32+
}

plotly/templates/static_plot.html

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
<!doctype html>
22
<html lang="en">
33

4-
<head>
5-
<meta charset="utf-8" />
4+
<head>
5+
<meta charset="utf-8" />
66
{{js_scripts}}
7-
</head>
7+
</head>
88

9-
<body>
10-
<div>
9+
<body>
10+
<div>
1111

12-
<div id="plotly-html-element" hidden></div>
13-
<img id="plotly-img-element"></img>
12+
<div id="plotly-html-element" hidden></div>
13+
<img class="plotly-img-element" id="plotly-img-element"></img>
1414

15-
<script type="module">
16-
const graph_div = document.getElementById("plotly-html-element");
17-
await Plotly.newPlot(graph_div, {{ plot| tojson | safe }});
15+
<script type="module">
16+
const graph_div = document.getElementById("plotly-html-element");
17+
await Plotly.newPlot(graph_div, {{ plot|tojson|safe }});
1818

19-
const img_element = document.getElementById("plotly-img-element");
19+
const img_element = document.getElementById("plotly-img-element");
2020
const data_url = await Plotly.toImage(
2121
graph_div,
2222
{
@@ -26,9 +26,9 @@
2626
}
2727
);
2828

29-
img_element.setAttribute("src", data_url);
30-
</script>
31-
</div>
32-
</body>
29+
img_element.setAttribute("src", data_url);
30+
</script>
31+
</div>
32+
</body>
3333

3434
</html>

0 commit comments

Comments
 (0)