Skip to content

Commit a95b4c4

Browse files
committed
Add a method to render the plot to anything implementing Write
Plotly supported rendering the plot to HTML, and writing the output to a file, located at a user given path. Often however, it's desirable to split the process of rendering and the process of outputting the rendering. In addition, writing to a path is not always the most flexible, since you don't always have a file path. It can then be useful to be able to write the rendered entity to any writable buffer, which can be accomplished by using the std::io::Write trait instead of a path, which is what this change set does.
1 parent 416a9c6 commit a95b4c4

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

plotly/src/plot.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ pub enum ImageFormat {
5454
EPS,
5555
}
5656

57-
5857
/// A struct that implements `Trace` can be serialized to json format that is understood by Plotly.js.
5958
pub trait Trace {
6059
fn serialize(&self) -> String;
@@ -120,7 +119,6 @@ plot.save("filename", ImageFormat::PNG, width, height, scale);
120119
See https://igiagkiozis.github.io/plotly/content/getting_started.html for further details.
121120
"#;
122121

123-
124122
impl Plot {
125123
/// Create a new `Plot`.
126124
pub fn new() -> Plot {
@@ -239,11 +237,25 @@ impl Plot {
239237
///
240238
/// In contrast to `Plot::show()` this will save the resulting html in a user specified location
241239
/// instead of the system temp directory.
240+
///
241+
/// In contrast to `Plot::write_html`, this will save the resulting html to a file located at a
242+
/// user specified location, instead of a writing it to anything that implements `std::io::Write`.
242243
pub fn to_html<P: AsRef<Path>>(&self, filename: P) {
244+
let mut file = File::create(filename.as_ref()).unwrap();
245+
246+
self.write_html(&mut file);
247+
}
248+
249+
/// Renders the contents of the `Plot` to HTML and outputs them to a writable buffer.
250+
///
251+
/// In contrast to `Plot::to_html`, this will save the resulting html to a byte buffer using the
252+
/// `std::io::Write` trait, instead of to a user specified file.
253+
pub fn write_html<W: Write>(&self, buffer: &mut W) {
243254
let rendered = self.render(false, "", 0, 0);
244255
let rendered = rendered.as_bytes();
245-
let mut file = File::create(filename.as_ref()).unwrap();
246-
file.write_all(rendered)
256+
257+
buffer
258+
.write_all(rendered)
247259
.expect("failed to write html output");
248260
}
249261

@@ -280,7 +292,10 @@ impl Plot {
280292
/// Display plot in Jupyter Notebook.
281293
pub fn notebook_display(&self) {
282294
let plot_data = self.to_jupyter_notebook_html();
283-
println!("EVCXR_BEGIN_CONTENT text/html\n{}\nEVCXR_END_CONTENT", plot_data);
295+
println!(
296+
"EVCXR_BEGIN_CONTENT text/html\n{}\nEVCXR_END_CONTENT",
297+
plot_data
298+
);
284299
}
285300

286301
/// Display plot in Jupyter Lab.

0 commit comments

Comments
 (0)