From 93cad5635316f2c6df77fb75c021a47a0b7566b5 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 28 Jan 2025 03:52:05 +0000 Subject: [PATCH 1/3] Update rand requirement from 0.8 to 0.9 Updates the requirements on [rand](https://github.com/rust-random/rand) to permit the latest version. - [Release notes](https://github.com/rust-random/rand/releases) - [Changelog](https://github.com/rust-random/rand/blob/master/CHANGELOG.md) - [Commits](https://github.com/rust-random/rand/compare/0.8.0...0.9.0) --- updated-dependencies: - dependency-name: rand dependency-type: direct:production ... Signed-off-by: dependabot[bot] --- plotly/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plotly/Cargo.toml b/plotly/Cargo.toml index 921675e0..37849eb4 100644 --- a/plotly/Cargo.toml +++ b/plotly/Cargo.toml @@ -40,7 +40,7 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" serde_repr = "0.1" serde_with = ">=2, <4" -rand = "0.8" +rand = "0.9" wasm-bindgen = { version = "0.2", optional = true } wasm-bindgen-futures = { version = "0.4", optional = true } From b9884f0d3472be344149c358c2dd620fbf25edcf Mon Sep 17 00:00:00 2001 From: Andrei Gherghescu <8067229+andrei-ng@users.noreply.github.com> Date: Wed, 29 Jan 2025 19:04:05 +0100 Subject: [PATCH 2/3] update code to work with rand=0.9 and rand_distr=0.5 crates Signed-off-by: Andrei Gherghescu <8067229+andrei-ng@users.noreply.github.com> --- docs/book/src/fundamentals/shapes.md | 2 +- examples/3d_charts/Cargo.toml | 2 +- examples/3d_charts/src/main.rs | 4 ++-- examples/basic_charts/Cargo.toml | 4 ++-- examples/basic_charts/src/main.rs | 10 +++++++--- examples/customization/Cargo.toml | 2 +- examples/customization/src/main.rs | 7 ++----- examples/shapes/Cargo.toml | 4 ++-- examples/shapes/src/main.rs | 3 +-- examples/statistical_charts/Cargo.toml | 4 ++-- examples/statistical_charts/src/main.rs | 16 ++++++++-------- plotly/Cargo.toml | 2 +- plotly/src/plot.rs | 12 ++++++------ plotly/src/traces/histogram.rs | 2 +- 14 files changed, 37 insertions(+), 37 deletions(-) diff --git a/docs/book/src/fundamentals/shapes.md b/docs/book/src/fundamentals/shapes.md index 4c34d684..04314a53 100644 --- a/docs/book/src/fundamentals/shapes.md +++ b/docs/book/src/fundamentals/shapes.md @@ -12,7 +12,7 @@ use plotly::layout::{ ShapeType, }; use plotly::{Bar, color::NamedColor, Plot, Scatter}; -use rand::thread_rng; +use rand::rng; use rand_distr::{Distribution, Normal}; ``` diff --git a/examples/3d_charts/Cargo.toml b/examples/3d_charts/Cargo.toml index fab9a60d..01f56c64 100644 --- a/examples/3d_charts/Cargo.toml +++ b/examples/3d_charts/Cargo.toml @@ -6,5 +6,5 @@ edition = "2021" [dependencies] ndarray = "0.16" -rand = "0.8" +rand = "0.9" plotly = { path = "../../plotly" } diff --git a/examples/3d_charts/src/main.rs b/examples/3d_charts/src/main.rs index f4b7c03a..23da9718 100644 --- a/examples/3d_charts/src/main.rs +++ b/examples/3d_charts/src/main.rs @@ -218,8 +218,8 @@ fn colorscale_plot(show: bool) -> Plot { let _color: Vec = (0..z.len()).collect(); let _color: Vec = (0..z.len()).map(|x| x as u8).collect(); let _color: Vec = { - let mut rng = rand::thread_rng(); - (0..z.len()).map(|_| rng.gen_range(0..100)).collect() + let mut rng = rand::rng(); + (0..z.len()).map(|_| rng.random_range(0..100)).collect() }; let color_max = color.iter().fold(f64::MIN, |acc, x| acc.max(*x as f64)); diff --git a/examples/basic_charts/Cargo.toml b/examples/basic_charts/Cargo.toml index 54ffb5ee..c89e6adf 100644 --- a/examples/basic_charts/Cargo.toml +++ b/examples/basic_charts/Cargo.toml @@ -7,5 +7,5 @@ edition = "2021" [dependencies] ndarray = "0.16" plotly = { path = "../../plotly" } -rand = "0.8" -rand_distr = "0.4" +rand = "0.9" +rand_distr = "0.5" diff --git a/examples/basic_charts/src/main.rs b/examples/basic_charts/src/main.rs index 0de8a8ca..89c30571 100644 --- a/examples/basic_charts/src/main.rs +++ b/examples/basic_charts/src/main.rs @@ -38,7 +38,7 @@ fn simple_scatter_plot(show: bool) -> Plot { // ANCHOR: line_and_scatter_plots fn line_and_scatter_plots(show: bool) -> Plot { let n: usize = 100; - let mut rng = rand::thread_rng(); + let mut rng = rand::rng(); let random_x: Vec = Array::linspace(0., 1., n).into_raw_vec_and_offset().0; let random_y0: Vec = Normal::new(5., 1.) .unwrap() @@ -273,8 +273,12 @@ fn colored_and_styled_scatter_plot(show: bool) -> Plot { // ANCHOR: large_data_sets fn large_data_sets(show: bool) -> Plot { let n: usize = 100_000; - let mut rng = rand::thread_rng(); - let r: Vec = Uniform::new(0., 1.).sample_iter(&mut rng).take(n).collect(); + let mut rng = rand::rng(); + let r: Vec = Uniform::new(0., 1.) + .unwrap() + .sample_iter(&mut rng) + .take(n) + .collect(); let theta: Vec = Normal::new(0., 2. * std::f64::consts::PI) .unwrap() .sample_iter(&mut rng) diff --git a/examples/customization/Cargo.toml b/examples/customization/Cargo.toml index bc794a09..74bc5cd9 100644 --- a/examples/customization/Cargo.toml +++ b/examples/customization/Cargo.toml @@ -6,6 +6,6 @@ edition = "2021" [dependencies] build_html = "2.5.0" -rand = "0.8" +rand = "0.9" ndarray = "0.16" plotly = { path = "../../plotly" } diff --git a/examples/customization/src/main.rs b/examples/customization/src/main.rs index 1988fc7f..c32c0090 100644 --- a/examples/customization/src/main.rs +++ b/examples/customization/src/main.rs @@ -121,14 +121,11 @@ fn write_html(html_data: &str) -> String { use std::env; use std::{fs::File, io::Write}; - use rand::{ - distributions::{Alphanumeric, DistString}, - thread_rng, - }; + use rand::distr::{Alphanumeric, SampleString}; // Set up the temp file with a unique filename. let mut temp = env::temp_dir(); - let mut plot_name = Alphanumeric.sample_string(&mut thread_rng(), 22); + let mut plot_name = Alphanumeric.sample_string(&mut rand::rng(), 22); plot_name.push_str(".html"); plot_name = format!("plotly_{}", plot_name); temp.push(plot_name); diff --git a/examples/shapes/Cargo.toml b/examples/shapes/Cargo.toml index 799fea60..927e0540 100644 --- a/examples/shapes/Cargo.toml +++ b/examples/shapes/Cargo.toml @@ -7,5 +7,5 @@ edition = "2021" [dependencies] ndarray = "0.16" plotly = { path = "../../plotly" } -rand = "0.8" -rand_distr = "0.4" +rand = "0.9" +rand_distr = "0.5" diff --git a/examples/shapes/src/main.rs b/examples/shapes/src/main.rs index a00864b4..c582435b 100644 --- a/examples/shapes/src/main.rs +++ b/examples/shapes/src/main.rs @@ -9,7 +9,6 @@ use plotly::{ }, Bar, Plot, Scatter, }; -use rand::thread_rng; use rand_distr::{num_traits::Float, Distribution, Normal}; // ANCHOR: filled_area_chart @@ -433,7 +432,7 @@ fn circles_positioned_relative_to_the_axes(show: bool) -> Plot { // ANCHOR: highlighting_clusters_of_scatter_points_with_circle_shapes fn highlighting_clusters_of_scatter_points_with_circle_shapes(show: bool) -> Plot { - let mut rng = thread_rng(); + let mut rng = rand::rng(); let x0 = Normal::new(2., 0.45) .unwrap() .sample_iter(&mut rng) diff --git a/examples/statistical_charts/Cargo.toml b/examples/statistical_charts/Cargo.toml index b2b02c20..0dffe48e 100644 --- a/examples/statistical_charts/Cargo.toml +++ b/examples/statistical_charts/Cargo.toml @@ -7,5 +7,5 @@ edition = "2021" [dependencies] ndarray = "0.16" plotly = { path = "../../plotly" } -rand = "0.8" -rand_distr = "0.4" +rand = "0.9" +rand_distr = "0.5" diff --git a/examples/statistical_charts/src/main.rs b/examples/statistical_charts/src/main.rs index 4f82eaef..11b2ea21 100644 --- a/examples/statistical_charts/src/main.rs +++ b/examples/statistical_charts/src/main.rs @@ -170,9 +170,9 @@ fn colored_and_styled_error_bars(show: bool) -> Plot { // Box Plots // ANCHOR: basic_box_plot fn basic_box_plot(show: bool) -> Plot { - let mut rng = rand::thread_rng(); - let uniform1 = Uniform::new(0.0, 1.0); - let uniform2 = Uniform::new(1.0, 2.0); + let mut rng = rand::rng(); + let uniform1 = Uniform::new(0.0, 1.0).unwrap(); + let uniform2 = Uniform::new(1.0, 2.0).unwrap(); let n = 50; let mut y0 = Vec::with_capacity(n); @@ -407,8 +407,8 @@ fn grouped_horizontal_box_plot(show: bool) -> Plot { fn fully_styled_box_plot(show: bool) -> Plot { let rnd_sample = |num, mul| -> Vec { let mut v: Vec = Vec::with_capacity(num); - let mut rng = rand::thread_rng(); - let uniform = Uniform::new(0.0, mul); + let mut rng = rand::rng(); + let uniform = Uniform::new(0.0, mul).unwrap(); for _ in 0..num { v.push(uniform.sample(&mut rng)); } @@ -478,7 +478,7 @@ fn fully_styled_box_plot(show: bool) -> Plot { // Histograms fn sample_normal_distribution(n: usize, mean: f64, std_dev: f64) -> Vec { - let mut rng = rand::thread_rng(); + let mut rng = rand::rng(); let dist = Normal::new(mean, std_dev).unwrap(); let mut v = Vec::::with_capacity(n); for _idx in 1..n { @@ -488,8 +488,8 @@ fn sample_normal_distribution(n: usize, mean: f64, std_dev: f64) -> Vec { } fn sample_uniform_distribution(n: usize, lb: f64, ub: f64) -> Vec { - let mut rng = rand::thread_rng(); - let dist = Uniform::new(lb, ub); + let mut rng = rand::rng(); + let dist = Uniform::new(lb, ub).unwrap(); let mut v = Vec::::with_capacity(n); for _idx in 1..n { v.push(dist.sample(&mut rng)); diff --git a/plotly/Cargo.toml b/plotly/Cargo.toml index 37849eb4..79be1a3f 100644 --- a/plotly/Cargo.toml +++ b/plotly/Cargo.toml @@ -51,5 +51,5 @@ itertools = ">=0.10, <0.15" itertools-num = "0.1" ndarray = "0.16" plotly_kaleido = { path = "../plotly_kaleido", features = ["download"] } -rand_distr = "0.4" +rand_distr = "0.5" base64 = "0.22" diff --git a/plotly/src/plot.rs b/plotly/src/plot.rs index 6dbe22eb..90c0c5cc 100644 --- a/plotly/src/plot.rs +++ b/plotly/src/plot.rs @@ -3,8 +3,8 @@ use std::{fs::File, io::Write, path::Path}; use dyn_clone::DynClone; use erased_serde::Serialize as ErasedSerialize; use rand::{ - distributions::{Alphanumeric, DistString}, - thread_rng, + distr::{Alphanumeric, SampleString}, + rng, }; use rinja::Template; use serde::Serialize; @@ -254,7 +254,7 @@ impl Plot { // Set up the temp file with a unique filename. let mut temp = env::temp_dir(); - let mut plot_name = Alphanumeric.sample_string(&mut thread_rng(), 22); + let mut plot_name = Alphanumeric.sample_string(&mut rng(), 22); plot_name.push_str(".html"); plot_name = format!("plotly_{}", plot_name); temp.push(plot_name); @@ -296,7 +296,7 @@ impl Plot { // Set up the temp file with a unique filename. let mut temp = env::temp_dir(); - let mut plot_name = Alphanumeric.sample_string(&mut thread_rng(), 22); + let mut plot_name = Alphanumeric.sample_string(&mut rng(), 22); plot_name.push_str(".html"); plot_name = format!("plotly_{}", plot_name); temp.push(plot_name); @@ -354,13 +354,13 @@ impl Plot { pub fn to_inline_html(&self, plot_div_id: Option<&str>) -> String { let plot_div_id = match plot_div_id { Some(id) => id.to_string(), - None => Alphanumeric.sample_string(&mut thread_rng(), 20), + None => Alphanumeric.sample_string(&mut rng(), 20), }; self.render_inline(&plot_div_id) } fn to_jupyter_notebook_html(&self) -> String { - let plot_div_id = Alphanumeric.sample_string(&mut thread_rng(), 20); + let plot_div_id = Alphanumeric.sample_string(&mut rng(), 20); let tmpl = JupyterNotebookPlotTemplate { plot: self, diff --git a/plotly/src/traces/histogram.rs b/plotly/src/traces/histogram.rs index 7b83b884..e1e8c422 100644 --- a/plotly/src/traces/histogram.rs +++ b/plotly/src/traces/histogram.rs @@ -220,7 +220,7 @@ where /// /// fn ndarray_to_traces() { /// let n: usize = 1_250; - /// let mut rng = rand::thread_rng(); + /// let mut rng = rand::rng(); /// let t: Array = Array::range(0., 10., 10. / n as f64); /// let mut ys: Array = Array::zeros((n, 4)); /// let mut count = 0.; From 8ebe67b700eed1a2f6f7d7e49e54c9c485095577 Mon Sep 17 00:00:00 2001 From: Andrei Gherghescu <8067229+andrei-ng@users.noreply.github.com> Date: Wed, 29 Jan 2025 19:56:24 +0100 Subject: [PATCH 3/3] 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 <8067229+andrei-ng@users.noreply.github.com> --- .cargo/config.toml | 2 ++ .github/workflows/ci.yml | 10 ++++------ CHANGELOG.md | 4 ++++ examples/wasm-yew-minimal/Cargo.toml | 2 +- plotly/Cargo.toml | 10 +++++----- plotly/src/bindings.rs | 2 +- plotly/src/lib.rs | 6 +++--- plotly/src/plot.rs | 9 +++++---- 8 files changed, 25 insertions(+), 20 deletions(-) create mode 100644 .cargo/config.toml diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 00000000..2e07606d --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,2 @@ +[target.wasm32-unknown-unknown] +rustflags = ['--cfg', 'getrandom_backend="wasm_js"'] diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a60329ed..73799af2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,12 +37,12 @@ jobs: with: components: clippy targets: wasm32-unknown-unknown - # lint the main library workspace excluding the wasm feature - - run: cargo clippy --features plotly_ndarray,plotly_image,kaleido -- -D warnings - # lint the plotly library with wasm enabled - - run: cargo clippy --package plotly --features wasm --target wasm32-unknown-unknown -- -D warnings + # lint the main library workspace for non-wasm target + - run: cargo clippy --all-features -- -D warnings # lint the non-wasm examples - run: cd ${{ github.workspace }}/examples && cargo clippy --workspace --exclude "wasm*" -- -D warnings + # lint the plotly library for wasm target + - run: cargo clippy --package plotly --target wasm32-unknown-unknown -- -D warnings # lint the wasm examples - run: cd ${{ github.workspace }}/examples && cargo clippy --target wasm32-unknown-unknown --package "wasm*" @@ -83,8 +83,6 @@ jobs: with: components: llvm-tools-preview - uses: taiki-e/install-action@cargo-llvm-cov - # we are skipping anything to do with wasm here - - run: cargo llvm-cov --workspace --features plotly_ndarray,plotly_image,kaleido --lcov --output-path lcov.info - uses: codecov/codecov-action@v3 build_examples: diff --git a/CHANGELOG.md b/CHANGELOG.md index ee28bed8..23f41d97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. 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). +## [0.13.0] - 2025-02-xx +### Changed +- [[#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. + ## [0.12.1] - 2025-01-02 ### Fixed - [[#269](https://github.com/plotly/plotly.rs/pull/269)] Fix publishing to crates.io issue diff --git a/examples/wasm-yew-minimal/Cargo.toml b/examples/wasm-yew-minimal/Cargo.toml index bc4156ba..7a094e75 100644 --- a/examples/wasm-yew-minimal/Cargo.toml +++ b/examples/wasm-yew-minimal/Cargo.toml @@ -8,7 +8,7 @@ authors = [ edition = "2021" [dependencies] -plotly = { path = "../../plotly", features = ["wasm"] } +plotly = { path = "../../plotly" } yew = "0.21" yew-hooks = "0.3" log = "0.4" diff --git a/plotly/Cargo.toml b/plotly/Cargo.toml index 79be1a3f..576335f3 100644 --- a/plotly/Cargo.toml +++ b/plotly/Cargo.toml @@ -21,7 +21,6 @@ plotly_ndarray = ["ndarray"] plotly_image = ["image"] plotly_embed_js = [] -wasm = ["getrandom", "js-sys", "wasm-bindgen", "wasm-bindgen-futures"] with-axum = ["rinja/with-axum", "rinja_axum"] [dependencies] @@ -29,9 +28,7 @@ rinja = { version = "0.3", features = ["serde_json"] } rinja_axum = { version = "0.3", optional = true } dyn-clone = "1" erased-serde = "0.4" -getrandom = { version = "0.2", features = ["js"], optional = true } image = { version = "0.25", optional = true } -js-sys = { version = "0.3", optional = true } plotly_derive = { version = "0.12", path = "../plotly_derive" } plotly_kaleido = { version = "0.12", path = "../plotly_kaleido", optional = true } ndarray = { version = "0.16", optional = true } @@ -41,8 +38,11 @@ serde_json = "1.0" serde_repr = "0.1" serde_with = ">=2, <4" rand = "0.9" -wasm-bindgen = { version = "0.2", optional = true } -wasm-bindgen-futures = { version = "0.4", optional = true } + +[target.'cfg(target_arch = "wasm32")'.dependencies] +getrandom = { version = "0.3", features = ["wasm_js"] } +wasm-bindgen-futures = { version = "0.4" } +wasm-bindgen = { version = "0.2" } [dev-dependencies] csv = "1.1" diff --git a/plotly/src/bindings.rs b/plotly/src/bindings.rs index 938430e3..9b86c05d 100644 --- a/plotly/src/bindings.rs +++ b/plotly/src/bindings.rs @@ -2,8 +2,8 @@ //! context, where it is assumed that a remote copy of the Javascript Plotly //! library is available, (i.e. via a CDN). -use js_sys::Object; use wasm_bindgen::prelude::*; +use wasm_bindgen_futures::js_sys::Object; use crate::Plot; diff --git a/plotly/src/lib.rs b/plotly/src/lib.rs index 9d331d92..a4b0a3a4 100644 --- a/plotly/src/lib.rs +++ b/plotly/src/lib.rs @@ -6,9 +6,9 @@ extern crate rand; extern crate rinja; extern crate serde; -#[cfg(all(feature = "kaleido", feature = "wasm"))] +#[cfg(all(feature = "kaleido", target_family = "wasm"))] compile_error!( - r#"The "kaleido" and "wasm" features are mutually exclusive and cannot be activated at the same time. Please disable one or the other."# + r#"The "kaleido" feature is not available on "wasm" targets. Please compile without this feature for the wasm target family."# ); #[cfg(feature = "plotly_ndarray")] @@ -16,7 +16,7 @@ pub mod ndarray; #[cfg(feature = "plotly_ndarray")] pub use crate::ndarray::ArrayTraces; -#[cfg(feature = "wasm")] +#[cfg(target_family = "wasm")] pub mod bindings; pub mod common; diff --git a/plotly/src/plot.rs b/plotly/src/plot.rs index 90c0c5cc..ecc72934 100644 --- a/plotly/src/plot.rs +++ b/plotly/src/plot.rs @@ -534,10 +534,11 @@ impl Plot { serde_json::to_string(self).unwrap() } - #[cfg(feature = "wasm")] + #[cfg(target_family = "wasm")] /// Convert a `Plot` to a native Javasript `js_sys::Object`. - pub fn to_js_object(&self) -> js_sys::Object { - use wasm_bindgen::JsCast; + pub fn to_js_object(&self) -> wasm_bindgen_futures::js_sys::Object { + use wasm_bindgen_futures::js_sys; + use wasm_bindgen_futures::wasm_bindgen::JsCast; // The only reason this could fail is if to_json() produces structurally // incorrect JSON. That would be a bug, and would require fixing in the // to_json()/serialization methods, rather than here @@ -734,7 +735,7 @@ mod tests { #[test] #[ignore] // Don't really want it to try and open a browser window every time we run a test. - #[cfg(not(feature = "wasm"))] + #[cfg(not(target_family = "wasm"))] fn show_image() { let plot = create_test_plot(); plot.show_image(ImageFormat::PNG, 1024, 680);