Skip to content

Commit 75bc0e7

Browse files
committed
Add reset_kc option to reset_execution_trackers, async_execute and execute methods (defaults to False)
1 parent 827cbf0 commit 75bc0e7

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

nbclient/client.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,13 @@ def __init__(self, nb, km=None, **kw):
298298
super().__init__(**kw)
299299
self.nb = nb
300300
self.km = km
301-
self.reset_execution_trackers()
301+
self.reset_execution_trackers(reset_kc=True)
302302

303-
def reset_execution_trackers(self):
303+
def reset_execution_trackers(self, reset_kc=False):
304304
"""Resets any per-execution trackers.
305305
"""
306-
self.kc = None
306+
if reset_kc:
307+
self.kc = None
307308
self.code_cells_executed = 0
308309
self._display_id_map = {}
309310
self.widget_state = {}
@@ -412,7 +413,7 @@ async def async_setup_kernel(self, **kwargs):
412413
"""
413414
Context manager for setting up the kernel to execute a notebook.
414415
415-
The assigns the Kernel Manager (`self.km`) if missing and Kernel Client(`self.kc`).
416+
This assigns the Kernel Manager (`self.km`) if missing and Kernel Client(`self.kc`).
416417
417418
When control returns from the yield it stops the client's zmq channels, and shuts
418419
down the kernel.
@@ -431,12 +432,23 @@ async def async_execute(self, **kwargs):
431432
"""
432433
Executes each code cell.
433434
435+
Parameters
436+
----------
437+
kwargs :
438+
Any option for `self.kernel_manager_class.start_kernel()`. Because
439+
that defaults to AsyncKernelManager, this will likely include options
440+
accepted by `AsyncKernelManager.start_kernel()``, which includes `cwd`.
441+
If present, `reset_kc` is passed to `self.reset_execution_trackers`:
442+
if True, the kernel client will be reset and a new one will be created
443+
(default: False).
444+
434445
Returns
435446
-------
436447
nb : NotebookNode
437448
The executed notebook.
438449
"""
439-
self.reset_execution_trackers()
450+
reset_kc = kwargs.pop('reset_kc', False)
451+
self.reset_execution_trackers(reset_kc=reset_kc)
440452

441453
async with self.async_setup_kernel(**kwargs):
442454
self.log.info("Executing notebook with kernel: %s" % self.kernel_name)

0 commit comments

Comments
 (0)