Skip to content

Commit a111730

Browse files
committed
make all js libraries work in offline or online mode
Fixes #242 Signed-off-by: Andrei Gherghescu <[email protected]>
1 parent 1119c84 commit a111730

File tree

8 files changed

+55
-33
lines changed

8 files changed

+55
-33
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ 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-10-29
6+
## [0.10.1] - 2024-11-x
77
### Added
88
- [[#239](https://github.com/plotly/plotly.rs/pull/239)] Add Categorical Axis Ordering and Axis Category Array.
99

1010
### Fixed
11+
- [[#242](https://github.com/plotly/plotly.rs/issues/242)] Disable request for tex-svg.js file
1112
- [[#237](https://github.com/plotly/plotly.rs/issues/237)] Add Categorical Axis ordering.
1213

1314
## [0.10.0] - 2024-09-16
1415
### Added
15-
- [[#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.
16+
- [[#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_js` to allow users to use CDN version even behind the `plotly_embed_js` feature flag.
1617

1718
### Fixed
1819
- [[#230](https://github.com/plotly/plotly.rs/pull/230)] Make Bar chart `width` and `offset` use `f64` values.

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Add this to your `Cargo.toml`:
6464
plotly = "0.10.0"
6565
```
6666

67-
## Exporting an Interactive Plot
67+
## Exporting a single Interactive Plot
6868

6969
Any figure can be saved as an HTML file using the `Plot.write_html()` method. These HTML files can be opened in any web browser to access the fully interactive figure.
7070

@@ -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 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.
81+
By default, the Plotly JavaScript library and some [MathJax](https://docs.mathjax.org/en/latest/web/components/index.html) components will always be included via CDN, which results in smaller file-size, but slightly slower first load as the JavaScript libraries have to be downloaded first. Alternatively, to embed the JavaScript libraries (several megabytes in size) directly into the HTML file, `plotly-rs` must be compiled with the feature flag `plotly_embed_js`. With this feature flag the Plotly and MathJax JavaScript libraries are directly embedded in the generated HTML file. It is still possible to use the CDN version, by using the `use_cdn_js` method.
8282

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

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

@@ -207,7 +207,7 @@ By default, the CDN version of `plotly.js` is used in the library and in the gen
207207

208208
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`.
209209

210-
When the feature is enabled, users can still opt in for the CDN version by using the method `use_cdn_plotly`.
210+
When the feature is enabled, users can still opt in for the CDN version by using the method `use_cdn_js`.
211211

212212
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.
213213

plotly/Cargo.toml

Lines changed: 1 addition & 0 deletions
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+
# Embed JavaScript into library and templates for offline use
2021
plotly_embed_js = []
2122
wasm = ["getrandom", "js-sys", "wasm-bindgen", "wasm-bindgen-futures"]
2223
with-axum = ["rinja/with-axum", "rinja_axum"]

plotly/src/plot.rs

Lines changed: 42 additions & 20 deletions
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-
plotly_js_source: String,
18+
js_scripts: 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-
plotly_js_source: String,
27+
js_scripts: String,
2828
width: usize,
2929
height: usize,
3030
}
@@ -182,26 +182,26 @@ pub struct Plot {
182182
#[serde(rename = "config")]
183183
configuration: Configuration,
184184
#[serde(skip)]
185-
plotly_js_source: String,
185+
js_scripts: String,
186186
}
187187

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

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.
198+
/// Switch to CDN for `plotly.js` and `MathJax` components in the standalone HTML plots rather than using
199+
/// the default local copies of the Javascript libraries. Method is only available when the feature
200+
/// `plotly_embed_js` is enabled since without this feature the default versions used
201+
/// are always the CDN versions.
202202
#[cfg(feature = "plotly_embed_js")]
203-
pub fn use_cdn_plotly(&mut self) {
204-
self.plotly_js_source = Self::cdn_plotly_js();
203+
pub fn use_cdn_js(&mut self) {
204+
self.js_scripts = Self::online_cdn_js();
205205
}
206206

207207
/// Add a `Trace` to the `Plot`.
@@ -419,7 +419,7 @@ impl Plot {
419419
fn render(&self) -> String {
420420
let tmpl = PlotTemplate {
421421
plot: self,
422-
plotly_js_source: self.plotly_js_source.clone(),
422+
js_scripts: self.js_scripts.clone(),
423423
};
424424
tmpl.render().unwrap()
425425
}
@@ -429,7 +429,7 @@ impl Plot {
429429
let tmpl = StaticPlotTemplate {
430430
plot: self,
431431
format,
432-
plotly_js_source: self.plotly_js_source.clone(),
432+
js_scripts: self.js_scripts.clone(),
433433
width,
434434
height,
435435
};
@@ -444,21 +444,43 @@ impl Plot {
444444
tmpl.render().unwrap()
445445
}
446446

447-
fn plotly_js_source() -> String {
447+
fn js_scripts() -> String {
448448
if cfg!(feature = "plotly_embed_js") {
449-
Self::local_plotly_js()
449+
Self::offline_js_sources()
450450
} else {
451-
Self::cdn_plotly_js()
451+
Self::online_cdn_js()
452452
}
453453
}
454454

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()
455+
fn offline_js_sources() -> String {
456+
let local_plotly_js = include_str!("../templates/plotly.min.js");
457+
let local_tex_mml_js = include_str!("../templates/tex-mml-chtml-3.2.0.js");
458+
let local_tex_svg_js = include_str!("../templates/tex-svg-3.2.2.js");
459+
format!(
460+
"<script type=\"text/javascript\">{}</script>\n
461+
<script type=\"text/javascript\">
462+
/**
463+
* tex-mml-chtml JS script
464+
**/
465+
{}
466+
</script>\n
467+
<script type=\"text/javascript\">
468+
/**
469+
* tex-svg JS script
470+
**/
471+
{}
472+
</script>\n",
473+
local_plotly_js, local_tex_mml_js, local_tex_svg_js
474+
)
475+
.to_string()
458476
}
459477

460-
fn cdn_plotly_js() -> String {
461-
r##"<script src="https://cdn.plot.ly/plotly-2.12.1.min.js"></script>"##.to_string()
478+
fn online_cdn_js() -> String {
479+
r##"<script src="https://cdn.plot.ly/plotly-2.12.1.min.js"></script>
480+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/es5/tex-svg.js"></script>
481+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/es5/tex-mml-chtml.js"></script>
482+
"##
483+
.to_string()
462484
}
463485

464486
pub fn to_json(&self) -> String {

plotly/templates/plot.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77

88
<body>
99
<div>
10-
<script src="https://cdn.jsdelivr.net/npm/[email protected]/es5/tex-svg.js"></script>
11-
12-
{{plotly_js_source}}
10+
{{js_scripts}}
1311

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

plotly/templates/static_plot.html

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@
55
</head>
66
<body>
77
<div>
8-
<script src="https://cdn.jsdelivr.net/npm/[email protected]/es5/tex-mml-chtml.js"></script>
9-
10-
{{plotly_js_source}}
8+
{{js_scripts}}
119

1210
<div id="plotly-html-element" hidden></div>
1311
<img id="plotly-img-element"></img>
1412

1513
<script type="module">
1614
const graph_div = document.getElementById("plotly-html-element");
1715
await Plotly.newPlot(graph_div, {{ plot|tojson|safe }});
18-
16+
1917
const img_element = document.getElementById("plotly-img-element");
2018
const data_url = await Plotly.toImage(
2119
graph_div,

plotly/templates/tex-mml-chtml-3.2.0.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plotly/templates/tex-svg-3.2.2.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)