1313
1414from poyo import parse_string
1515
16+ from log import trace
1617import nbformat
1718from nbclient import NotebookClient
19+ from jupyter_client import KernelManager
20+ from jupyter_core .utils import run_sync
1821
1922# optional import of papermill for params support
2023try :
@@ -37,6 +40,7 @@ class RestartKernel(Exception):
3740# execute a notebook
3841def notebook_execute (options , status ):
3942
43+ trace ('inside notebook_execute' )
4044 # if this is a re-execution of a previously loaded kernel,
4145 # make sure the underlying python version hasn't changed
4246 python_cmd = options .get ("python_cmd" , None )
@@ -97,6 +101,7 @@ def notebook_execute(options, status):
97101 # read the notebook
98102 nb = nbformat .read (input , as_version = NB_FORMAT_VERSION )
99103
104+ trace ('read notebook' )
100105 # inject parameters if provided
101106 if params :
102107 nb_parameterize (nb , params )
@@ -108,17 +113,21 @@ def notebook_execute(options, status):
108113 # are we using the cache, if so connect to the cache, and then if we aren't in 'refresh'
109114 # (forced re-execution) mode then try to satisfy the execution request from the cache
110115 if cache == True or cache == "refresh" :
116+ trace ('using cache' )
111117 if not get_cache :
112118 raise ImportError ('The jupyter-cache package is required for cached execution' )
119+ trace ('getting cache' )
113120 nb_cache = get_cache (".jupyter_cache" )
114121 if not cache == "refresh" :
115122 cached_nb = nb_from_cache (nb , nb_cache )
116123 if cached_nb :
117124 cached_nb .cells .pop (0 )
118125 nb_write (cached_nb , input )
119126 status ("(Notebook read from cache)\n \n " )
127+ trace ('(Notebook read from cache)' )
120128 return True # can persist kernel
121129 else :
130+ trace ('not using cache' )
122131 nb_cache = None
123132
124133 # create resources for execution
@@ -130,8 +139,11 @@ def notebook_execute(options, status):
130139 if run_path :
131140 resources ["metadata" ]["path" ] = run_path
132141
142+ trace ("Will attempt to create notebook" )
133143 # create NotebookClient
144+ trace ("type of notebook: {0}" .format (type (nb )))
134145 client , created = notebook_init (nb , resources , allow_errors )
146+ trace ("NotebookClient created" )
135147
136148 # complete progress if necessary
137149 if (not quiet ) and created :
@@ -155,6 +167,7 @@ def notebook_execute(options, status):
155167
156168 # execute cell
157169 if cell .cell_type == 'code' :
170+ trace ("Executing cell {0}" .format (index ))
158171 cell = cell_execute (
159172 client ,
160173 cell ,
@@ -164,6 +177,7 @@ def notebook_execute(options, status):
164177 index > 0 # add_to_history
165178 )
166179 cell .execution_count = current_code_cell
180+ trace ("Executed cell {0}" .format (index ))
167181
168182 # if this was the setup cell, see if we need to exit b/c dependencies are out of date
169183 if index == 0 :
@@ -195,6 +209,9 @@ def notebook_execute(options, status):
195209 # end progress
196210 if progress :
197211 status ("Done\n " )
212+ trace ("Done" )
213+
214+ trace ("Notebook execution complete" )
198215
199216 # set widgets metadata
200217 client .set_widgets_metadata ()
@@ -236,14 +253,21 @@ def notebook_init(nb, resources, allow_errors):
236253 created = False
237254 if not hasattr (notebook_init , "client" ):
238255
256+ trace ("Creating NotebookClient" )
239257 # create notebook client
240258 client = NotebookClient (nb , resources = resources )
241259 client .allow_errors = allow_errors
242260 client .record_timing = False
243261 client .create_kernel_manager ()
244262 client .start_new_kernel ()
245263 client .start_new_kernel_client ()
246- info_msg = client .wait_for_reply (client .kc .kernel_info ())
264+ info = client .kc .kernel_info ()
265+
266+ async def get_info ():
267+ return await client .kc .kernel_info ()
268+ info = run_sync (get_info )()
269+
270+ info_msg = client .wait_for_reply (info )
247271 client .nb .metadata ['language_info' ] = info_msg ['content' ]['language_info' ]
248272 notebook_init .client = client
249273 created = True
0 commit comments