Skip to content

Commit e3e615a

Browse files
committed
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>
1 parent 341e20f commit e3e615a

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

plotly/src/plot.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,18 @@ impl Plot {
275275
Plot::show_with_default_app(temp_path);
276276
}
277277

278+
/// Display the fully rendered HTML `Plot` in the default system browser.
279+
///
280+
/// The HTML file is generated and saved in the provided filename as long as the path
281+
/// already exists, after the file is saved, it is read and displayed by the browser.
282+
#[cfg(not(target_family = "wasm"))]
283+
pub fn show_html<P: AsRef<Path> + std::clone::Clone>(&self, filename: P) {
284+
let path = filename.as_ref().to_str().unwrap();
285+
self.write_html(filename.clone());
286+
// Hand off the job of opening the browser to an OS-specific implementation.
287+
Plot::show_with_default_app(path);
288+
}
289+
278290
/// Display the fully rendered `Plot` as a static image of the given format
279291
/// in the default system browser.
280292
#[cfg(not(target_family = "wasm"))]
@@ -311,7 +323,8 @@ impl Plot {
311323
pub fn write_html<P: AsRef<Path>>(&self, filename: P) {
312324
let rendered = self.to_html();
313325

314-
let mut file = File::create(filename).unwrap();
326+
let mut file =
327+
File::create(filename).expect("Provided filepath does not exist or is not accessible");
315328
file.write_all(rendered.as_bytes())
316329
.expect("failed to write html output");
317330
file.flush().unwrap();

0 commit comments

Comments
 (0)