Skip to content

Commit e80f837

Browse files
Rework wasm feature and make it work with getrandom 0.3 (#277)
* update rand requirement from 0.8 to 0.9 * update rand_distr requirement from 0.4 to 0.5 * update getrandom requirement from 0.2 to 0.3 * update code to work with rand=0.9 and rand_distr=0.5 crates * fix issues with latest getrandom 0.3 and wasm target - reworked the wasm use case and removed the wasm feature flag, putting everything behind the target specific dependencies Signed-off-by: Andrei Gherghescu <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
1 parent 7c63fbe commit e80f837

File tree

20 files changed

+63
-58
lines changed

20 files changed

+63
-58
lines changed

.cargo/config.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[target.wasm32-unknown-unknown]
2+
rustflags = ['--cfg', 'getrandom_backend="wasm_js"']

.github/workflows/ci.yml

+4-6
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ jobs:
3737
with:
3838
components: clippy
3939
targets: wasm32-unknown-unknown
40-
# lint the main library workspace excluding the wasm feature
41-
- run: cargo clippy --features plotly_ndarray,plotly_image,kaleido -- -D warnings
42-
# lint the plotly library with wasm enabled
43-
- run: cargo clippy --package plotly --features wasm --target wasm32-unknown-unknown -- -D warnings
40+
# lint the main library workspace for non-wasm target
41+
- run: cargo clippy --all-features -- -D warnings
4442
# lint the non-wasm examples
4543
- run: cd ${{ github.workspace }}/examples && cargo clippy --workspace --exclude "wasm*" -- -D warnings
44+
# lint the plotly library for wasm target
45+
- run: cargo clippy --package plotly --target wasm32-unknown-unknown -- -D warnings
4646
# lint the wasm examples
4747
- run: cd ${{ github.workspace }}/examples && cargo clippy --target wasm32-unknown-unknown --package "wasm*"
4848

@@ -83,8 +83,6 @@ jobs:
8383
with:
8484
components: llvm-tools-preview
8585
- uses: taiki-e/install-action@cargo-llvm-cov
86-
# we are skipping anything to do with wasm here
87-
- run: cargo llvm-cov --workspace --features plotly_ndarray,plotly_image,kaleido --lcov --output-path lcov.info
8886
- uses: codecov/codecov-action@v3
8987

9088
build_examples:

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.13.0] - 2025-02-xx
7+
### Changed
8+
- [[#277](https://github.com/plotly/plotly.rs/pull/277)] Removed `wasm` feature flag and put evrything behind target specific dependencies. Added `.cargo/config.toml` for configuration flags needed by `getrandom` version 0.3 on `wasm` targets.
9+
610
## [0.12.1] - 2025-01-02
711
### Fixed
812
- [[#269](https://github.com/plotly/plotly.rs/pull/269)] Fix publishing to crates.io issue

docs/book/src/fundamentals/shapes.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use plotly::layout::{
1212
ShapeType,
1313
};
1414
use plotly::{Bar, color::NamedColor, Plot, Scatter};
15-
use rand::thread_rng;
15+
use rand::rng;
1616
use rand_distr::{Distribution, Normal};
1717
```
1818

examples/3d_charts/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ edition = "2021"
66

77
[dependencies]
88
ndarray = "0.16"
9-
rand = "0.8"
9+
rand = "0.9"
1010
plotly = { path = "../../plotly" }

examples/3d_charts/src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ fn colorscale_plot(show: bool) -> Plot {
218218
let _color: Vec<usize> = (0..z.len()).collect();
219219
let _color: Vec<u8> = (0..z.len()).map(|x| x as u8).collect();
220220
let _color: Vec<i16> = {
221-
let mut rng = rand::thread_rng();
222-
(0..z.len()).map(|_| rng.gen_range(0..100)).collect()
221+
let mut rng = rand::rng();
222+
(0..z.len()).map(|_| rng.random_range(0..100)).collect()
223223
};
224224

225225
let color_max = color.iter().fold(f64::MIN, |acc, x| acc.max(*x as f64));

examples/basic_charts/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ edition = "2021"
77
[dependencies]
88
ndarray = "0.16"
99
plotly = { path = "../../plotly" }
10-
rand = "0.8"
11-
rand_distr = "0.4"
10+
rand = "0.9"
11+
rand_distr = "0.5"

examples/basic_charts/src/main.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fn simple_scatter_plot(show: bool) -> Plot {
3838
// ANCHOR: line_and_scatter_plots
3939
fn line_and_scatter_plots(show: bool) -> Plot {
4040
let n: usize = 100;
41-
let mut rng = rand::thread_rng();
41+
let mut rng = rand::rng();
4242
let random_x: Vec<f64> = Array::linspace(0., 1., n).into_raw_vec_and_offset().0;
4343
let random_y0: Vec<f64> = Normal::new(5., 1.)
4444
.unwrap()
@@ -273,8 +273,12 @@ fn colored_and_styled_scatter_plot(show: bool) -> Plot {
273273
// ANCHOR: large_data_sets
274274
fn large_data_sets(show: bool) -> Plot {
275275
let n: usize = 100_000;
276-
let mut rng = rand::thread_rng();
277-
let r: Vec<f64> = Uniform::new(0., 1.).sample_iter(&mut rng).take(n).collect();
276+
let mut rng = rand::rng();
277+
let r: Vec<f64> = Uniform::new(0., 1.)
278+
.unwrap()
279+
.sample_iter(&mut rng)
280+
.take(n)
281+
.collect();
278282
let theta: Vec<f64> = Normal::new(0., 2. * std::f64::consts::PI)
279283
.unwrap()
280284
.sample_iter(&mut rng)

examples/customization/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ edition = "2021"
66

77
[dependencies]
88
build_html = "2.5.0"
9-
rand = "0.8"
9+
rand = "0.9"
1010
ndarray = "0.16"
1111
plotly = { path = "../../plotly" }

examples/customization/src/main.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,11 @@ fn write_html(html_data: &str) -> String {
121121
use std::env;
122122
use std::{fs::File, io::Write};
123123

124-
use rand::{
125-
distributions::{Alphanumeric, DistString},
126-
thread_rng,
127-
};
124+
use rand::distr::{Alphanumeric, SampleString};
128125

129126
// Set up the temp file with a unique filename.
130127
let mut temp = env::temp_dir();
131-
let mut plot_name = Alphanumeric.sample_string(&mut thread_rng(), 22);
128+
let mut plot_name = Alphanumeric.sample_string(&mut rand::rng(), 22);
132129
plot_name.push_str(".html");
133130
plot_name = format!("plotly_{}", plot_name);
134131
temp.push(plot_name);

examples/shapes/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ edition = "2021"
77
[dependencies]
88
ndarray = "0.16"
99
plotly = { path = "../../plotly" }
10-
rand = "0.8"
11-
rand_distr = "0.4"
10+
rand = "0.9"
11+
rand_distr = "0.5"

examples/shapes/src/main.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use plotly::{
99
},
1010
Bar, Plot, Scatter,
1111
};
12-
use rand::thread_rng;
1312
use rand_distr::{num_traits::Float, Distribution, Normal};
1413

1514
// ANCHOR: filled_area_chart
@@ -433,7 +432,7 @@ fn circles_positioned_relative_to_the_axes(show: bool) -> Plot {
433432

434433
// ANCHOR: highlighting_clusters_of_scatter_points_with_circle_shapes
435434
fn highlighting_clusters_of_scatter_points_with_circle_shapes(show: bool) -> Plot {
436-
let mut rng = thread_rng();
435+
let mut rng = rand::rng();
437436
let x0 = Normal::new(2., 0.45)
438437
.unwrap()
439438
.sample_iter(&mut rng)

examples/statistical_charts/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ edition = "2021"
77
[dependencies]
88
ndarray = "0.16"
99
plotly = { path = "../../plotly" }
10-
rand = "0.8"
11-
rand_distr = "0.4"
10+
rand = "0.9"
11+
rand_distr = "0.5"

examples/statistical_charts/src/main.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ fn colored_and_styled_error_bars(show: bool) -> Plot {
170170
// Box Plots
171171
// ANCHOR: basic_box_plot
172172
fn basic_box_plot(show: bool) -> Plot {
173-
let mut rng = rand::thread_rng();
174-
let uniform1 = Uniform::new(0.0, 1.0);
175-
let uniform2 = Uniform::new(1.0, 2.0);
173+
let mut rng = rand::rng();
174+
let uniform1 = Uniform::new(0.0, 1.0).unwrap();
175+
let uniform2 = Uniform::new(1.0, 2.0).unwrap();
176176
let n = 50;
177177

178178
let mut y0 = Vec::with_capacity(n);
@@ -407,8 +407,8 @@ fn grouped_horizontal_box_plot(show: bool) -> Plot {
407407
fn fully_styled_box_plot(show: bool) -> Plot {
408408
let rnd_sample = |num, mul| -> Vec<f64> {
409409
let mut v: Vec<f64> = Vec::with_capacity(num);
410-
let mut rng = rand::thread_rng();
411-
let uniform = Uniform::new(0.0, mul);
410+
let mut rng = rand::rng();
411+
let uniform = Uniform::new(0.0, mul).unwrap();
412412
for _ in 0..num {
413413
v.push(uniform.sample(&mut rng));
414414
}
@@ -478,7 +478,7 @@ fn fully_styled_box_plot(show: bool) -> Plot {
478478

479479
// Histograms
480480
fn sample_normal_distribution(n: usize, mean: f64, std_dev: f64) -> Vec<f64> {
481-
let mut rng = rand::thread_rng();
481+
let mut rng = rand::rng();
482482
let dist = Normal::new(mean, std_dev).unwrap();
483483
let mut v = Vec::<f64>::with_capacity(n);
484484
for _idx in 1..n {
@@ -488,8 +488,8 @@ fn sample_normal_distribution(n: usize, mean: f64, std_dev: f64) -> Vec<f64> {
488488
}
489489

490490
fn sample_uniform_distribution(n: usize, lb: f64, ub: f64) -> Vec<f64> {
491-
let mut rng = rand::thread_rng();
492-
let dist = Uniform::new(lb, ub);
491+
let mut rng = rand::rng();
492+
let dist = Uniform::new(lb, ub).unwrap();
493493
let mut v = Vec::<f64>::with_capacity(n);
494494
for _idx in 1..n {
495495
v.push(dist.sample(&mut rng));

examples/wasm-yew-minimal/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ authors = [
88
edition = "2021"
99

1010
[dependencies]
11-
plotly = { path = "../../plotly", features = ["wasm"] }
11+
plotly = { path = "../../plotly" }
1212
yew = "0.21"
1313
yew-hooks = "0.3"
1414
log = "0.4"

plotly/Cargo.toml

+7-7
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,14 @@ plotly_ndarray = ["ndarray"]
2121
plotly_image = ["image"]
2222
plotly_embed_js = []
2323

24-
wasm = ["getrandom", "js-sys", "wasm-bindgen", "wasm-bindgen-futures"]
2524
with-axum = ["rinja/with-axum", "rinja_axum"]
2625

2726
[dependencies]
2827
rinja = { version = "0.3", features = ["serde_json"] }
2928
rinja_axum = { version = "0.3", optional = true }
3029
dyn-clone = "1"
3130
erased-serde = "0.4"
32-
getrandom = { version = "0.2", features = ["js"], optional = true }
3331
image = { version = "0.25", optional = true }
34-
js-sys = { version = "0.3", optional = true }
3532
plotly_derive = { version = "0.12", path = "../plotly_derive" }
3633
plotly_kaleido = { version = "0.12", path = "../plotly_kaleido", optional = true }
3734
ndarray = { version = "0.16", optional = true }
@@ -40,9 +37,12 @@ serde = { version = "1.0", features = ["derive"] }
4037
serde_json = "1.0"
4138
serde_repr = "0.1"
4239
serde_with = ">=2, <4"
43-
rand = "0.8"
44-
wasm-bindgen = { version = "0.2", optional = true }
45-
wasm-bindgen-futures = { version = "0.4", optional = true }
40+
rand = "0.9"
41+
42+
[target.'cfg(target_arch = "wasm32")'.dependencies]
43+
getrandom = { version = "0.3", features = ["wasm_js"] }
44+
wasm-bindgen-futures = { version = "0.4" }
45+
wasm-bindgen = { version = "0.2" }
4646

4747
[dev-dependencies]
4848
csv = "1.1"
@@ -51,5 +51,5 @@ itertools = ">=0.10, <0.15"
5151
itertools-num = "0.1"
5252
ndarray = "0.16"
5353
plotly_kaleido = { path = "../plotly_kaleido", features = ["download"] }
54-
rand_distr = "0.4"
54+
rand_distr = "0.5"
5555
base64 = "0.22"

plotly/src/bindings.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
//! context, where it is assumed that a remote copy of the Javascript Plotly
33
//! library is available, (i.e. via a CDN).
44
5-
use js_sys::Object;
65
use wasm_bindgen::prelude::*;
6+
use wasm_bindgen_futures::js_sys::Object;
77

88
use crate::Plot;
99

plotly/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ extern crate rand;
66
extern crate rinja;
77
extern crate serde;
88

9-
#[cfg(all(feature = "kaleido", feature = "wasm"))]
9+
#[cfg(all(feature = "kaleido", target_family = "wasm"))]
1010
compile_error!(
11-
r#"The "kaleido" and "wasm" features are mutually exclusive and cannot be activated at the same time. Please disable one or the other."#
11+
r#"The "kaleido" feature is not available on "wasm" targets. Please compile without this feature for the wasm target family."#
1212
);
1313

1414
#[cfg(feature = "plotly_ndarray")]
1515
pub mod ndarray;
1616
#[cfg(feature = "plotly_ndarray")]
1717
pub use crate::ndarray::ArrayTraces;
1818

19-
#[cfg(feature = "wasm")]
19+
#[cfg(target_family = "wasm")]
2020
pub mod bindings;
2121

2222
pub mod common;

plotly/src/plot.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::{fs::File, io::Write, path::Path};
33
use dyn_clone::DynClone;
44
use erased_serde::Serialize as ErasedSerialize;
55
use rand::{
6-
distributions::{Alphanumeric, DistString},
7-
thread_rng,
6+
distr::{Alphanumeric, SampleString},
7+
rng,
88
};
99
use rinja::Template;
1010
use serde::Serialize;
@@ -254,7 +254,7 @@ impl Plot {
254254

255255
// Set up the temp file with a unique filename.
256256
let mut temp = env::temp_dir();
257-
let mut plot_name = Alphanumeric.sample_string(&mut thread_rng(), 22);
257+
let mut plot_name = Alphanumeric.sample_string(&mut rng(), 22);
258258
plot_name.push_str(".html");
259259
plot_name = format!("plotly_{}", plot_name);
260260
temp.push(plot_name);
@@ -296,7 +296,7 @@ impl Plot {
296296

297297
// Set up the temp file with a unique filename.
298298
let mut temp = env::temp_dir();
299-
let mut plot_name = Alphanumeric.sample_string(&mut thread_rng(), 22);
299+
let mut plot_name = Alphanumeric.sample_string(&mut rng(), 22);
300300
plot_name.push_str(".html");
301301
plot_name = format!("plotly_{}", plot_name);
302302
temp.push(plot_name);
@@ -354,13 +354,13 @@ impl Plot {
354354
pub fn to_inline_html(&self, plot_div_id: Option<&str>) -> String {
355355
let plot_div_id = match plot_div_id {
356356
Some(id) => id.to_string(),
357-
None => Alphanumeric.sample_string(&mut thread_rng(), 20),
357+
None => Alphanumeric.sample_string(&mut rng(), 20),
358358
};
359359
self.render_inline(&plot_div_id)
360360
}
361361

362362
fn to_jupyter_notebook_html(&self) -> String {
363-
let plot_div_id = Alphanumeric.sample_string(&mut thread_rng(), 20);
363+
let plot_div_id = Alphanumeric.sample_string(&mut rng(), 20);
364364

365365
let tmpl = JupyterNotebookPlotTemplate {
366366
plot: self,
@@ -534,10 +534,11 @@ impl Plot {
534534
serde_json::to_string(self).unwrap()
535535
}
536536

537-
#[cfg(feature = "wasm")]
537+
#[cfg(target_family = "wasm")]
538538
/// Convert a `Plot` to a native Javasript `js_sys::Object`.
539-
pub fn to_js_object(&self) -> js_sys::Object {
540-
use wasm_bindgen::JsCast;
539+
pub fn to_js_object(&self) -> wasm_bindgen_futures::js_sys::Object {
540+
use wasm_bindgen_futures::js_sys;
541+
use wasm_bindgen_futures::wasm_bindgen::JsCast;
541542
// The only reason this could fail is if to_json() produces structurally
542543
// incorrect JSON. That would be a bug, and would require fixing in the
543544
// to_json()/serialization methods, rather than here
@@ -734,7 +735,7 @@ mod tests {
734735

735736
#[test]
736737
#[ignore] // Don't really want it to try and open a browser window every time we run a test.
737-
#[cfg(not(feature = "wasm"))]
738+
#[cfg(not(target_family = "wasm"))]
738739
fn show_image() {
739740
let plot = create_test_plot();
740741
plot.show_image(ImageFormat::PNG, 1024, 680);

plotly/src/traces/histogram.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ where
220220
///
221221
/// fn ndarray_to_traces() {
222222
/// let n: usize = 1_250;
223-
/// let mut rng = rand::thread_rng();
223+
/// let mut rng = rand::rng();
224224
/// let t: Array<f64, Ix1> = Array::range(0., 10., 10. / n as f64);
225225
/// let mut ys: Array<f64, Ix2> = Array::zeros((n, 4));
226226
/// let mut count = 0.;

0 commit comments

Comments
 (0)