Advanced

Returning Values

Communication with the IPython Kernel is asynchronous, which causes challenges with Python code blocks in Jupyter cells that run synchronously. Multiple comm messages cannot be awaited during the synchronous Python code block execution. With regards to ITKWidgets this means that getter functions do not “just work” - the Python code cannot complete until the comm message has resolved with the response and the message cannot resolve until the code has completed. This creates a deadlock that prevents the kernel from progressing. This has been a documented issue for the Jupyter ipykernel for many years.

Libraries like ipython_blocking and jupyter-ui-poll have made efforts to address this issue and their approaches have been a great source of inspiration for the custom solution that we have chosen for ITKWidgets.

Our current solution prevents deadlocks but requires that getters be requested in one cell and resolved in another. For example:

viewer = view(image)
bg_color = viewer.get_background_color()
print(bg_color)

would simply become:

viewer = view(image)
bg_color = viewer.get_background_color()
print(bg_color)

This has not yet been applied in JupyterLite so getters are not yet well-behaved in JupyterLite.