@@ -33,6 +33,8 @@ define([
33
33
34
34
// defaults, overridden by server's config
35
35
var options = {
36
+ clear_timings_on_clear_output : false ,
37
+ clear_timings_on_kernel_restart : false ,
36
38
default_kernel_to_utc : true ,
37
39
display_absolute_format : 'HH:mm:ss YYYY-MM-DD' ,
38
40
display_absolute_timings : true ,
@@ -43,7 +45,7 @@ define([
43
45
color : '#00bb00' ,
44
46
} ,
45
47
relative_timing_update_period : 10 ,
46
- template : {
48
+ template : {
47
49
executed : 'executed in ${duration}, finished ${end_time}' ,
48
50
queued : 'execution queued ${start_time}' ,
49
51
} ,
@@ -79,60 +81,104 @@ define([
79
81
} ;
80
82
}
81
83
82
- function toggle_timing_display ( cell , vis ) {
83
- if ( cell instanceof CodeCell ) {
84
- var ce = cell . element ;
85
- var timing_area = ce . find ( '.timing_area' ) ;
86
- if ( timing_area . length > 0 ) {
87
- if ( vis === undefined ) {
88
- vis = ! timing_area . is ( ':visible' ) ;
89
- }
90
- timing_area . toggle ( vis ) ;
91
- return vis ;
92
- }
93
- }
84
+ function patch_CodeCell_clear_output ( ) {
85
+ console . log ( log_prefix , 'Patching CodeCell.prototype.clear_output to clear timings also.' ) ;
86
+ var orig_clear_output = CodeCell . prototype . clear_output ;
87
+ CodeCell . prototype . clear_output = function ( ) {
88
+ var ret = orig_clear_output . apply ( this , arguments ) ;
89
+ clear_timing_data ( [ this ] ) ;
90
+ return ret ;
91
+ } ;
94
92
}
95
93
96
- function toggle_timing_display_multiple ( cells , vis ) {
94
+ function toggle_timing_display ( cells , vis ) {
97
95
for ( var i = 0 ; i < cells . length ; i ++ ) {
98
- if ( cells [ i ] instanceof CodeCell ) {
99
- vis = toggle_timing_display ( cells [ i ] , vis ) ;
96
+ var cell = cells [ i ] ;
97
+ if ( cell instanceof CodeCell ) {
98
+ var ce = cell . element ;
99
+ var timing_area = ce . find ( '.timing_area' ) ;
100
+ if ( timing_area . length > 0 ) {
101
+ if ( vis === undefined ) {
102
+ vis = ! timing_area . is ( ':visible' ) ;
103
+ }
104
+ timing_area . toggle ( vis ) ;
105
+ }
100
106
}
101
107
}
108
+ return vis ;
109
+ }
110
+
111
+ function clear_timing_data ( cells ) {
112
+ cells . forEach ( function ( cell , idx , arr ) {
113
+ delete cell . metadata . ExecuteTime ;
114
+ cell . element . find ( '.timing_area' ) . remove ( ) ;
115
+ } ) ;
116
+ events . trigger ( 'set_dirty.Notebook' , { value : true } ) ;
102
117
}
103
118
104
- function toggle_timing_display_selected ( ) {
105
- toggle_timing_display_multiple ( Jupyter . notebook . get_selected_cells ( ) ) ;
119
+ function clear_timing_data_all ( ) {
120
+ console . log ( log_prefix , 'Clearing all timing data' ) ;
121
+ clear_timing_data ( Jupyter . notebook . get_cells ( ) ) ;
106
122
}
107
123
108
124
function create_menu ( ) {
109
- var menu_toggle_timings = $ ( '<li/>' )
125
+ var timings_menu_item = $ ( '<li/>' )
110
126
. addClass ( 'dropdown-submenu' )
111
127
. append (
112
- $ ( '<a/>' ) . text ( 'Toggle timings' )
128
+ $ ( '<a href="#">' )
129
+ . text ( 'Execution Timings' )
130
+ . on ( 'click' , function ( evt ) { evt . preventDefault ( ) ; } )
113
131
)
114
132
. appendTo ( $ ( '#cell_menu' ) ) ;
115
133
116
134
var timings_submenu = $ ( '<ul/>' )
117
135
. addClass ( 'dropdown-menu' )
118
- . appendTo ( menu_toggle_timings ) ;
136
+ . appendTo ( timings_menu_item ) ;
119
137
120
138
$ ( '<li/>' )
121
139
. attr ( 'title' , 'Toggle the timing box for the selected cell(s)' )
122
140
. append (
123
- $ ( '<a/>' )
124
- . text ( 'Selected' )
125
- . on ( 'click' , toggle_timing_display_selected )
141
+ $ ( '<a href="#">' )
142
+ . text ( 'Toggle visibility (selected)' )
143
+ . on ( 'click' , function ( evt ) {
144
+ evt . preventDefault ( ) ;
145
+ toggle_timing_display ( Jupyter . notebook . get_selected_cells ( ) ) ;
146
+ } )
126
147
)
127
148
. appendTo ( timings_submenu ) ;
128
149
129
150
$ ( '<li/>' )
130
151
. attr ( 'title' , 'Toggle the timing box for all cells' )
131
152
. append (
132
- $ ( '<a/>' )
133
- . text ( 'All' )
153
+ $ ( '<a href="#">' )
154
+ . text ( 'Toggle visibility (all)' )
155
+ . on ( 'click' , function ( evt ) {
156
+ evt . preventDefault ( ) ;
157
+ toggle_timing_display ( Jupyter . notebook . get_cells ( ) ) ;
158
+ } )
159
+ )
160
+ . appendTo ( timings_submenu ) ;
161
+
162
+ $ ( '<li/>' )
163
+ . attr ( 'title' , 'Clear the selected cell(s) timing data' )
164
+ . append (
165
+ $ ( '<a href="#">' )
166
+ . text ( 'Clear (selected)' )
167
+ . on ( 'click' , function ( evt ) {
168
+ evt . preventDefault ( ) ;
169
+ clear_timing_data ( Jupyter . notebook . get_selected_cells ( ) ) ;
170
+ } )
171
+ )
172
+ . appendTo ( timings_submenu ) ;
173
+
174
+ $ ( '<li/>' )
175
+ . attr ( 'title' , 'Clear the timing data from all cells' )
176
+ . append (
177
+ $ ( '<a href="#">' )
178
+ . text ( 'Clear (all)' )
134
179
. on ( 'click' , function ( evt ) {
135
- toggle_timing_display_multiple ( Jupyter . notebook . get_cells ( ) ) ;
180
+ evt . preventDefault ( ) ;
181
+ clear_timing_data ( Jupyter . notebook . get_cells ( ) ) ;
136
182
} )
137
183
)
138
184
. appendTo ( timings_submenu ) ;
@@ -209,13 +255,13 @@ define([
209
255
if ( timing_area . length < 1 ) {
210
256
timing_area = $ ( '<div/>' )
211
257
. addClass ( 'timing_area' + ( options . display_right_aligned ? ' text-right' : '' ) )
212
- . on ( 'dblclick' , function ( evt ) { toggle_timing_display ( cell ) ; } )
258
+ . on ( 'dblclick' , function ( evt ) { toggle_timing_display ( [ cell ] ) ; } )
213
259
. appendTo ( cell . element . find ( '.input_area' ) ) ;
214
260
}
215
261
216
262
var start_time = moment ( cell . metadata . ExecuteTime . start_time ) ,
217
263
end_time = cell . metadata . ExecuteTime . end_time ;
218
- var msg = options . template [ end_time ? 'executed' : 'queued' ]
264
+ var msg = options . template [ end_time ? 'executed' : 'queued' ] ;
219
265
msg = msg . replace ( '${start_time}' , format_moment ( start_time ) ) ;
220
266
if ( end_time ) {
221
267
end_time = moment ( end_time ) ;
@@ -277,6 +323,15 @@ define([
277
323
update_all_timing_areas ( ) ;
278
324
}
279
325
326
+ // setup optional clear-data calls
327
+ if ( options . clear_timings_on_clear_output ) {
328
+ patch_CodeCell_clear_output ( ) ;
329
+ }
330
+ if ( options . clear_timings_on_kernel_restart ) {
331
+ console . log ( log_prefix , 'Binding kernel_restarting.Kernel event to clear timings.' ) ;
332
+ events . on ( 'kernel_restarting.Kernel' , clear_timing_data_all ) ;
333
+ }
334
+
280
335
// if displaying relative times, update them at intervals
281
336
if ( ! options . display_absolute_timings ) {
282
337
var period_ms = 1000 * Math . max ( 1 , options . relative_timing_update_period ) ;
0 commit comments