Skip to content

Commit b38d7a3

Browse files
author
Golf Player
committed
Addressing PR comments
1 parent 733423a commit b38d7a3

File tree

3 files changed

+29
-24
lines changed

3 files changed

+29
-24
lines changed

.bumpversion.cfg

-8
This file was deleted.

nbclient/client.py

+24-13
Original file line numberDiff line numberDiff line change
@@ -227,33 +227,43 @@ class NotebookClient(LoggingConfigurable):
227227

228228
kernel_manager_class: KernelManager = Type(config=True, help='The kernel manager class to use.')
229229

230-
on_kernel_create = Any(
230+
on_execution_start = Any(
231231
default_value=None,
232232
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+
"""),
234238
).tag(config=True)
235239

236240
on_cell_start = Any(
237241
default_value=None,
238242
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+
"""),
240247
).tag(config=True)
241248

242249
on_cell_complete = Any(
243250
default_value=None,
244251
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+
"""),
251257
).tag(config=True)
252258

253259
on_cell_error = Any(
254260
default_value=None,
255261
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+
"""),
257267
).tag(config=True)
258268

259269
@default('kernel_manager_class')
@@ -418,7 +428,6 @@ async def async_start_new_kernel_client(self, **kwargs) -> t.Tuple[KernelClient,
418428

419429
kernel_id = await ensure_async(self.km.start_kernel(extra_arguments=self.extra_arguments,
420430
**kwargs))
421-
run_hook(self.on_kernel_create, kernel_id)
422431

423432
# if self.km is not a KernelManager, it's probably a MultiKernelManager
424433
try:
@@ -442,6 +451,7 @@ async def async_start_new_kernel_client(self, **kwargs) -> t.Tuple[KernelClient,
442451
await self._async_cleanup_kernel()
443452
raise
444453
self.kc.allow_stdin = False
454+
run_hook(self.on_execution_start, kernel_id=kernel_id)
445455
return self.kc, kernel_id
446456

447457
start_new_kernel_client = run_sync(async_start_new_kernel_client)
@@ -740,7 +750,7 @@ def _check_raise_for_error(
740750
)
741751

742752
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)
744754
if self.force_raise_errors or not cell_allows_errors:
745755
raise CellExecutionError.from_cell_and_msg(cell, exec_reply['content'])
746756

@@ -792,14 +802,15 @@ async def async_execute_cell(
792802
cell['metadata']['execution'] = {}
793803

794804
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)
796806
parent_msg_id = await ensure_async(
797807
self.kc.execute(
798808
cell.source,
799809
store_history=store_history,
800810
stop_on_error=not self.allow_errors
801811
)
802812
)
813+
run_hook(self.on_cell_complete, cell=cell, cell_index=cell_index)
803814
# We launched a code cell to execute
804815
self.code_cells_executed += 1
805816
exec_timeout = self._get_timeout(cell)

nbclient/util.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import sys
88
import inspect
99
from typing import Callable, Awaitable, Any, Union
10+
from functools import partial
1011

1112

1213
def check_ipython() -> None:
@@ -93,12 +94,13 @@ async def ensure_async(obj: Union[Awaitable, Any]) -> Any:
9394
return obj
9495

9596

96-
def run_hook(hook, *args):
97+
def run_hook(hook: Callable, **kwargs) -> None:
9798
if hook is None:
9899
return
99100
if inspect.iscoroutinefunction(hook):
100-
future = hook(*args)
101+
future = hook(**kwargs)
101102
else:
102103
loop = asyncio.get_event_loop()
103-
future = loop.run_in_executor(None, hook, *args)
104+
hook_with_kwargs = partial(hook, **kwargs)
105+
future = loop.run_in_executor(None, hook_with_kwargs)
104106
asyncio.ensure_future(future)

0 commit comments

Comments
 (0)