Skip to content

Commit 9cf057d

Browse files
committed
refactor examples to generate standalone html
Signed-off-by: Andrei Gherghescu <[email protected]>
1 parent 3326cb6 commit 9cf057d

File tree

41 files changed

+633
-683
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+633
-683
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444
# lint the plotly library for wasm target
4545
- run: cargo clippy --package plotly --target wasm32-unknown-unknown -- -D warnings
4646
# lint the wasm examples
47-
- run: cd ${{ github.workspace }}/examples && cargo clippy --target wasm32-unknown-unknown --package "wasm*"
47+
- run: cd ${{ github.workspace }}/examples/wasm-yew && cargo clippy --target wasm32-unknown-unknown --all
4848

4949
semver:
5050
name: semver
@@ -115,11 +115,11 @@ jobs:
115115
strategy:
116116
fail-fast: false
117117
matrix:
118-
example: [wasm-yew-minimal, wasm-yew-callback-minimal]
118+
example: [basic, callback-example]
119119
runs-on: ubuntu-latest
120120
steps:
121121
- uses: actions/checkout@v4
122122
- uses: dtolnay/rust-toolchain@stable
123123
with:
124124
targets: wasm32-unknown-unknown
125-
- run: cd ${{ github.workspace }}/examples/${{ matrix.example }} && cargo build --target wasm32-unknown-unknown
125+
- run: cd ${{ github.workspace }}/examples/wasm-yew/${{ matrix.example }} && cargo build --target wasm32-unknown-unknown

examples/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
out/
1+
output/

examples/3d_charts/README.md

Lines changed: 0 additions & 6 deletions
This file was deleted.

examples/3d_charts/src/main.rs

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rand::Rng;
1111

