16
16
from nbformat .v4 import output_from_msg
17
17
18
18
from .exceptions import CellTimeoutError , DeadKernelError , CellExecutionComplete , CellExecutionError
19
+ from .util import run_sync
19
20
20
21
21
22
def timestamp ():
@@ -294,43 +295,6 @@ def __init__(self, nb, km=None, **kw):
294
295
self .km = km
295
296
self .reset_execution_trackers ()
296
297
297
- def run_blocking (self , coro ):
298
- """Runs a coroutine and blocks until it has executed.
299
-
300
- An event loop is created if no one already exists. If an event loop is
301
- already running, this event loop execution is nested into the already
302
- running one if `nest_asyncio` is set to True.
303
-
304
- Parameters
305
- ----------
306
- coro : coroutine
307
- The coroutine to be executed.
308
-
309
- Returns
310
- -------
311
- result :
312
- Whatever the coroutine returns.
313
- """
314
- try :
315
- loop = asyncio .get_event_loop ()
316
- except RuntimeError :
317
- loop = asyncio .new_event_loop ()
318
- asyncio .set_event_loop (loop )
319
- if self .nest_asyncio :
320
- import nest_asyncio
321
- nest_asyncio .apply (loop )
322
- try :
323
- result = loop .run_until_complete (coro )
324
- except RuntimeError as e :
325
- if str (e ) == 'This event loop is already running' :
326
- raise RuntimeError (
327
- 'You are trying to run nbclient in an environment where an '
328
- 'event loop is already running. Please pass `nest_asyncio=True` in '
329
- '`NotebookClient.execute` and such methods.'
330
- )
331
- raise
332
- return result
333
-
334
298
def reset_execution_trackers (self ):
335
299
"""Resets any per-execution trackers.
336
300
"""
@@ -417,9 +381,6 @@ async def setup_kernel(self, **kwargs):
417
381
self .kc .stop_channels ()
418
382
self .kc = None
419
383
420
- def execute (self , ** kwargs ):
421
- return self .run_blocking (self .async_execute (** kwargs ))
422
-
423
384
async def async_execute (self , ** kwargs ):
424
385
"""
425
386
Executes each code cell.
@@ -445,6 +406,8 @@ async def async_execute(self, **kwargs):
445
406
446
407
return self .nb
447
408
409
+ execute = run_sync (async_execute )
410
+
448
411
def set_widgets_metadata (self ):
449
412
if self .widget_state :
450
413
self .nb .metadata .widgets = {
@@ -591,11 +554,6 @@ def _check_raise_for_error(self, cell, exec_reply):
591
554
if (exec_reply is not None ) and exec_reply ['content' ]['status' ] == 'error' :
592
555
raise CellExecutionError .from_cell_and_msg (cell , exec_reply ['content' ])
593
556
594
- def execute_cell (self , cell , cell_index , execution_count = None , store_history = True ):
595
- return self .run_blocking (
596
- self .async_execute_cell (cell , cell_index , execution_count , store_history )
597
- )
598
-
599
557
async def async_execute_cell (self , cell , cell_index , execution_count = None , store_history = True ):
600
558
"""
601
559
Executes a single code cell.
@@ -661,6 +619,8 @@ async def async_execute_cell(self, cell, cell_index, execution_count=None, store
661
619
self .nb ['cells' ][cell_index ] = cell
662
620
return cell
663
621
622
+ execute_cell = run_sync (async_execute_cell )
623
+
664
624
def process_message (self , msg , cell , cell_index ):
665
625
"""
666
626
Processes a kernel message, updates cell state, and returns the
0 commit comments