Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7f33c31

Browse files
author
Golf Player
committedJun 20, 2020
Addressing PR comments
1 parent 5deaee6 commit 7f33c31

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed
 

‎nbclient/client.py

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

224224
kernel_manager_class = Type(config=True, help='The kernel manager class to use.')
225225

226-
on_kernel_create = Any(
226+
on_execution_start = Any(
227227
default_value=None,
228228
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+
"""),
230234
).tag(config=True)
231235

232236
on_cell_start = Any(
233237
default_value=None,
234238
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+
"""),
236243
).tag(config=True)
237244

238245
on_cell_complete = Any(
239246
default_value=None,
240247
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+
"""),
247253
).tag(config=True)
248254

249255
on_cell_error = Any(
250256
default_value=None,
251257
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+
"""),
253263
).tag(config=True)
254264

255265
@default('kernel_manager_class')
@@ -407,7 +417,6 @@ async def async_start_new_kernel_client(self, **kwargs):
407417

408418
kernel_id = await ensure_async(self.km.start_kernel(extra_arguments=self.extra_arguments,
409419
**kwargs))
410-
run_hook(self.on_kernel_create, kernel_id)
411420

412421
# if self.km is not a KernelManager, it's probably a MultiKernelManager
413422
try:
@@ -431,6 +440,7 @@ async def async_start_new_kernel_client(self, **kwargs):
431440
await self._async_cleanup_kernel()
432441
raise
433442
self.kc.allow_stdin = False
443+
run_hook(self.on_execution_start, kernel_id=kernel_id)
434444
return self.kc, kernel_id
435445

436446
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):
697707
)
698708

699709
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)
701711
if self.force_raise_errors or not cell_allows_errors:
702712
raise CellExecutionError.from_cell_and_msg(cell, exec_reply['content'])
703713

@@ -743,14 +753,15 @@ async def async_execute_cell(self, cell, cell_index, execution_count=None, store
743753
cell['metadata']['execution'] = {}
744754

745755
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)
747757
parent_msg_id = await ensure_async(
748758
self.kc.execute(
749759
cell.source,
750760
store_history=store_history,
751761
stop_on_error=not self.allow_errors
752762
)
753763
)
764+
run_hook(self.on_cell_complete, cell=cell, cell_index=cell_index)
754765
# We launched a code cell to execute
755766
self.code_cells_executed += 1
756767
exec_timeout = self._get_timeout(cell)

‎nbclient/util.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import asyncio
77
import sys
88
import inspect
9+
from functools import partial
910

1011

1112
def check_ipython():
@@ -91,12 +92,13 @@ async def ensure_async(obj):
9192
return obj
9293

9394

94-
def run_hook(hook, *args):
95+
def run_hook(hook, **kwargs):
9596
if hook is None:
9697
return
9798
if inspect.iscoroutinefunction(hook):
98-
future = hook(*args)
99+
future = hook(**kwargs)
99100
else:
100101
loop = asyncio.get_event_loop()
101-
future = loop.run_in_executor(None, hook, *args)
102+
hook_with_kwargs = partial(hook, **kwargs)
103+
future = loop.run_in_executor(None, hook_with_kwargs)
102104
asyncio.ensure_future(future)

0 commit comments

Comments
 (0)
Please sign in to comment.