Skip to content

Commit 067e8c0

Browse files
committed
wip: fix readme for new kaleido feature
Signed-off-by: Andrei Gherghescu <[email protected]>
1 parent 4973679 commit 067e8c0

File tree

5 files changed

+42
-26
lines changed

5 files changed

+42
-26
lines changed

README.md

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,16 @@ plot.show(); // The default web browser will open, displaying an interactive plo
9898

9999
## Exporting a Static Image
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**.
102+
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`.
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 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+
102111

103112
```toml
104113
# Cargo.toml
@@ -121,12 +130,6 @@ plot.add_trace(trace);
121130
plot.write_image("out.png", ImageFormat::PNG, 800, 600, 1.0);
122131
```
123132

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-
130133
## Usage Within a Wasm Environment
131134

132135
Using `Plotly.rs` in a Wasm-based frontend framework is possible by enabling the `wasm` feature:
@@ -198,6 +201,16 @@ The following feature flags are available:
198201

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

204+
Requires `Kaleido` to have been previously installed on the host machine. See below feature flag and [click on this link](#my-multi-word-header).
205+
206+
### `kaleido_download`
207+
208+
Enable download and install of Kaleido binary at build time from [Kaleido releases](https://github.com/plotly/Kaleido/releases/) on the host machine.
209+
210+
211+
212+
This will ensure that your applications can export plots to static images on the host machine. However, when porting your applications to a different machine, `Kaleido` will have to be installed manually. See [Setting up Kaleido](#my-multi-word-header).
213+
201214
### `plotly_image`
202215

203216
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_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)