From e3e615aa63f5e28f12fe8eaccee8b2b838cec101 Mon Sep 17 00:00:00 2001 From: Andrei Gherghescu <8067229+andrei-ng@users.noreply.github.com> Date: Tue, 30 Jul 2024 14:57:53 +0200 Subject: [PATCH] provide alternative show_html method - the show() method by default creates a random named file in `/tmp` dir, however in some cases `/tmp` is not available, e.g., snap's Firefox runs in an isolated enviroment and cannot access the system's `/tmp` - provide a method that calls write_html followed by show for easier access Closes #170 Signed-off-by: Andrei Gherghescu <8067229+andrei-ng@users.noreply.github.com> --- plotly/src/plot.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/plotly/src/plot.rs b/plotly/src/plot.rs index d02fe92..d958ca9 100644 --- a/plotly/src/plot.rs +++ b/plotly/src/plot.rs @@ -275,6 +275,18 @@ impl Plot { Plot::show_with_default_app(temp_path); } + /// Display the fully rendered HTML `Plot` in the default system browser. + /// + /// The HTML file is generated and saved in the provided filename as long as the path + /// already exists, after the file is saved, it is read and displayed by the browser. + #[cfg(not(target_family = "wasm"))] + pub fn show_html + std::clone::Clone>(&self, filename: P) { + let path = filename.as_ref().to_str().unwrap(); + self.write_html(filename.clone()); + // Hand off the job of opening the browser to an OS-specific implementation. + Plot::show_with_default_app(path); + } + /// Display the fully rendered `Plot` as a static image of the given format /// in the default system browser. #[cfg(not(target_family = "wasm"))] @@ -311,7 +323,8 @@ impl Plot { pub fn write_html>(&self, filename: P) { let rendered = self.to_html(); - let mut file = File::create(filename).unwrap(); + let mut file = + File::create(filename).expect("Provided filepath does not exist or is not accessible"); file.write_all(rendered.as_bytes()) .expect("failed to write html output"); file.flush().unwrap();