-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathlib.rs
More file actions
404 lines (365 loc) · 13 KB
/
lib.rs
File metadata and controls
404 lines (365 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//! # Plotly Kaleido
//! Plotly Kaleido implements the `kaleido` feature for [Plotly.rs](https://github.com/plotly/plotly.rs)
//!
//! The `kaleido` feature enables `Plot` conversion to the following output
//! formats: png, jpeg, webp, svg, pdf and eps. It has the added benefit over
//! the `orca` feature in that there is no need for an additional installation
//! for the feature to function properly. The Kaleido precompiled binaries are
//! packaged redistributed from the original repository [plotly/Kaleido](https://github.com/plotly/Kaleido).
//!
//! Note that [plotly/Kaleido](https://github.com/plotly/Kaleido) is still in pre-release and as such the `kaleido`
//! feature should be considered in pre-release mode as well.
#![cfg(not(target_family = "wasm"))]
use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use base64::{engine::general_purpose, Engine as _};
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[allow(dead_code)]
#[derive(Deserialize, Debug)]
struct KaleidoResult {
code: i32,
message: Option<String>,
#[serde(rename = "pdfBgColor")]
pdf_background_color: Option<String>,
format: Option<String>,
result: Option<String>,
width: Option<usize>,
height: Option<usize>,
scale: Option<f64>,
version: Option<String>,
}
impl KaleidoResult {
fn from(result: &str) -> KaleidoResult {
serde_json::from_str(result).unwrap()
}
}
#[derive(Serialize)]
struct PlotData<'a> {
// TODO: as with `data`, it would be much better if this were a plotly::ImageFormat, but
// problems with cyclic dependencies.
format: String,
width: usize,
height: usize,
scale: f64,
// TODO: it would be great if this could be a plotly::Plot, but with the current workspace set
// up, that would be a cyclic dependency.
data: &'a Value,
}
impl<'a> PlotData<'a> {
fn new(data: &'a Value, format: &str, width: usize, height: usize, scale: f64) -> PlotData<'a> {
PlotData {
format: format.to_string(),
width,
height,
scale,
data,
}
}
fn to_json(&self) -> String {
serde_json::to_string(self).unwrap()
}
}
#[derive(Default)]
pub struct Kaleido {
cmd_path: PathBuf,
}
impl Kaleido {
const KALEIDO_PATH_ENV: &str = "KALEIDO_PATH";
pub fn new() -> Kaleido {
use std::env;
let path = match env::var(Self::KALEIDO_PATH_ENV) {
Ok(runtime_env) => runtime_env,
Err(runtime_env_err) => match option_env!("KALEIDO_COMPILE_TIME_DLD_PATH") {
Some(compile_time_path) => compile_time_path.to_string(),
None => {
println!("{}: {}", Self::KALEIDO_PATH_ENV, runtime_env_err);
println!("Use `kaleido_download` feature to automatically download, install and use Kaleido when targeting applications that run on the host machine.");
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
);
std::process::exit(1);
}
},
};
let path = match Kaleido::binary_path(&path) {
Ok(kaleido_path) => kaleido_path,
Err(msg) => panic!("Failed tu use Kaleido binary at {} due to {}", path, msg),
};
Kaleido { cmd_path: path }
}
fn binary_path(dld_path: &str) -> Result<PathBuf, &'static str> {
let mut p = PathBuf::from(dld_path);
p = Self::os_binary_path(p);
if !p.exists() {
return Err("could not find kaleido executable in path");
}
Ok(p)
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn os_binary_path(path: PathBuf) -> PathBuf {
match path.join("kaleido").canonicalize() {
Ok(v) => v,
Err(e) => {
println!(
"Failed to find Kaleido binary at '{}': {e}",
path.to_string_lossy()
);
panic!("{e}");
}
}
}
#[cfg(target_os = "windows")]
fn os_binary_path(path: PathBuf) -> PathBuf {
path.join("kaleido.cmd")
}
/// Generate a static image from a Plotly graph and save it to a file
pub fn save(
&self,
dst: &Path,
plotly_data: &Value,
format: &str,
width: usize,
height: usize,
scale: f64,
) -> Result<(), Box<dyn std::error::Error>> {
let mut dst = PathBuf::from(dst);
dst.set_extension(format);
let image_data = self.convert(plotly_data, format, width, height, scale)?;
let data = match format {
"svg" | "eps" => image_data.as_bytes(),
_ => &general_purpose::STANDARD.decode(image_data).unwrap(),
};
let mut file = File::create(dst.as_path())?;
file.write_all(data)?;
file.flush()?;
Ok(())
}
/// Generate a static image from a Plotly graph and return it as a String
/// The output may be base64 encoded or a plain text depending on the image
/// format provided as argument. SVG and EPS are returned in plain text
/// while JPEG, PNG, WEBP will be returned as a base64 encoded string.
pub fn image_to_string(
&self,
plotly_data: &Value,
format: &str,
width: usize,
height: usize,
scale: f64,
) -> Result<String, Box<dyn std::error::Error>> {
let image_data = self.convert(plotly_data, format, width, height, scale)?;
Ok(image_data)
}
/// Convert the Plotly graph to a static image using Kaleido and return the
/// result as a String
pub fn convert(
&self,
plotly_data: &Value,
format: &str,
width: usize,
height: usize,
scale: f64,
) -> Result<String, Box<dyn std::error::Error>> {
let p = self.cmd_path.to_str().unwrap();
// Removed flag 'disable-gpu' as it causes issues on MacOS and other platforms
// see Kaleido issue #323
let cmd_args = vec![
"plotly",
"--allow-file-access-from-files",
"--disable-breakpad",
"--disable-dev-shm-usage",
"--disable-software-rasterizer",
"--single-process",
"--no-sandbox",
];
#[allow(clippy::zombie_processes)]
let mut process = Command::new(p)
.current_dir(self.cmd_path.parent().unwrap())
.args(cmd_args)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.unwrap_or_else(|_| {
panic!(
"{}",
format!(
"failed to spawn Kaleido binary at {}",
self.cmd_path.to_string_lossy()
)
.to_string()
)
});
{
let plot_data = PlotData::new(plotly_data, format, width, height, scale).to_json();
let mut process_stdin = process.stdin.take().unwrap();
process_stdin
.write_all(plot_data.as_bytes())
.expect("couldn't write to Kaleido stdin");
process_stdin.flush()?;
}
let output_lines = BufReader::new(process.stdout.take().unwrap()).lines();
for line in output_lines.map_while(Result::ok) {
let res = KaleidoResult::from(line.as_str());
if let Some(image_data) = res.result {
// TODO: this should be refactored
// The assumption is that KaleidoResult contains a single image.
// We should end the loop on the first valid one.
// If that is not the case, prior implementation would have returned the last
// valid image
return Ok(image_data);
}
}
// Don't eat up Kaleido/Chromium errors but show them in the terminal
println!("Kaleido failed to generate static image for format: {format}.");
println!("Kaleido stderr output:");
let stderr = process.stderr.take().unwrap();
let stderr_lines = BufReader::new(stderr).lines();
for line in stderr_lines {
let line = line.unwrap();
eprintln!("{}", line);
}
Ok(String::default())
}
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use serde_json::{json, to_value};
use super::*;
fn create_test_plot() -> Value {
to_value(json!({
"data": [
{
"name": "Surface",
"type": "surface",
"x": [
1.0,
2.0,
3.0
],
"y": [
4.0,
5.0,
6.0
],
"z": [
[
1.0,
2.0,
3.0
],
[
4.0,
5.0,
6.0
],
[
7.0,
8.0,
9.0
]
]
}
],
"layout": {}
}))
.unwrap()
}
#[test]
fn can_find_kaleido_executable() {
let _k = Kaleido::new();
}
#[test]
fn plot_data_to_json() {
let test_plot = create_test_plot();
let kaleido_data = PlotData::new(&test_plot, "png", 400, 500, 1.);
let expected = json!({
"data": test_plot,
"format": "png",
"width": 400,
"height": 500,
"scale": 1.0
});
assert_eq!(to_value(kaleido_data).unwrap(), expected);
}
// For MacOS failures, see issue #241 and upstream https://github.com/plotly/Kaleido/issues/323 is resolved
#[test]
fn save_png() {
let test_plot = create_test_plot();
let k = Kaleido::new();
let dst = PathBuf::from("example.png");
let r = k.save(dst.as_path(), &test_plot, "png", 1200, 900, 4.5);
assert!(r.is_ok());
assert!(dst.exists());
let metadata = std::fs::metadata(&dst).expect("Could not retrieve file metadata");
let file_size = metadata.len();
assert!(file_size > 0,);
assert!(std::fs::remove_file(dst.as_path()).is_ok());
}
#[test]
fn save_jpeg() {
let test_plot = create_test_plot();
let k = Kaleido::new();
let dst = PathBuf::from("example.jpeg");
let r = k.save(dst.as_path(), &test_plot, "jpeg", 1200, 900, 4.5);
assert!(r.is_ok());
assert!(dst.exists());
let metadata = std::fs::metadata(&dst).expect("Could not retrieve file metadata");
let file_size = metadata.len();
assert!(file_size > 0,);
assert!(std::fs::remove_file(dst.as_path()).is_ok());
}
#[test]
fn save_webp() {
let test_plot = create_test_plot();
let k = Kaleido::new();
let dst = PathBuf::from("example.webp");
let r = k.save(dst.as_path(), &test_plot, "webp", 1200, 900, 4.5);
assert!(r.is_ok());
assert!(dst.exists());
let metadata = std::fs::metadata(&dst).expect("Could not retrieve file metadata");
let file_size = metadata.len();
assert!(file_size > 0,);
assert!(std::fs::remove_file(dst.as_path()).is_ok());
}
#[test]
fn save_svg() {
let test_plot = create_test_plot();
let k = Kaleido::new();
let dst = PathBuf::from("example.svg");
let r = k.save(dst.as_path(), &test_plot, "svg", 1200, 900, 4.5);
assert!(r.is_ok());
assert!(dst.exists());
let metadata = std::fs::metadata(&dst).expect("Could not retrieve file metadata");
let file_size = metadata.len();
assert!(file_size > 0,);
assert!(std::fs::remove_file(dst.as_path()).is_ok());
}
#[test]
fn save_pdf() {
let test_plot = create_test_plot();
let k = Kaleido::new();
let dst = PathBuf::from("example.pdf");
let r = k.save(dst.as_path(), &test_plot, "pdf", 1200, 900, 4.5);
assert!(r.is_ok());
assert!(dst.exists());
let metadata = std::fs::metadata(&dst).expect("Could not retrieve file metadata");
let file_size = metadata.len();
assert!(file_size > 0,);
assert!(std::fs::remove_file(dst.as_path()).is_ok());
}
// Kaleido generates empty eps files
#[test]
#[ignore]
fn save_eps() {
let test_plot = create_test_plot();
let k = Kaleido::new();
let dst = PathBuf::from("example.eps");
let r = k.save(dst.as_path(), &test_plot, "eps", 1200, 900, 4.5);
assert!(r.is_ok());
assert!(std::fs::remove_file(dst.as_path()).is_ok());
}
}