Skip to content

Commit 60669d3

Browse files
authored
Merge pull request #44 from foresterre/patch-impl-write
Add a method to render the plot to anything implementing Write
2 parents 7fed71c + a95b4c4 commit 60669d3

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

plotly/src/plot.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,25 @@ impl Plot {
246246
///
247247
/// In contrast to `Plot::show()` this will save the resulting html in a user specified location
248248
/// instead of the system temp directory.
249+
///
250+
/// In contrast to `Plot::write_html`, this will save the resulting html to a file located at a
251+
/// user specified location, instead of a writing it to anything that implements `std::io::Write`.
249252
pub fn to_html<P: AsRef<Path>>(&self, filename: P) {
253+
let mut file = File::create(filename.as_ref()).unwrap();
254+
255+
self.write_html(&mut file);
256+
}
257+
258+
/// Renders the contents of the `Plot` to HTML and outputs them to a writable buffer.
259+
///
260+
/// In contrast to `Plot::to_html`, this will save the resulting html to a byte buffer using the
261+
/// `std::io::Write` trait, instead of to a user specified file.
262+
pub fn write_html<W: Write>(&self, buffer: &mut W) {
250263
let rendered = self.render(false, "", 0, 0);
251264
let rendered = rendered.as_bytes();
252-
let mut file = File::create(filename.as_ref()).unwrap();
253-
file.write_all(rendered)
265+
266+
buffer
267+
.write_all(rendered)
254268
.expect("failed to write html output");
255269
}
256270

0 commit comments

Comments
 (0)