@@ -227,33 +227,43 @@ class NotebookClient(LoggingConfigurable):
227
227
228
228
kernel_manager_class : KernelManager = Type (config = True , help = 'The kernel manager class to use.' )
229
229
230
- on_kernel_create = Any (
230
+ on_execution_start = Any (
231
231
default_value = None ,
232
232
allow_none = True ,
233
- help = """A callable which executes when the kernel is created.""" ,
233
+ help = dedent ("""
234
+ Called after the kernel manager and kernel client are setup, and cells
235
+ are about to execute.
236
+ Called with kwargs `kernel_id`.
237
+ """ ),
234
238
).tag (config = True )
235
239
236
240
on_cell_start = Any (
237
241
default_value = None ,
238
242
allow_none = True ,
239
- help = """A callable which executes before a cell is executed.""" ,
243
+ help = dedent ("""
244
+ A callable which executes before a cell is executed.
245
+ Called with kwargs `cell`, and `cell_index`.
246
+ """ ),
240
247
).tag (config = True )
241
248
242
249
on_cell_complete = Any (
243
250
default_value = None ,
244
251
allow_none = True ,
245
- help = dedent (
246
- """
247
- A callable which executes after a cell execution is complete. It is
248
- called even when a cell results in a failure.
249
- """
250
- ),
252
+ help = dedent ("""
253
+ A callable which executes after a cell execution is complete. It is
254
+ called even when a cell results in a failure.
255
+ Called with kwargs `cell`, and `cell_index`.
256
+ """ ),
251
257
).tag (config = True )
252
258
253
259
on_cell_error = Any (
254
260
default_value = None ,
255
261
allow_none = True ,
256
- help = """A callable which executes when a cell execution results in an error.""" ,
262
+ help = dedent ("""
263
+ A callable which executes when a cell execution results in an error.
264
+ This is executed even if errors are suppressed with `cell_allows_errors`.
265
+ Called with kwargs `cell`, and `cell_index`.
266
+ """ ),
257
267
).tag (config = True )
258
268
259
269
@default ('kernel_manager_class' )
@@ -418,7 +428,6 @@ async def async_start_new_kernel_client(self, **kwargs) -> t.Tuple[KernelClient,
418
428
419
429
kernel_id = await ensure_async (self .km .start_kernel (extra_arguments = self .extra_arguments ,
420
430
** kwargs ))
421
- run_hook (self .on_kernel_create , kernel_id )
422
431
423
432
# if self.km is not a KernelManager, it's probably a MultiKernelManager
424
433
try :
@@ -442,6 +451,7 @@ async def async_start_new_kernel_client(self, **kwargs) -> t.Tuple[KernelClient,
442
451
await self ._async_cleanup_kernel ()
443
452
raise
444
453
self .kc .allow_stdin = False
454
+ run_hook (self .on_execution_start , kernel_id = kernel_id )
445
455
return self .kc , kernel_id
446
456
447
457
start_new_kernel_client = run_sync (async_start_new_kernel_client )
@@ -740,7 +750,7 @@ def _check_raise_for_error(
740
750
)
741
751
742
752
if (exec_reply is not None ) and exec_reply ['content' ]['status' ] == 'error' :
743
- run_hook (self .on_cell_error , cell , cell_index )
753
+ run_hook (self .on_cell_error , cell = cell , cell_index = cell_index )
744
754
if self .force_raise_errors or not cell_allows_errors :
745
755
raise CellExecutionError .from_cell_and_msg (cell , exec_reply ['content' ])
746
756
@@ -792,14 +802,15 @@ async def async_execute_cell(
792
802
cell ['metadata' ]['execution' ] = {}
793
803
794
804
self .log .debug ("Executing cell:\n %s" , cell .source )
795
- run_hook (self .on_cell_start , cell , cell_index )
805
+ run_hook (self .on_cell_start , cell = cell , cell_index = cell_index )
796
806
parent_msg_id = await ensure_async (
797
807
self .kc .execute (
798
808
cell .source ,
799
809
store_history = store_history ,
800
810
stop_on_error = not self .allow_errors
801
811
)
802
812
)
813
+ run_hook (self .on_cell_complete , cell = cell , cell_index = cell_index )
803
814
# We launched a code cell to execute
804
815
self .code_cells_executed += 1
805
816
exec_timeout = self ._get_timeout (cell )
0 commit comments