1
+ use std:: sync:: atomic:: { AtomicU64 , Ordering } ;
2
+ use std:: time:: { SystemTime , UNIX_EPOCH } ;
1
3
use std:: { fs:: File , io:: Write , path:: Path } ;
2
4
3
5
use askama:: Template ;
@@ -16,6 +18,8 @@ use serde::Serialize;
16
18
17
19
use crate :: { layout:: Frame , Configuration , Layout } ;
18
20
21
+ static SEED_COUNTER : AtomicU64 = AtomicU64 :: new ( 0 ) ;
22
+
19
23
#[ derive( Template ) ]
20
24
#[ template( path = "plot.html" , escape = "none" ) ]
21
25
struct PlotTemplate < ' a > {
@@ -270,7 +274,8 @@ impl Plot {
270
274
271
275
// Set up the temp file with a unique filename.
272
276
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 ) ;
274
279
plot_name. push_str ( ".html" ) ;
275
280
plot_name = format ! ( "plotly_{plot_name}" ) ;
276
281
temp. push ( plot_name) ;
@@ -313,7 +318,8 @@ impl Plot {
313
318
314
319
// Set up the temp file with a unique filename.
315
320
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 ) ;
317
323
plot_name. push_str ( ".html" ) ;
318
324
plot_name = format ! ( "plotly_{plot_name}" ) ;
319
325
temp. push ( plot_name) ;
@@ -371,13 +377,16 @@ impl Plot {
371
377
pub fn to_inline_html ( & self , plot_div_id : Option < & str > ) -> String {
372
378
let plot_div_id = match plot_div_id {
373
379
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
+ }
375
383
} ;
376
384
self . render_inline ( & plot_div_id)
377
385
}
378
386
379
387
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 ) ;
381
390
382
391
let tmpl = JupyterNotebookPlotTemplate {
383
392
plot : self ,
@@ -869,6 +878,17 @@ impl Plot {
869
878
. spawn ( )
870
879
. expect ( DEFAULT_HTML_APP_NOT_FOUND ) ;
871
880
}
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
+ }
872
892
}
873
893
874
894
impl PartialEq for Plot {
0 commit comments