Skip to content

Commit 63ac1dd

Browse files
committed
refactor examples and add themes example to mdbook
Signed-off-by: Andrei Gherghescu <[email protected]>
1 parent 30b0367 commit 63ac1dd

File tree

34 files changed

+280
-225
lines changed

34 files changed

+280
-225
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ jobs:
9595
3d_charts,
9696
basic_charts,
9797
custom_controls,
98+
customization,
9899
financial_charts,
99100
images,
100101
kaleido,
@@ -103,6 +104,7 @@ jobs:
103104
scientific_charts,
104105
shapes,
105106
subplots,
107+
themes,
106108
]
107109
runs-on: ubuntu-latest
108110
steps:
@@ -140,5 +142,6 @@ jobs:
140142
cd ${{ github.workspace }}/examples/3d_charts && cargo run
141143
cd ${{ github.workspace }}/examples/subplots && cargo run
142144
cd ${{ github.workspace }}/examples/shapes && cargo run
145+
cd ${{ github.workspace }}/examples/themes && cargo run
143146
- name: Build book
144147
run: mdbook build docs/book

docs/book/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@
2929
- [Subplots](./recipes/subplots.md)
3030
- [Subplots](./recipes/subplots/subplots.md)
3131
- [Multiple Axes](./recipes/subplots/multiple_axes.md)
32+
- [Themes](./recipes/themes.md)

