8000 added to_b64 · plotly/plotly.rs@106931e · GitHub
[go: up one dir, main page]

Skip to content

Commit 106931e

Browse files
mchantandrei-ng
authored andcommitted
added to_b64
1 parent dd57bf6 commit 106931e

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

plotly_kaleido/src/lib.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,60 @@ impl Kaleido {
180180

181181
Ok(())
182182
}
183+
184+
// similar to save, but returns b64 string
185+
pub fn to_b64(
186+
&self,
187+
plotly_data: &Value,
188+
format: &str,
189+
width: usize,
190+
height: usize,
191+
scale: f64,
192+
) -> Result<String, Box<dyn std::error::Error>> {
193+
194+
let p = self.cmd_path.as_path();
195+
let p = p.to_str().unwrap();
196+
let p = String::from(p);
197+
198+
let mut process = Command::new(p.as_str())
199+
.current_dir(self.cmd_path.parent().unwrap())
200+
.args([
201+
"plotly",
202+
"--disable-gpu",
203+
"--allow-file-access-from-files",
204+
"--disable-breakpad",
205+
"--disable-dev-shm-usage",
206+
"--disable-software-rasterizer",
207+
"--single-process",
208+
])
209+
.stdin(Stdio::piped())
210+
.stdout(Stdio::piped())
211+
.stderr(Stdio::piped())
212+
.spawn()
213+
.expect("failed to spawn Kaleido binary");
214+
215+
{
216+
let plot_data = PlotData::new(plotly_data, format, width, height, scale).to_json();
217+
let mut process_stdin = process.stdin.take().unwrap();
218+
process_stdin
219+
.write_all(plot_data.as_bytes())
220+
.expect("couldn't write to Kaleido stdin");
221+
process_stdin.flush()?;
222+
}
223+
224+
let output_lines = BufReader::new(process.stdout.take().unwrap()).lines();
225+
226+
let mut b64_str: String = "".into();
227+
for line in output_lines.map_while(Result::ok) {
228+
// println!("{}", &line);
229+
let res = KaleidoResult::from(line.as_str());
230+
if let Some(image_data) = res.result {
231+
b64_str = image_data
232+
}
233+
}
234+
235+
Ok(b64_str)
236+
}
183237
}
184238

185239
#[cfg(test)]

0 commit comments

Comments
 (0)
0