1+ use std:: sync:: atomic:: { AtomicU64 , Ordering } ;
2+ use std:: time:: { SystemTime , UNIX_EPOCH } ;
13use std:: { fs:: File , io:: Write , path:: Path } ;
24
35use askama:: Template ;
@@ -16,6 +18,8 @@ use serde::Serialize;
1618
1719use crate :: { layout:: Frame , Configuration , Layout } ;
1820
21+ static SEED_COUNTER : AtomicU64 = AtomicU64 :: new ( 0 ) ;
22+
1923#[ derive( Template ) ]
2024#[ template( path = "plot.html" , escape = "none" ) ]
2125struct PlotTemplate < ' a > {
@@ -270,7 +274,8 @@ impl Plot {
270274
271275 // Set up the temp file with a unique filename.
272276 let mut temp = env:: temp_dir ( ) ;
273- let mut plot_name = Alphanumeric . sample_string ( & mut SmallRng :: seed_from_u64 ( 42 ) , 22 ) ;
277+ let mut plot_name =
278+ Alphanumeric . sample_string ( & mut SmallRng :: seed_from_u64 ( Self :: generate_seed ( ) ) , 22 ) ;
274279 plot_name. push_str ( ".html" ) ;
275280 plot_name = format ! ( "plotly_{plot_name}" ) ;
276281 temp. push ( plot_name) ;
@@ -313,7 +318,8 @@ impl Plot {
313318
314319 // Set up the temp file with a unique filename.
315320 let mut temp = env:: temp_dir ( ) ;
316- let mut plot_name = Alphanumeric . sample_string ( & mut SmallRng :: seed_from_u64 ( 42 ) , 22 ) ;
321+ let mut plot_name =
322+ Alphanumeric . sample_string ( & mut SmallRng :: seed_from_u64 ( Self :: generate_seed ( ) ) , 22 ) ;
317323 plot_name. push_str ( ".html" ) ;
318324 plot_name = format ! ( "plotly_{plot_name}" ) ;
319325 temp. push ( plot_name) ;
@@ -371,13 +377,16 @@ impl Plot {
371377 pub fn to_inline_html ( & self , plot_div_id : Option < & str > ) -> String {
372378 let plot_div_id = match plot_div_id {
373379 Some ( id) => id. to_string ( ) ,
374- None => Alphanumeric . sample_string ( & mut SmallRng :: seed_from_u64 ( 42 ) , 20 ) ,
380+ None => {
381+ Alphanumeric . sample_string ( & mut SmallRng :: seed_from_u64 ( Self :: generate_seed ( ) ) , 20 )
382+ }
375383 } ;
376384 self . render_inline ( & plot_div_id)
377385 }
378386
379387 fn to_jupyter_notebook_html ( & self ) -> String {
380- let plot_div_id = Alphanumeric . sample_string ( & mut SmallRng :: seed_from_u64 ( 42 ) , 20 ) ;
388+ let plot_div_id =
389+ Alphanumeric . sample_string ( & mut SmallRng :: seed_from_u64 ( Self :: generate_seed ( ) ) , 20 ) ;
381390
382391 let tmpl = JupyterNotebookPlotTemplate {
383392 plot : self ,
@@ -869,6 +878,17 @@ impl Plot {
869878 . spawn ( )
870879 . expect ( DEFAULT_HTML_APP_NOT_FOUND ) ;
871880 }
881+
882+ /// Generate unique seeds for SmallRng such that file names and div names
883+ /// are unique random for each call
884+ pub ( crate ) fn generate_seed ( ) -> u64 {
885+ let time = SystemTime :: now ( )
886+ . duration_since ( UNIX_EPOCH )
887+ . unwrap_or_default ( )
888+ . as_nanos ( ) as u64 ;
889+ let counter = SEED_COUNTER . fetch_add ( 1 , Ordering :: Relaxed ) ;
890+ time ^ counter
891+ }
872892}
873893
874894impl PartialEq for Plot {
0 commit comments