@@ -223,33 +223,43 @@ class NotebookClient(LoggingConfigurable):
223
223
224
224
kernel_manager_class = Type (config = True , help = 'The kernel manager class to use.' )
225
225
226
- on_kernel_create = Any (
226
+ on_execution_start = Any (
227
227
default_value = None ,
228
228
allow_none = True ,
229
- help = """A callable which executes when the kernel is created.""" ,
229
+ help = dedent ("""
230
+ Called after the kernel manager and kernel client are setup, and cells
231
+ are about to execute.
232
+ Called with kwargs `kernel_id`.
233
+ """ ),
230
234
).tag (config = True )
231
235
232
236
on_cell_start = Any (
233
237
default_value = None ,
234
238
allow_none = True ,
235
- help = """A callable which executes before a cell is executed.""" ,
239
+ help = dedent ("""
240
+ A callable which executes before a cell is executed.
241
+ Called with kwargs `cell`, and `cell_index`.
242
+ """ ),
236
243
).tag (config = True )
237
244
238
245
on_cell_complete = Any (
239
246
default_value = None ,
240
247
allow_none = True ,
241
- help = dedent (
242
- """
243
- A callable which executes after a cell execution is complete. It is
244
- called even when a cell results in a failure.
245
- """
246
- ),
248
+ help = dedent ("""
249
+ A callable which executes after a cell execution is complete. It is
250
+ called even when a cell results in a failure.
251
+ Called with kwargs `cell`, and `cell_index`.
252
+ """ ),
247
253
).tag (config = True )
248
254
249
255
on_cell_error = Any (
250
256
default_value = None ,
251
257
allow_none = True ,
252
- help = """A callable which executes when a cell execution results in an error.""" ,
258
+ help = dedent ("""
259
+ A callable which executes when a cell execution results in an error.
260
+ This is executed even if errors are suppressed with `cell_allows_errors`.
261
+ Called with kwargs `cell`, and `cell_index`.
262
+ """ ),
253
263
).tag (config = True )
254
264
255
265
@default ('kernel_manager_class' )
@@ -407,7 +417,6 @@ async def async_start_new_kernel_client(self, **kwargs):
407
417
408
418
kernel_id = await ensure_async (self .km .start_kernel (extra_arguments = self .extra_arguments ,
409
419
** kwargs ))
410
- run_hook (self .on_kernel_create , kernel_id )
411
420
412
421
# if self.km is not a KernelManager, it's probably a MultiKernelManager
413
422
try :
@@ -431,6 +440,7 @@ async def async_start_new_kernel_client(self, **kwargs):
431
440
await self ._async_cleanup_kernel ()
432
441
raise
433
442
self .kc .allow_stdin = False
443
+ run_hook (self .on_execution_start , kernel_id = kernel_id )
434
444
return self .kc , kernel_id
435
445
436
446
start_new_kernel_client = run_sync (async_start_new_kernel_client )
@@ -697,7 +707,7 @@ def _check_raise_for_error(self, cell, cell_index, exec_reply):
697
707
)
698
708
699
709
if (exec_reply is not None ) and exec_reply ['content' ]['status' ] == 'error' :
700
- run_hook (self .on_cell_error , cell , cell_index )
710
+ run_hook (self .on_cell_error , cell = cell , cell_index = cell_index )
701
711
if self .force_raise_errors or not cell_allows_errors :
702
712
raise CellExecutionError .from_cell_and_msg (cell , exec_reply ['content' ])
703
713
@@ -743,14 +753,15 @@ async def async_execute_cell(self, cell, cell_index, execution_count=None, store
743
753
cell ['metadata' ]['execution' ] = {}
744
754
745
755
self .log .debug ("Executing cell:\n %s" , cell .source )
746
- run_hook (self .on_cell_start , cell , cell_index )
756
+ run_hook (self .on_cell_start , cell = cell , cell_index = cell_index )
747
757
parent_msg_id = await ensure_async (
748
758
self .kc .execute (
749
759
cell .source ,
750
760
store_history = store_history ,
751
761
stop_on_error = not self .allow_errors
752
762
)
753
763
)
764
+ run_hook (self .on_cell_complete , cell = cell , cell_index = cell_index )
754
765
# We launched a code cell to execute
755
766
self .code_cells_executed += 1
756
767
exec_timeout = self ._get_timeout (cell )
0 commit comments