Skip to content

Commit 0e122bb

Browse files
author
Golf Player
committed
Expose some information about notebook execution state
1 parent 3e2e2f6 commit 0e122bb

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

nbclient/client.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import atexit
22
import collections
33
import datetime
4+
import enum
45
import base64
56
import signal
67
from textwrap import dedent
@@ -32,6 +33,15 @@ def timestamp():
3233
return datetime.datetime.utcnow().isoformat() + 'Z'
3334

3435

36+
class ExecutionState(enum.Enum):
37+
NOTHING = 0
38+
STARTUP = 1
39+
STARTING_KERNEL_CLIENT = 2
40+
EXECUTING = 3
41+
CLEANING_UP = 4
42+
COMPLETE = 5
43+
44+
3545
class NotebookClient(LoggingConfigurable):
3646
"""
3747
Encompasses a Client for executing cells in a notebook
@@ -310,6 +320,9 @@ def reset_execution_trackers(self):
310320
self.output_hook_stack = collections.defaultdict(list)
311321
# our front-end mimicing Output widgets
312322
self.comm_objects = {}
323+
self.state = ExecutionState.NOTHING
324+
self.current_cell = None
325+
self.current_cell_index = -1
313326

314327
def start_kernel_manager(self):
315328
"""Creates a new kernel manager.
@@ -332,6 +345,7 @@ def start_kernel_manager(self):
332345
return self.km
333346

334347
async def _async_cleanup_kernel(self):
348+
self.state = ExecutionState.CLEANING_UP
335349
now = self.shutdown_kernel == "immediate"
336350
try:
337351
# Queue the manager to kill the process, and recover gracefully if it's already dead.
@@ -367,6 +381,8 @@ async def async_start_new_kernel_client(self, **kwargs):
367381
kernel_id : string-ized version 4 uuid
368382
The id of the started kernel.
369383
"""
384+
385+
self.state = ExecutionState.STARTING_KERNEL_CLIENT
370386
resource_path = self.resources.get('metadata', {}).get('path') or None
371387
if resource_path and 'cwd' not in kwargs:
372388
kwargs["cwd"] = resource_path
@@ -499,8 +515,10 @@ async def async_execute(self, reset_kc=False, **kwargs):
499515
if reset_kc and self.km:
500516
await self._async_cleanup_kernel()
501517
self.reset_execution_trackers()
518+
self.state = ExecutionState.STARTUP
502519

503520
async with self.async_setup_kernel(**kwargs):
521+
self.state = ExecutionState.EXECUTING
504522
self.log.info("Executing notebook with kernel: %s" % self.kernel_name)
505523
for index, cell in enumerate(self.nb.cells):
506524
# Ignore `'execution_count' in content` as it's always 1
@@ -512,7 +530,7 @@ async def async_execute(self, reset_kc=False, **kwargs):
512530
info_msg = await self.async_wait_for_reply(msg_id)
513531
self.nb.metadata['language_info'] = info_msg['content']['language_info']
514532
self.set_widgets_metadata()
515-
533+
self.state = ExecutionState.COMPLETE
516534
return self.nb
517535

518536
execute = run_sync(async_execute)
@@ -711,6 +729,9 @@ async def async_execute_cell(self, cell, cell_index, execution_count=None, store
711729
if self.record_timing and 'execution' not in cell['metadata']:
712730
cell['metadata']['execution'] = {}
713731

732+
self.current_cell = cell
733+
self.current_cell_index = cell_index
734+
714735
self.log.debug("Executing cell:\n%s", cell.source)
715736
parent_msg_id = await ensure_async(
716737
self.kc.execute(

0 commit comments

Comments
 (0)