Skip to content

Commit 3f47ac2

Browse files
authored
Merge branch 'main' into multiples-axis
2 parents 5fcea76 + cfa7bc3 commit 3f47ac2

File tree

6 files changed

+52
-29
lines changed

6 files changed

+52
-29
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file.
33

44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
55

6+
## [0.10.0] - 2024-xx-xx
7+
### Added
8+
- [[#231](https://github.com/plotly/plotly.rs/pull/231)] Added new `plotly_embed_js` feature to reduce binary sizes by not embedding `plotly.min.js` in the library unless explicitly enabled via the feature flag. Deprecates `use_local_plotly` in favor of explicit opt-in via the feature flag and introduce method `use_cdn_plotly` to allow users to use CDN version even behind the `plotly_embed_js` feature flag.
9+
610
## [0.9.1] - 2024-09-06
711
### Added
812
- [[#217](https://github.com/plotly/plotly.rs/pull/217)] Added show_html(filename) method to bypass situations where accessing default `/tmp` is not possible, e.g., with in SNAP Firefox

README.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ plot.add_trace(trace);
7878
plot.write_html("out.html");
7979
```
8080

81-
By default, the Plotly JavaScript library will be included via CDN, which results in a smaller filesize, but slightly slower first load as the JavaScript library has to be downloaded first. To instead embed the JavaScript library (several megabytes in size) directly into the HTML file, the following can be done:
81+
By default, the Plotly JavaScript library will be included via CDN, which results in a smaller filesize, but slightly slower first load as the JavaScript library has to be downloaded first. To instead embed the JavaScript library (several megabytes in size) directly into the HTML file, the library must be compiled with the feature flag `plotly_embed_js`. Once enabled, by default the JavaScript library is directly embedded in the generated HTML file. It is still possible to use the CDN version, by using the `use_cdn_plotly` method.
8282

8383
```rust
8484
// <-- Create a `Plot` -->
8585

86-
plot.use_local_plotly();
86+
plot.use_cdn_plotly();
8787
plot.write_html("out.html");
8888
```
8989

@@ -201,6 +201,16 @@ Adds trait implementations so that `image::RgbImage` and `image::RgbaImage` can
201201

202202
Adds support for creating plots directly using [ndarray](https://github.com/rust-ndarray/ndarray) types.
203203

204+
### `plotly_embed_js`
205+
206+
By default, the CDN version of `plotly.js` is used in the library and in the generated HTML files. This feature can be used to opt in for embedding `plotly.min.js` in the generated HTML files. The benefit is that the plot will load faster in the browser.
207+
208+
However, there are two downsides of using this feature flag, one is that the resulting html will be much larger, as a copy of the `plotly.min.js` library is embedded in each HTML file. The second, more relevant, is that a copy of the `plotly.min.js` library needs to be compiled in the `plotly-rs` library itself which increases the size by approx `3.5 Mb`.
209+
210+
When the feature is enabled, users can still opt in for the CDN version by using the method `use_cdn_plotly`.
211+
212+
Note that when using `Plot::to_inline_html()`, it is assumed that the `plotly.js` library is already in scope within the HTML file, so enabling this feature flag will have no effect.
213+
204214
### `wasm`
205215

206216
Enables compilation for the `wasm32-unknown-unknown` target and provides access to a `bindings` module containing wrappers around functions exported by the plotly.js library.

plotly/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ exclude = ["target/*"]
1717
kaleido = ["plotly_kaleido"]
1818
plotly_ndarray = ["ndarray"]
1919
plotly_image = ["image"]
20+
plotly_embed_js = []
2021
wasm = ["getrandom", "js-sys", "wasm-bindgen", "wasm-bindgen-futures"]
2122
with-axum = ["rinja/with-axum", "rinja_axum"]
2223

plotly/src/plot.rs

+30-16
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{Configuration, Layout};
1515
#[template(path = "plot.html", escape = "none")]
1616
struct PlotTemplate<'a> {
1717
plot: &'a Plot,
18-
remote_plotly_js: bool,
18+
plotly_js_source: String,
1919
}
2020

2121
#[derive(Template)]
@@ -24,7 +24,7 @@ struct PlotTemplate<'a> {
2424
struct StaticPlotTemplate<'a> {
2525
plot: &'a Plot,
2626
format: ImageFormat,
27-
remote_plotly_js: bool,
27+
plotly_js_source: String,
2828
width: usize,
2929
height: usize,
3030
}
@@ -182,29 +182,26 @@ pub struct Plot {
182182
#[serde(rename = "config")]
183183
configuration: Configuration,
184184
#[serde(skip)]
185-
remote_plotly_js: bool,
185+
plotly_js_source: String,
186186
}
187187

188188
impl Plot {
189189
/// Create a new `Plot`.
190190
pub fn new() -> Plot {
191191
Plot {
192192
traces: Traces::new(),
193-
remote_plotly_js: true,
193+
plotly_js_source: Self::plotly_js_source(),
194194
..Default::default()
195195
}
196196
}
197197

198-
/// This option results in the plotly.js library being written directly in
199-
/// the html output. The benefit is that the plot will load faster in
200-
/// the browser and the downside is that the resulting html will be much
201-
/// larger.
202-
///
203-
/// Note that when using `Plot::to_inline_html()`, it is assumed that the
204-
/// `plotly.js` library is already in scope, so setting this attribute
205-
/// will have no effect.
206-
pub fn use_local_plotly(&mut self) {
207-
self.remote_plotly_js = false;
198+
/// Switch to CDN `plotly.js` in the generated HTML instead of the default
199+
/// local `plotly.js` version. Method is only available when the feature
200+
/// `plotly_embed_js` is enabled since without this feature the default
201+
/// version used is always the CDN version.
202+
#[cfg(feature = "plotly_embed_js")]
203+
pub fn use_cdn_plotly(&mut self) {
204+
self.plotly_js_source = Self::cdn_plotly_js();
208205
}
209206

210207
/// Add a `Trace` to the `Plot`.
@@ -422,7 +419,7 @@ impl Plot {
422419
fn render(&self) -> String {
423420
let tmpl = PlotTemplate {
424421
plot: self,
425-
remote_plotly_js: self.remote_plotly_js,
422+
plotly_js_source: self.plotly_js_source.clone(),
426423
};
427424
tmpl.render().unwrap()
428425
}
@@ -432,7 +429,7 @@ impl Plot {
432429
let tmpl = StaticPlotTemplate {
433430
plot: self,
434431
format,
435-
remote_plotly_js: self.remote_plotly_js,
432+
plotly_js_source: self.plotly_js_source.clone(),
436433
width,
437434
height,
438435
};
@@ -447,6 +444,23 @@ impl Plot {
447444
tmpl.render().unwrap()
448445
}
449446

447+
fn plotly_js_source() -> String {
448+
if cfg!(feature = "plotly_embed_js") {
449+
Self::local_plotly_js()
450+
} else {
451+
Self::cdn_plotly_js()
452+
}
453+
}
454+
455+
fn local_plotly_js() -> String {
456+
let local_plotly = include_str!("../templates/plotly.min.js");
457+
format!("<script type=\"text/javascript\">{}</script>", local_plotly).to_string()
458+
}
459+
460+
fn cdn_plotly_js() -> String {
461+
r##"<script src="https://cdn.plot.ly/plotly-2.12.1.min.js"></script>"##.to_string()
462+
}
463+
450464
pub fn to_json(&self) -> String {
451465
serde_json::to_string(self).unwrap()
452466
}

plotly/templates/plot.html

+2-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@
88
<body>
99
<div>
1010
<script src="https://cdn.jsdelivr.net/npm/[email protected]/es5/tex-svg.js"></script>
11-
{% if remote_plotly_js -%}
12-
<script src="https://cdn.plot.ly/plotly-2.12.1.min.js"></script>
13-
{% else -%}
14-
<script type="text/javascript">{% include "plotly.min.js" %}</script>
15-
{% endif -%}
11+
12+
{{plotly_js_source}}
1613

1714
<div id="plotly-html-element" class="plotly-graph-div" style="height:100%; width:100%;"></div>
1815

plotly/templates/static_plot.html

+3-6
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66
<body>
77
<div>
88
<script src="https://cdn.jsdelivr.net/npm/[email protected]/es5/tex-mml-chtml.js"></script>
9-
{% if remote_plotly_js -%}
10-
<script src="https://cdn.plot.ly/plotly-2.12.1.min.js"></script>
11-
{% else -%}
12-
<script type="text/javascript">{% include "plotly.min.js" %}</script>
13-
{% endif -%}
9+
10+
{{plotly_js_source}}
1411

1512
<div id="plotly-html-element" hidden></div>
1613
<img id="plotly-img-element"></img>
@@ -33,4 +30,4 @@
3330
</script>
3431
</div>
3532
</body>
36-
</html>
33+
</html>

0 commit comments

Comments
 (0)