Skip to content

Commit 74bf4f9

Browse files
committed
update readme related to kaleido usage and kaleido_download feature
Signed-off-by: Andrei Gherghescu <[email protected]>
1 parent 4973679 commit 74bf4f9

File tree

6 files changed

+54
-30
lines changed

6 files changed

+54
-30
lines changed

README.md

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
* [Introduction](#introduction)
4141
* [Basic Usage](#basic-usage)
4242
* [Exporting an Interactive Plot](#exporting-an-interactive-plot)
43-
* [Exporting a Static Image](#exporting-a-static-image)
43+
* [Exporting Static Images with Kaleido](#exporting-static-images-with-kaleido)
4444
* [Usage Within a Wasm Environment](#usage-within-a-wasm-environment)
4545
* [Crate Feature Flags](#crate-feature-flags)
4646
* [Contributing](#contributing)
@@ -96,18 +96,38 @@ If you only want to view the plot in the browser quickly, use the `Plot.show()`
9696
plot.show(); // The default web browser will open, displaying an interactive plot
9797
```
9898

99-
## Exporting a Static Image
99+
## Exporting Static Images with Kaleido
100100

101-
To save a plot as a static image, the `kaleido` feature is required:
101+
To save a plot as a static image, the `kaleido` feature is required as well as installing an **external dependency**.
102102

103+
### Kaleido external dependency
104+
105+
When developing applications for your host, enabling both `kaleido` and `kaleido_download` features will ensure that the `kaleido` binary is downloaded for your system's architecture at compile time. After download, it is unpacked into a specific path, e.g., on Linux this is `/home/USERNAME/.config/kaleido`. With these two features enabled, static images can be exported as described in the next section as long as the application run on the same host where where this crate was compiled on.
106+
107+
When the applications developed with `plotly.rs` are intended for other targets or when the user wants to control where the `kaleido` binary is installed then Kaleido must be manually downloaded and installed. Setting the environment variable `KALEIDO_PATH=/path/installed/kaleido/` will ensure that applications that were built with the `kaleido` feature enabled can locate the `kaleido` executable and use it to generate static images.
108+
109+
Kaleido binaries are available on Github [release page](https://github.com/plotly/Kaleido/releases). It currently supports Linux(`x86_64`), Windows(`x86_64`) and MacOS(`x86_64`/`aarch64`).
110+
111+
## Exporting a Static Images
112+
113+
Enable the `kaleido` feature and opt in for automatic downloading of the `kaleido` binaries by doing the following
114+
115+
```toml
116+
# Cargo.toml
117+
118+
[dependencies]
119+
plotly = { version = "0.11", features = ["kaleido", "kaleido_download"] }
120+
```
121+
122+
Alternatively, enable only the `kaleido` feature and manually install Kaleido.
103123
```toml
104124
# Cargo.toml
105125

106126
[dependencies]
107127
plotly = { version = "0.11", features = ["kaleido"] }
108128
```
109129

110-
With this feature enabled, plots can be saved as any of `png`, `jpeg`, `webp`, `svg`, `pdf` and `eps`. Note that the plot will be a static image, i.e. they will be non-interactive.
130+
With the feature enabled, plots can be saved as any of `png`, `jpeg`, `webp`, `svg`, `pdf` and `eps`. Note that the plot will be a static image, i.e. they will be non-interactive.
111131

112132
Exporting a simple plot looks like this:
113133

@@ -121,12 +141,6 @@ plot.add_trace(trace);
121141
plot.write_image("out.png", ImageFormat::PNG, 800, 600, 1.0);
122142
```
123143

124-
### _Kaleido dependency_
125-
126-
On your host, when building this project with the `kaleido` feature enabled the Kaleido binary is downloaded automatically for your system's architecture at compile time from the official Kaleido [release page](https://github.com/plotly/Kaleido/releases). This library currently supports `x86_64` on Linux and Windows, and both `x86_64` and `aarch64` on macOS.
127-
128-
When building application for other targets that depend on this feature, the `Kaleido` binary will need to be installed manually on the target machine. Currently, the location where the binary is expected is hardcoded depending on the target OS. E.g., on Linux this defaults to `~/.config/kaleido`. This is defined in source code [here](https://github.com/plotly/plotly.rs/blob/1405731b5121c1343b491e307222a21ef4becc5e/plotly_kaleido/src/lib.rs#L89)
129-
130144
## Usage Within a Wasm Environment
131145

132146
Using `Plotly.rs` in a Wasm-based frontend framework is possible by enabling the `wasm` feature:
@@ -198,6 +212,13 @@ The following feature flags are available:
198212

199213
Adds plot save functionality to the following formats: `png`, `jpeg`, `webp`, `svg`, `pdf` and `eps`.
200214

215+
Requires `Kaleido` to have been previously installed on the host machine. See the following feature flag and [Kaleido external dependency](#kaleido-external-dependency).
216+
217+
### `kaleido_download`
218+
219+
Enable download and install of Kaleido binary at build time from [Kaleido releases](https://github.com/plotly/Kaleido/releases/) on the host machine.
220+
See [Kaleido external dependency](#kaleido-external-dependency) for more details.
221+
201222
### `plotly_image`
202223

203224
Adds trait implementations so that `image::RgbImage` and `image::RgbaImage` can be used more directly with the `plotly::Image` trace.

examples/kaleido/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ authors = [
88
edition = "2021"
99

1010
[dependencies]
11-
plotly = { path = "../../plotly", features = ["kaleido", "kaleido_fetch"] }
11+
plotly = { path = "../../plotly", features = ["kaleido", "kaleido_download"] }

examples/kaleido/src/main.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@ fn main() {
55
let trace = Scatter::new(vec![0, 1, 2], vec![2, 1, 0]);
66
plot.add_trace(trace);
77

8-
// Adjust these arguments to set the image format, width and height of the
8+
// Adjust these arguments to set the width and height of the
99
// output image.
1010
let filename = "out";
11-
let image_format = ImageFormat::PNG;
1211
let width = 800;
1312
let height = 600;
1413
let scale = 1.0;
1514

1615
// The image will be saved to format!("{filename}.{image_format}") relative to
1716
// the current working directory.
18-
plot.write_image(filename, image_format, width, height, scale);
17+
plot.write_image(filename, ImageFormat::EPS, width, height, scale);
18+
plot.write_image(filename, ImageFormat::JPEG, width, height, scale);
19+
plot.write_image(filename, ImageFormat::PDF, width, height, scale);
20+
plot.write_image(filename, ImageFormat::PNG, width, height, scale);
21+
plot.write_image(filename, ImageFormat::SVG, width, height, scale);
22+
plot.write_image(filename, ImageFormat::WEBP, width, height, scale);
23+
24+
let _svg_string = plot.to_svg(width, height, scale);
1925
}

plotly/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ exclude = ["target/*"]
1515

1616
[features]
1717
kaleido = ["plotly_kaleido"]
18-
kaleido_fetch = ["plotly_kaleido/download"]
18+
kaleido_download = ["plotly_kaleido/download"]
1919

2020
plotly_ndarray = ["ndarray"]
2121
plotly_image = ["image"]

plotly/src/plot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ mod tests {
809809
assert!(!dst.exists());
810810
}
811811

812-
#[cfg(target_os = "linux")]
812+
#[cfg(not(target_os = "macos"))]
813813
#[test]
814814
#[cfg(feature = "kaleido")]
815815
fn test_save_to_webp() {

plotly_kaleido/src/lib.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl Kaleido {
8787
Some(compile_time_path) => compile_time_path.to_string(),
8888
None => {
8989
println!("{}: {}", Self::KALEIDO_PATH_ENV, runtime_env_err);
90-
println!("Use `kaleido_fetch` feature to automatically download, install and use Kaleido when targeting applications that run on the host machine.");
90+
println!("Use `kaleido_download` feature to automatically download, install and use Kaleido when targeting applications that run on the host machine.");
9191
println!("Use `{}` environment variable when targeting applications intended to run on different machines. Manually install Kaleido on the target machine and point {} to the installation location.", Self::KALEIDO_PATH_ENV, Self::KALEIDO_PATH_ENV
9292
);
9393
std::process::exit(1);
@@ -227,22 +227,19 @@ impl Kaleido {
227227
let output_lines = BufReader::new(process.stdout.take().unwrap()).lines();
228228
for line in output_lines.map_while(Result::ok) {
229229
let res = KaleidoResult::from(line.as_str());
230-
match res.result {
231-
Some(image_data) => {
232-
// TODO: this should be refactored
233-
// The assumption is that KaleidoResult contains a single image.
234-
// We should end the loop on the first valid one.
235-
// If that is not the case, prior implementation would have returned the last
236-
// valid image
237-
return Ok(image_data);
238-
}
239-
None => {
240-
println!("empty line from Kaleido stdout");
241-
}
230+
if let Some(image_data) = res.result {
231+
// TODO: this should be refactored
232+
// The assumption is that KaleidoResult contains a single image.
233+
// We should end the loop on the first valid one.
234+
// If that is not the case, prior implementation would have returned the last
235+
// valid image
236+
return Ok(image_data);
242237
}
243238
}
244239

245-
// Don't eat up Kaleido/Chromiu erros but show them in the terminal
240+
// Don't eat up Kaleido/Chromium errors but show them in the terminal
241+
println!("Kaleido failed to generate static image for format: {format}.");
242+
println!("Kaleido stderr output:");
246243
let stderr = process.stderr.take().unwrap();
247244
let stderr_lines = BufReader::new(stderr).lines();
248245
for line in stderr_lines {

0 commit comments

Comments
 (0)