-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding the abaility to queue future cell requests with the goal to a)not run any cells until the viewer has been created and b)inspect cells before running to check for getters. When getters are found they should create futures and once they have resolved we can run that cell in its completion.
- Loading branch information
Showing
2 changed files
with
94 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import asyncio | ||
from inspect import isawaitable, iscoroutinefunction | ||
from queue import Queue | ||
import re | ||
import sys | ||
from IPython import get_ipython | ||
|
||
|
||
class CellWatcher(object): | ||
def __init__(self, viewer): | ||
self.raw = '' | ||
self.id = None | ||
self.viewer = viewer | ||
self.view_object = None | ||
self.shell = get_ipython() | ||
self.kernel = self.shell.kernel | ||
self.execute_request_handler = self.kernel.shell_handlers["execute_request"] | ||
|
||
self._events = Queue() | ||
|
||
if iscoroutinefunction(self.execute_request_handler): # ipykernel 6+ | ||
self.kernel.shell_handlers["execute_request"] = self.capture_event_async | ||
else: | ||
# ipykernel < 6 | ||
self.kernel.shell_handlers["execute_request"] = self.capture_event | ||
|
||
self.shell.events.register('pre_run_cell', self.pre_run_cell) | ||
self.shell.events.register('post_run_cell', self.post_run_cell) | ||
|
||
def capture_event(self, stream, ident, parent): | ||
self._events.put((stream, ident, parent)) | ||
|
||
async def capture_event_async(self, stream, ident, parent): | ||
self.capture_event(stream, ident, parent) | ||
|
||
def preprocess_getters(self, raw): | ||
regex = f"{self.view_object}.get_(.*)\([^()]*\)" | ||
# Find and evaluate any getters in the cell | ||
[self.shell.ev(m.group()) for m in re.finditer(regex, raw)] | ||
|
||
async def execute_next_request(self): | ||
stream, ident, parent = self._events.get() | ||
|
||
self.preprocess_getters(parent["content"]["code"]) | ||
|
||
sys.stdout.flush() | ||
sys.stderr.flush() | ||
self.kernel.set_parent(ident, parent) | ||
if self.kernel._aborting: | ||
self.kernel._send_abort_reply(stream, parent, ident) | ||
else: | ||
rr = self.kernel.execute_request(stream, ident, parent) | ||
if isawaitable(rr): | ||
rr = await rr | ||
|
||
shell_stream = getattr(self.kernel, "shell_stream", None) # ipykernel 6 vs 5 differences | ||
|
||
# replicate shell_dispatch behaviour | ||
sys.stdout.flush() | ||
sys.stderr.flush() | ||
if shell_stream is not None: # 6+ | ||
self.kernel._publish_status("idle", "shell") | ||
shell_stream.flush(2) | ||
else: | ||
self.kernel._publish_status("idle") | ||
|
||
if self._events.qsize(): | ||
await self.execute_next_request() | ||
|
||
def _callback(self, name, future): | ||
self.viewer.results[name].set_result(future.result()) | ||
getters_resolved = [f.done() for f in self.viewer.results.values()] | ||
# if all getters have resolved then ready to re-run | ||
if all(getters_resolved): | ||
self.shell.run_cell(raw_cell=self.raw, cell_id=self.id) | ||
|
||
def pre_run_cell(self, info): | ||
# grab info for re-run | ||
self.raw = info.raw_cell | ||
self.id = info.cell_id | ||
|
||
def find_view_object(self, raw): | ||
assignments = re.split("\n|=|;", raw) | ||
obj_idx = [i-1 for i, v in enumerate(assignments) if 'view(' in v] | ||
if len(obj_idx): | ||
self.view_object = assignments[obj_idx[0]].strip() | ||
|
||
def post_run_cell(self, result): | ||
if self.view_object is None: | ||
self.find_view_object(result.info.raw_cell) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters