itkwidgets.cell_watcher

Module Contents

Classes

Viewers

This class is designed to track each instance of the Viewer class that is instantiated as well as whether or not that instance is available for updates or requests.

CellWatcher

A singleton class used in interactive Jupyter notebooks in order to support asynchronous network communication that would otherwise be blocked by the IPython kernel.

Data

API

itkwidgets.cell_watcher.background_tasks

‘set(…)’

class itkwidgets.cell_watcher.Viewers

Bases: object

This class is designed to track each instance of the Viewer class that is instantiated as well as whether or not that instance is available for updates or requests.

Initialization

property data: Dict[str, Dict[str, bool]]

Get the underlying data dict containg all viewer data

Returns:

A dict of key, value pairs mapping the unique Viewer name to a dictionary containing a ‘ready’ key and a boolean value reflecting the ready state of the Viewer.

Return type:

Dict[str, Dict[str, bool]]

property not_created: List[str]

Return a list of all unavailable viewers

Returns:

A list of names of viewers that have not yet been created.

Return type:

List[str]

add_viewer(view: str) None

Add a new Viewer object to track.

Parameters:

view (str) – The unique string identifier for the Viewer object

update_viewer_status(view: str, status: bool) None

Update a Viewer’s ‘ready’ status.

Parameters:
  • view (str) – The unique string identifier for the Viewer object

  • status (bool) – Boolean value indicating whether or not the viewer is available for requests or updates. This should be false when the plugin API is not yet available or new data is not yet rendered.

viewer_ready(view: str) bool

Request the ‘ready’ status of a viewer.

Parameters:

view (str) – The unique string identifier for the Viewer object

Returns:

Boolean value indicating whether or not the viewer is available for requests or updates. This will be false when the plugin API is not yet available or new data is not yet rendered.

Return type:

bool

class itkwidgets.cell_watcher.CellWatcher

Bases: object

A singleton class used in interactive Jupyter notebooks in order to support asynchronous network communication that would otherwise be blocked by the IPython kernel.

__new__()

Create a singleton class.

setup() None

Perform the initial setup, including intercepting ‘execute_request’ handlers so that we can handle them internally before the IPython kernel does.

add_viewer(view: str) None

Add a new Viewer object to track.

Parameters:

view (str) – The unique string identifier for the Viewer object

update_viewer_status(view: str, status: bool) None

Update a Viewer’s ‘ready’ status. If the last cell run failed because the viewer was unavailable try to run the cell again.

Parameters:
  • view (str) – The unique string identifier for the Viewer object

  • status (bool) – Boolean value indicating whether or not the viewer is available for requests or updates. This should be false when the plugin API is not yet available or new data is not yet rendered.

viewer_ready(view: str) bool

Request the ‘ready’ status of a viewer.

Parameters:

view (str) – The unique string identifier for the Viewer object

Returns:

Boolean value indicating whether or not the viewer is available for requests or updates. This will be false when the plugin API is not yet available or new data is not yet rendered.

Return type:

bool

_task_cleanup(task: asyncio.Task) None

Callback to discard references to tasks once they’ve completed.

Parameters:

task (asyncio.Task) – Completed task that no longer needs a strong reference

create_task(fn: Callable) None

Create a task from the function passed in.

Parameters:

fn (Callable) – Coroutine to run concurrently as a Task

capture_event(stream: zmq.eventloop.zmqstream.ZMQStream, ident: list, parent: dict) None

Capture execute_request messages so that we can queue and process them concurrently as tasks to prevent blocking.

Parameters:
  • stream (ZMQStream) – Class to manage event-based messaging on a zmq socket

  • ident (list) – ZeroMQ routing prefix, which can be zero or more socket identities

  • parent (dict) – A dictonary of dictionaries representing a complete message as defined by the Jupyter message specification

async capture_event_async(stream: zmq.eventloop.zmqstream.ZMQStream, ident: list, parent: dict) None

Capture execute_request messages so that we can queue and process them concurrently as tasks to prevent blocking. Asynchronous for ipykernel 6+.

Parameters:
  • stream (ZMQStream) – Class to manage event-based messaging on a zmq socket

  • ident (list) – ZeroMQ routing prefix, which can be zero or more socket identities

  • parent (dict) – A dictonary of dictionaries representing a complete message as defined by the Jupyter message specification

property all_getters_resolved: bool

Determine if all tasks representing asynchronous network calls that fetch values have resolved.

Returns:

Whether or not all tasks for the current cell have resolved

Return type:

bool

ready_to_run_next_cell() bool

Determine if we are ready to run the next cell in the queue.

Returns:

If created Viewer objects are available and all futures are resolved.

Return type:

bool

async execute_next_request() None

Grab the next request if needed and then run the cell if it it ready to be run. Modeled after the approach used in jupyter-ui-poll.

Ref:

https://github.com/Kirill888/jupyter-ui-poll/blob/f65b81f95623c699ed7fd66a92be6d40feb73cde/jupyter_ui_poll/_poll.py#L75-L101

async _execute_next_request() None

Run the cell with the ipykernel shell_handler for execute_request

update_namespace() None

Update the namespace variables with the results from the getters

_callback(*args, **kwargs) None

After each future resolves check to see if they are all resolved. If so, update the namespace and run the next cell in the queue.

post_run_cell(response: IPython.core.interactiveshell.ExecutionResult) None

Runs after interactive execution (e.g. a cell in a notebook). Set the abort flag if there are errors produced by cell execution.

Parameters:

response (ExecutionResult) – The response message produced by cell execution