@@ -40,6 +40,20 @@ define([
40
40
}
41
41
) ;
42
42
43
+ function count_init_cells ( ) {
44
+ console . log ( log_prefix , 'counting initialization cells' ) ;
45
+ var num = 0 ;
46
+ var cells = Jupyter . notebook . get_cells ( ) ;
47
+ for ( var ii = 0 ; ii < cells . length ; ii ++ ) {
48
+ var cell = cells [ ii ] ;
49
+ if ( ( cell instanceof codecell . CodeCell ) && cell . metadata . init_cell === true ) {
50
+ num ++ ;
51
+ }
52
+ }
53
+ console . log ( log_prefix , 'found ' + num + ' initialization cell' + ( num !== 1 ? 's' : '' ) ) ;
54
+ return num
55
+ }
56
+
43
57
function run_init_cells ( ) {
44
58
console . log ( log_prefix , 'running all initialization cells' ) ;
45
59
var num = 0 ;
@@ -103,24 +117,38 @@ define([
103
117
}
104
118
105
119
if ( options . run_on_kernel_ready ) {
106
- if ( ! Jupyter . notebook . trusted ) {
107
- dialog . modal ( {
108
- title : 'Initialization cells in untrusted notebook' ,
109
- body : 'This notebook is not trusted, so initialization cells will not be automatically run on kernel load. You can still run them manually, though.' ,
110
- buttons : { 'OK' : { 'class' : 'btn-primary' } } ,
111
- notebook : Jupyter . notebook ,
112
- keyboard_manager : Jupyter . keyboard_manager ,
113
- } ) ;
114
- return ;
120
+ var num = count_init_cells ( ) ;
121
+
122
+ if ( num ) {
123
+ if ( Jupyter . notebook . trusted ) {
124
+ run_init_cells_asap ( )
125
+ }
126
+ else {
127
+ dialog . modal ( {
128
+ title : 'Untrusted notebook with initialization code' ,
129
+ body : num + ' initialization code cell' + ( num !== 1 ? 's' : '' ) + ' was found but not run since this notebook is untrusted.' ,
130
+ buttons : {
131
+ 'Trust notebook' : {
132
+ 'class' : 'btn-danger' ,
133
+ 'click' : ( ) => Jupyter . notebook . trust_notebook ( )
134
+ } ,
135
+ 'Do nothing' : { 'class' : 'btn-primary' }
136
+ } ,
137
+ notebook : Jupyter . notebook ,
138
+ keyboard_manager : Jupyter . keyboard_manager ,
139
+ } ) ;
140
+ }
115
141
}
142
+ }
143
+ }
116
144
117
- if ( Jupyter . notebook && Jupyter . notebook . kernel && Jupyter . notebook . kernel . info_reply . status === 'ok' ) {
118
- // kernel is already ready
119
- run_init_cells ( ) ;
120
- }
121
- // whenever a (new) kernel becomes ready, run all initialization cells
122
- events . on ( 'kernel_ready.Kernel' , run_init_cells ) ;
145
+ function run_init_cells_asap ( ) {
146
+ if ( Jupyter . notebook && Jupyter . notebook . kernel && Jupyter . notebook . kernel . info_reply . status === 'ok' ) {
147
+ // kernel is already ready
148
+ run_init_cells ( ) ;
123
149
}
150
+ // whenever a (new) kernel becomes ready, run all initialization cells
151
+ events . on ( 'kernel_ready.Kernel' , run_init_cells ) ;
124
152
}
125
153
126
154
return {
0 commit comments