1212
// 3D Scatter Plots
1313
// ANCHOR: simple_scatter3d_plot
14-
fn simple_scatter3d_plot(show: bool) -> Plot {
14+
fn simple_scatter3d_plot(show: bool, file_name: &str) {
1515
let n: usize = 100;
1616
let t: Vec<f64> = Array::linspace(0., 10., n).into_raw_vec_and_offset().0;
1717
let y: Vec<f64> = t.iter().map(|x| x.sin()).collect();
@@ -21,15 +21,15 @@ fn simple_scatter3d_plot(show: bool) -> Plot {
2121
let mut plot = Plot::new();
2222
plot.add_trace(trace);
2323

24+
let path = write_example_to_html(&plot, file_name);
2425
if show {
25-
plot.show();
26+
plot.show_html(path);
2627
}
27-
plot
2828
}
2929
// ANCHOR_END: simple_scatter3d_plot
3030

3131
// ANCHOR: customized_scatter3d_plot
32-
fn customized_scatter3d_plot(show: bool) -> Plot {
32+
fn customized_scatter3d_plot(show: bool, file_name: &str) {
3333
let n: usize = 100;
3434
let t: Vec<f64> = Array::linspace(0., 10., n).into_raw_vec_and_offset().0;
3535
let y: Vec<f64> = t.iter().map(|x| x.sin()).collect();
@@ -114,16 +114,16 @@ fn customized_scatter3d_plot(show: bool) -> Plot {
114114
.height(500);
115115
plot.set_layout(layout);
116116

117+
let path = write_example_to_html(&plot, file_name);
117118
if show {
118-
plot.show();
119+
plot.show_html(path);
119120
}
120-
plot
121121
}
122122
// ANCHOR_END: customized_scatter3d_plot
123123

124124
// 3D Line Plots
125125
// ANCHOR: simple_line3d_plot
126-
fn simple_line3d_plot(show: bool) -> Plot {
126+
fn simple_line3d_plot(show: bool, file_name: &str) {
127127
let n: usize = 100;
128128
let t: Vec<f64> = Array::linspace(0., 10., n).into_raw_vec_and_offset().0;
129129
let y: Vec<f64> = t.iter().map(|x| x.sin()).collect();
@@ -133,16 +133,16 @@ fn simple_line3d_plot(show: bool) -> Plot {
133133
let mut plot = Plot::new();
134134
plot.add_trace(trace);
135135

136+
let path = write_example_to_html(&plot, file_name);
136137
if show {
137-
plot.show();
138+
plot.show_html(path);
138139
}
139-
plot
140140
}
141141
// ANCHOR_END: simple_line3d_plot
142142

143143
// 3D Surface Plot
144144
// ANCHOR: surface_plot
145-
fn surface_plot(show: bool) -> Plot {
145+
fn surface_plot(show: bool, file_name: &str) {
146146
let n: usize = 100;
147147
let x: Vec<f64> = Array::linspace(-10., 10., n).into_raw_vec_and_offset().0;
148148
let y: Vec<f64> = Array::linspace(-10., 10., n).into_raw_vec_and_offset().0;
@@ -159,15 +159,15 @@ fn surface_plot(show: bool) -> Plot {
159159
let mut plot = Plot::new();
160160
plot.add_trace(trace);
161161

162+
let path = write_example_to_html(&plot, file_name);
162163
if show {
163-
plot.show();
164+
plot.show_html(path);
164165
}
165-
plot
166166
}
167167
// ANCHOR_END: surface_plot
168168

169169
// ANCHOR: mesh_3d_plot
170-
fn mesh_3d_plot(show: bool) -> Plot {
170+
fn mesh_3d_plot(show: bool, file_name: &str) {
171171
let trace = Mesh3D::new(
172172
vec![0, 1, 2, 0],
173173
vec![0, 0, 1, 2],
@@ -182,15 +182,15 @@ fn mesh_3d_plot(show: bool) -> Plot {
182182
let mut plot = Plot::new();
183183
plot.add_trace(trace);
184184

185+
let path = write_example_to_html(&plot, file_name);
185186
if show {
186-
plot.show();
187+
plot.show_html(path);
187188
}
188-
plot
189189
}
190190
// ANCHOR_END: mesh_3d_plot
191191

192192
// ANCHOR: colorscale_plot
193-
fn colorscale_plot(show: bool) -> Plot {
193+
fn colorscale_plot(show: bool, file_name: &str) {
194194
let mut plot = Plot::new();
195195

196196
let x = (0..100)
@@ -252,34 +252,31 @@ fn colorscale_plot(show: bool) -> Plot {
252252

253253
plot.set_layout(layout);
254254

255+
let path = write_example_to_html(&plot, file_name);
255256
if show {
256-
plot.show();
257+
plot.show_html(path);
257258
}
258-
plot
259259
}
260260
// ANCHOR_END: colorscale_plot
261261

262-
fn write_example_to_html(plot: Plot, name: &str) {
263-
std::fs::create_dir_all("./out").unwrap();
264-
let html = plot.to_inline_html(Some(name));
265-
std::fs::write(format!("./out/{}.html", name), html).unwrap();
262+
fn write_example_to_html(plot: &Plot, name: &str) -> String {
263+
std::fs::create_dir_all("./output").unwrap();
264+
let path = format!("./output/{}.html", name);
265+
plot.write_html(&path);
266+
path
266267
}
267268

268269
fn main() {
269270
// Change false to true on any of these lines to display the example.
270-
271271
// Scatter3D Plots
272-
write_example_to_html(simple_scatter3d_plot(false), "simple_scatter3d_plot");
273-
write_example_to_html(simple_line3d_plot(false), "simple_line3d_plot");
274-
write_example_to_html(
275-
customized_scatter3d_plot(false),
276-
"customized_scatter3d_plot",
277-
);
278-
write_example_to_html(colorscale_plot(false), "colorscale_plot");
272+
simple_scatter3d_plot(false, "simple_scatter3d_plot");
273+
simple_line3d_plot(false, "simple_line3d_plot");
274+
customized_scatter3d_plot(false, "customized_scatter3d_plot");
275+
colorscale_plot(false, "colorscale_plot");
279276

280277
// Surface Plots
281-
write_example_to_html(surface_plot(false), "surface_plot");
278+
surface_plot(false, "surface_plot");
282279

283280
// Mesh Plots
284-
write_example_to_html(mesh_3d_plot(false), "mesh_3d_plot");
281+
mesh_3d_plot(false, "mesh_3d_plot");
285282
}

examples/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[workspace]
22
members = ["*"]
33
resolver = "2"
4-
exclude = ["jupyter", "target"]
4+
exclude = ["jupyter", "target", "wasm-yew"]

examples/README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Examples
22

3-
This folder contains a multitude of usage examples covering as many features of the library as possible. Instructions on how to run each example can be found in each example's subdirectory.
3+
This folder contains a multitude of usage examples covering as many features of the library as possible.
44

5-
Pull requests with more examples of different behaviour are always welcome.
5+
To run the basic examples, `cd` into the chosen example folder and run `cargo run`. Upon completion all the generated plots of that example are located in the `output` folder as `html` files and can be open in your browser.
6+
7+
You can also configure the chosen example to open the resulting plots automatically. To do so, open the `src/main.rs` file, locate the `main` function and change the boolean flag of the called functions from `false` to `true`. This will make the examples open the default browser application and load the generated HTML files.
8+
9+
For more complex examples, instructions on how to run them can be found in the README of each example's subdirectory.
10+
11+
Pull requests with more examples of different behavior are always welcome.

examples/basic_charts/README.md

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)