@@ -180,6 +180,60 @@ impl Kaleido {
180
180
181
181
Ok ( ( ) )
182
182
}
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
+ }
183
237
}
184
238
185
239
#[ cfg( test) ]
0 commit comments