Skip to content

Commit

Permalink
provide alternative show_html method
Browse files Browse the repository at this point in the history
 - 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 <[email protected]>
  • Loading branch information
andrei-ng committed Jul 30, 2024
1 parent 341e20f commit e3e615a
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion plotly/src/plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P: AsRef<Path> + 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"))]
Expand Down Expand Up @@ -311,7 +323,8 @@ impl Plot {
pub fn write_html<P: AsRef<Path>>(&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();
Expand Down

0 comments on commit e3e615a

Please sign in to comment.