fsleyes.actions.notebook

This module provides the NotebookAction class, an action which starts an IPython kernel and a Jupyter notebook server, allowing the user to interact with FSLeyes via a jupyter notebook.

class fsleyes.actions.notebook.mock

Bases: object

__dict__ = mappingproxy({'__module__': 'fsleyes.actions.notebook', '__dict__': <attribute '__dict__' of 'mock' objects>, '__weakref__': <attribute '__weakref__' of 'mock' objects>, '__doc__': None})
__module__ = 'fsleyes.actions.notebook'
__weakref__

list of weak references to the object (if defined)

class fsleyes.actions.notebook.NotebookAction(overlayList, displayCtx, frame)

Bases: fsleyes.actions.base.Action

The NotebookAction is an Action which (if necessary) starts an embedded IPython kernel and a jupyter notebook server, and opens the server home page in a web browser allowing the user to interact with FSLeyes via notebooks.

__init__(overlayList, displayCtx, frame)

Create a NotebookAction.

Parameters:
_NotebookAction__bounce(secs, progdlg)

Used by __startKernel() and __startServer(). Blocks for from ipykernel.kernelapp import IPKernelApp``secs``, bouncing the Progress dialog ten times per second, and yielding to the wx main loop.

_NotebookAction__openNotebooks()

Called when this NotebookAction is invoked. Starts the server and kernel if necessary, then opens a new notebook in a web browser.

_NotebookAction__startKernel(progdlg)

Attempts to create and start a BackgroundIPythonKernel.

Returns:the kernel if it was started.
Raises:A RuntimeError if the kernel did not start.
_NotebookAction__startServer(progdlg)

Attempts to create and start a NotebookServer.

Returns:the server if it was started.
Raises:A RuntimeError if the serer did not start.
__module__ = 'fsleyes.actions.notebook'
class fsleyes.actions.notebook.BackgroundIPythonKernel(overlayList, displayCtx, frame)

Bases: threading.Thread

The BackgroundIPythonKernel creates an IPython jupyter kernel and makes it accessible over tcp (on the local machine only). The zmq event loop is run on a separate thread, but the kernel handles events on the main thread, via idle.idle().

The Jupyter/IPython documentation is quite fragmented at this point in time; this github issue was useful in figuring out the implementation details.

__init__(overlayList, displayCtx, frame)

Set up the kernel and zmq ports. A jupyter connection file containing the information needed to connect to the kernel is saved to a temporary file - its path is accessed as an attribute called connfile().

Parameters:
kernel

The IPythonKernel object.

env

The namespace passed to the kernel.

connfile

The jupyter connection file containing information to connect to the IPython kernel.

error

If an error occurs on the background thread causing it to crash, a reference to the Exception is stored here.

run()

Start the IPython kernel and Run the zmq event loop.

_BackgroundIPythonKernel__kernelDispatch()

Event loop used for the IPython kernel. Submits the kernel function to the idle.idle() loop, and schedules another call to this method on the zmq event loop.

This means that, while the zmq loop runs in its own thread, the IPython kernel is executed on the main thread.

_BackgroundIPythonKernel__screenshot(view)

Insert a screenshot of the given view into the notebook.

__module__ = 'fsleyes.actions.notebook'
class fsleyes.actions.notebook.FSLeyesIPythonKernel(stdout, stderr, *args, **kwargs)

Bases: fsleyes.actions.notebook.mock

Custom IPython kernel used by FSLeyes. All this class does is ensure that the sys.stdout and sys.stderr streams are set appropriately when the kernel is executing code.

__init__(stdout, stderr, *args, **kwargs)

Initialize self. See help(type(self)) for accurate signature.

execute_request(*args, **kwargs)
dispatch_control(*args, **kwargs)
dispatch_shell(*args, **kwargs)
_FSLeyesIPythonKernel__patch_streams()
__module__ = 'fsleyes.actions.notebook'
class fsleyes.actions.notebook.FSLeyesIPythonShell

Bases: fsleyes.actions.notebook.mock

Custom IPython shell class used by FSLeyes.

enable_gui(gui)

Overrides ipykernel.zmqshell.ZMQInteractiveShell.enable_gui.

The default implementation will attempt to change the IPython GUI integration event loop, which will conflict with our own event loop in the BackgroundIPythonKernel. So this implementation does nothing.

__module__ = 'fsleyes.actions.notebook'
class fsleyes.actions.notebook.NotebookServer(connfile)

Bases: threading.Thread

Thread which starts a jupyter notebook server, and waits until it dies or is killed.

The server is configured such that all notebooks will connect to the same kernel, specified by the kernelFile parameter to __init__().

__init__(connfile)

Create a NotebookServer thread.

Parameters:connfile – Connection file of the IPython kernel to connect to.
port

Returns the TCP port that the notebook server is listening on. Will return None before the server has started.

ready

Returns True if the server is running and ready, False otherwise.

token

Returns an authentication token to use for connectng to the notebook server.

url

Returns the URL to use to connect to this server.

stdout

After the server has died, returns its standard output. While the server is still running, returns None.

stderr

After the server has died, returns its standard error. While the server is still running, returns None.

_NotebookServer__initConfigDir(cfgdir)

Creates a copy of the FSLeyes /assets/jupyter/ configuration directory in $TMPDIR, and customises its settings accordingly.

_NotebookServer__readPort()
__module__ = 'fsleyes.actions.notebook'
run()

Sets up a server configuration file, and then calls jupyer-notebook via subprocess.Popen. Waits until the notebook process dies.

fsleyes.actions.notebook.nbmain(argv)

Notebook entry point used when FSLeyes is running as a frozen application.

Used to start notebook server and kernel processes.