docs/book/src/recipes/themes.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Themes
2+
3+
The complete source code for the following examples can also be found [here](https://github.com/plotly/plotly.rs/tree/main/examples/themes).
4+
5+
# Use different theme templates
6+
7+
Similar to [Plotly Python templates](https://plotly.com/python/templates/), plotly.rs provides several built-in themes that can be applied to your plots.
8+
9+
```rust,no_run
10+
use plotly::{
11+
common::{Marker, Mode, Title},
12+
layout::{Layout, BuiltinTheme},
13+
Plot, Scatter,
14+
};
15+
```
16+
17+
The `to_inline_html` method is used to produce the html plot displayed in this page.
18+
19+
## Default Theme (Plotly)
20+
21+
{{#include ../../../../examples/themes/output/inline_gapminder_plotly.html}}
22+
23+
## Plotly White Theme
24+
25+
{{#include ../../../../examples/themes/output/inline_gapminder_plotly_white.html}}
26+
27+
## Plotly Dark Theme
28+
29+
{{#include ../../../../examples/themes/output/inline_gapminder_plotly_dark.html}}
30+
31+
## Seaborn Theme
32+
33+
{{#include ../../../../examples/themes/output/inline_gapminder_seaborn.html}}
34+
35+
## Matplotlib Theme
36+
37+
{{#include ../../../../examples/themes/output/inline_gapminder_matplotlib.html}}
38+
39+
## Plotnine Theme
40+
41+
{{#include ../../../../examples/themes/output/inline_gapminder_plotnine.html}}
42+
43+
## Available Themes
44+
45+
The following built-in themes are available in plotly.rs:
46+
47+
- `BuiltinTheme::Default` - Default Plotly theme
48+
- `BuiltinTheme::PlotlyWhite` - Clean white background theme
49+
- `BuiltinTheme::PlotlyDark` - Dark theme
50+
- `BuiltinTheme::Seaborn` - Seaborn-style theme
51+
- `BuiltinTheme::SeabornWhitegrid` - Seaborn with white grid
52+
- `BuiltinTheme::SeabornDark` - Dark Seaborn theme
53+
- `BuiltinTheme::Matplotlib` - Matplotlib-style theme
54+
- `BuiltinTheme::Plotnine` - Plotnine-style theme
55+
56+
## Using Themes
57+
58+
To apply a theme to your plot, use the `template()` method on the layout:
59+
60+
```rust
61+
let theme = BuiltinTheme::Seaborn;
62+
let layout = Layout::new().template(theme.build());
63+
plot.set_layout(layout);
64+
```
65+
66+
The example above uses real Gapminder 2007 data showing the relationship between GDP per capita, life expectancy, and population size across different continents, with marker sizes representing population and colors representing continents.

examples/3d_charts/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ edition = "2021"
88
ndarray = "0.16"
99
rand = "0.9"
1010
plotly = { path = "../../plotly" }
11+
plotly_utils = { path = "../plotly_utils" }

examples/3d_charts/src/main.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use plotly::{
77
layout::{Axis, Camera, Layout, LayoutScene, Legend, Margin, ProjectionType},
88
Mesh3D, Plot, Scatter3D, Surface,
99
};
10+
use plotly_utils::write_example_to_html;
1011
use rand::Rng;
1112

1213
// 3D Scatter Plots
@@ -259,18 +260,6 @@ fn colorscale_plot(show: bool, file_name: &str) {
259260
}
260261
// ANCHOR_END: colorscale_plot
261262

262-
fn write_example_to_html(plot: &Plot, name: &str) -> String {
263-
std::fs::create_dir_all("./output").unwrap();
264-
// Write inline HTML
265-
let html = plot.to_inline_html(Some(name));
266-
let path = format!("./output/inline_{}.html", name);
267-
std::fs::write(path, html).unwrap();
268-
// Write standalone HTML
269-
let path = format!("./output/{}.html", name);
270-
plot.write_html(&path);
271-
path
272-
}
273-
274263
fn main() {
275264
// Change false to true on any of these lines to display the example.
276265
// Scatter3D Plots

examples/Cargo.toml

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

examples/basic_charts/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ edition = "2021"
77
[dependencies]
88
ndarray = "0.16"
99
plotly = { path = "../../plotly" }
10+
plotly_utils = { path = "../plotly_utils" }
1011
rand = "0.9"
1112
rand_distr = "0.5"

examples/basic_charts/src/main.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use plotly::{
1515
traces::table::{Cells, Header},
1616
Bar, Pie, Plot, Sankey, Scatter, ScatterPolar, Table,
1717
};
18+
use plotly_utils::write_example_to_html;
1819
use rand_distr::{Distribution, Normal, Uniform};
1920

2021
// Scatter Plots
@@ -996,18 +997,6 @@ fn grouped_donout_pie_charts(show: bool, file_name: &str) {
996997
}
997998
// ANCHOR_END: grouped_donout_pie_charts
998999

999-
fn write_example_to_html(plot: &Plot, name: &str) -> String {
1000-
std::fs::create_dir_all("./output").unwrap();
1001-
// Write inline HTML
1002-
let html = plot.to_inline_html(Some(name));
1003-
let path = format!("./output/inline_{}.html", name);
1004-
std::fs::write(path, html).unwrap();
1005-
// Write standalone HTML
1006-
let path = format!("./output/{}.html", name);
1007-
plot.write_html(&path);
1008-
path
1009-
}
1010-
10111000
fn main() {
10121001
// Change false to true on any of these lines to display the example.
10131002

examples/custom_controls/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ edition = "2021"
77
[dependencies]
88
itertools = "0.10"
99
plotly = { path = "../../plotly" }
10+
plotly_utils = { path = "../plotly_utils" }

examples/custom_controls/src/main.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use plotly::{
99
},
1010
Bar, HeatMap, Layout, Plot,
1111
};
12+
use plotly_utils::write_example_to_html;
1213

1314
/// Display a bar chart with an associated dropdown selector to show different
1415
/// data.
@@ -117,18 +118,6 @@ fn bar_chart_with_modifiable_bar_mode(show: bool, file_name: &str) {
117118
}
118119
// ANCHOR_END: colorscale_plot
119120

120-
fn write_example_to_html(plot: &Plot, name: &str) -> String {
121-
std::fs::create_dir_all("./output").unwrap();
122-
// Write inline HTML
123-
let html = plot.to_inline_html(Some(name));
124-
let path = format!("./output/inline_{}.html", name);
125-
std::fs::write(path, html).unwrap();
126-
// Write standalone HTML
127-
let path = format!("./output/{}.html", name);
128-
plot.write_html(&path);
129-
path
130-
}
131-
132121
fn main() {
133122
// Change false to true on any of these lines to display the example.
134123

examples/customization/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ build_html = "2.5.0"
99
rand = "0.9"
1010
ndarray = "0.16"
1111
plotly = { path = "../../plotly" }
12+
plotly_utils = { path = "../plotly_utils" }

examples/customization/src/main.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use plotly::{
1111
layout::{Center, DragMode, Mapbox, MapboxStyle, Margin},
1212
Configuration, DensityMapbox, Layout, Plot, Scatter, Scatter3D,
1313
};
14+
use plotly_utils::write_example_to_html;
1415
const DEFAULT_HTML_APP_NOT_FOUND: &str = "Could not find default application for HTML files.";
1516

1617
fn density_mapbox_responsive_autofill(show: bool, file_name: &str) {
@@ -130,18 +131,6 @@ fn show_with_default_app(temp_path: &str) {
130131
.expect(DEFAULT_HTML_APP_NOT_FOUND);
131132
}
132133

133-
fn write_example_to_html(plot: &Plot, name: &str) -> String {
134-
std::fs::create_dir_all("./output").unwrap();
135-
// Write inline HTML
136-
let html = plot.to_inline_html(Some(name));
137-
let path = format!("./output/inline_{}.html", name);
138-
std::fs::write(path, html).unwrap();
139-
// Write standalone HTML
140-
let path = format!("./output/{}.html", name);
141-
plot.write_html(&path);
142-
path
143-
}
144-
145134
fn main() {
146135
// Switch the boolean flag to `true` to display the example, otherwise manually
147136
// open the generated file in the `output` folder.

examples/financial_charts/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ edition = "2021"
77
[dependencies]
88
csv = "1.1"
99
plotly = { path = "../../plotly" }
10+
plotly_utils = { path = "../plotly_utils" }
1011
serde = "1.0"

examples/financial_charts/src/main.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::path::PathBuf;
66
use plotly::common::TickFormatStop;
77
use plotly::layout::{Axis, RangeSelector, RangeSlider, SelectorButton, SelectorStep, StepMode};
88
use plotly::{Candlestick, Layout, Ohlc, Plot, Scatter};
9+
use plotly_utils::write_example_to_html;
910
use serde::Deserialize;
1011

1112
#[derive(Deserialize)]
@@ -319,18 +320,6 @@ fn simple_ohlc_chart(show: bool, file_name: &str) {
319320
}
320321
// ANCHOR_END: simple_ohlc_chart
321322

322-
fn write_example_to_html(plot: &Plot, name: &str) -> String {
323-
std::fs::create_dir_all("./output").unwrap();
324-
// Write inline HTML
325-
let html = plot.to_inline_html(Some(name));
326-
let path = format!("./output/inline_{}.html", name);
327-
std::fs::write(path, html).unwrap();
328-
// Write standalone HTML
329-
let path = format!("./output/{}.html", name);
330-
plot.write_html(&path);
331-
path
332-
}
333-
334323
fn main() {
335324
// Change false to true on any of these lines to display the example.
336325

examples/images/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ plotly = { path = "../../plotly", features = [
1111
"plotly_image",
1212
"plotly_ndarray",
1313
] }
14+
15+
plotly_utils = { path = "../plotly_utils" }

examples/images/src/main.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use ndarray::arr2;
44
use plotly::{color::Rgb, image::ColorModel, Image, Plot};
5+
use plotly_utils::write_example_to_html;
56

67
fn basic_image(show: bool, file_name: &str) {
78
let w = Rgb::new(255, 255, 255);
@@ -102,18 +103,6 @@ fn trace_from_ndarray_rgba(show: bool, file_name: &str) {
102103
}
103104
}
104105

105-
fn write_example_to_html(plot: &Plot, name: &str) -> String {
106-
std::fs::create_dir_all("./output").unwrap();
107-
// Write inline HTML
108-
let html = plot.to_inline_html(Some(name));
109-
let path = format!("./output/inline_{}.html", name);
110-
std::fs::write(path, html).unwrap();
111-
// Write standalone HTML
112-
let path = format!("./output/{}.html", name);
113-
plot.write_html(&path);
114-
path
115-
}
116-
117106
fn main() {
118107
// Change false to true on any of these lines to display the example.
119108
basic_image(false, "basic_image");

examples/maps/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2021"
66

77
[dependencies]
88
plotly = { path = "../../plotly" }
9+
plotly_utils = { path = "../plotly_utils" }
910
csv = "1.3"
1011
reqwest = { version = "0.11", features = ["blocking"] }
1112

examples/maps/src/main.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use plotly::{
88
},
99
DensityMapbox, Layout, Plot, ScatterGeo, ScatterMapbox,
1010
};
11+
use plotly_utils::write_example_to_html;
1112

1213
fn scatter_mapbox(show: bool, file_name: &str) {
1314
let trace = ScatterMapbox::new(vec![45.5017], vec![-73.5673])
@@ -159,18 +160,6 @@ fn density_mapbox(show: bool, file_name: &str) {
159160
}
160161
}
161162

162-
fn write_example_to_html(plot: &Plot, name: &str) -> String {
163-
std::fs::create_dir_all("./output").unwrap();
164-
// Write inline HTML
165-
let html = plot.to_inline_html(Some(name));
166-
let path = format!("./output/inline_{}.html", name);
167-
std::fs::write(path, html).unwrap();
168-
// Write standalone HTML
169-
let path = format!("./output/{}.html", name);
170-
plot.write_html(&path);
171-
path
172-
}
173-
174163
fn main() {
175164
// Change false to true on any of these lines to display the example.
176165
scatter_mapbox(false, "scatter_mapbox");

examples/ndarray/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ edition = "2021"
77
[dependencies]
88
ndarray = "0.16"
99
plotly = { path = "../../plotly", features = ["plotly_ndarray"] }
10+
plotly_utils = { path = "../plotly_utils" }

examples/ndarray/src/main.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use ndarray::{Array, Ix1, Ix2};
44
use plotly::common::Mode;
55
use plotly::ndarray::ArrayTraces;
66
use plotly::{Plot, Scatter};
7+
use plotly_utils::write_example_to_html;
78

89
fn single_ndarray_trace(show: bool, file_name: &str) {
910
let n: usize = 11;
@@ -73,18 +74,6 @@ fn multiple_ndarray_traces_over_rows(show: bool, file_name: &str) {
7374
}
7475
}
7576

76-
fn write_example_to_html(plot: &Plot, name: &str) -> String {
77-
std::fs::create_dir_all("./output").unwrap();
78-
// Write inline HTML
79-
let html = plot.to_inline_html(Some(name));
80-
let path = format!("./output/inline_{}.html", name);
81-
std::fs::write(path, html).unwrap();
82-
// Write standalone HTML
83-
let path = format!("./output/{}.html", name);
84-
plot.write_html(&path);
85-
path
86-
}
87-
8877
fn main() {
8978
// Change false to true on any of these lines to display the example.
9079
single_ndarray_trace(false, "single_ndarray_trace");

examples/plotly_utils/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "plotly_utils"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
plotly = { path = "../../plotly" }

0 commit comments

Comments
 (0)