Skip to content

Attaching a notebook to a running kernel

Edward Banner edited this page Apr 11, 2018 · 1 revision

After installing codebook you can start a jupyter server with the following command.

jupyter notebook \
  --NotebookApp.kernel_manager_class=codebook.ExternalIPythonKernelManager \
  --Session.key='b""'

Start a new notebook. You should get the following output.

In [1]: dir()

['In',
 'Out',
 '_',
 '__',
 '___',
 '__builtin__',
 '__builtins__',
 '__doc__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 '_dh',
 '_i',
 '_i1',
 '_ih',
 '_ii',
 '_iii',
 '_oh',
 '_sh',
 'exit',
 'get_ipython',
 'quit']

Look at the script example/foo.py. This script contains the following code.

import os

import IPython

EXISTING_KERNEL = True

open(f'{os.environ["HOME"]}/.pynt', 'a').close()
IPython.start_kernel(user_ns={**locals(), **globals(), **vars()})

It defines a single variable EXISTING_KERNEL, writes a file .pynt to your home directory, and starts a IPython kernel which captures all the global and local variables.

Now run it. You should get output similar to the following.

$ python example/foo.py
To connect another client to this kernel, use:
    --existing kernel-854.json

Now go back to your notebook and do a Kernel -> Restart. Now you should get the following output.

In [1]: dir()

['EXISTING_KERNEL',
 'IPython',
 'In',
 'Out',
 '_',
 '__',
 '___',
 '__annotations__',
 '__builtin__',
 '__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 '_dh',
 '_i',
 '_i1',
 '_ih',
 '_ii',
 '_iii',
 '_oh',
 '_sh',
 'exit',
 'get_ipython',
 'os',
 'quit']

Notice the variable EXISTING_KERNEL is available. Also some other variables like IPython and os are available. We have successfully connected to the kernel started by foo.py.

Additionally the .pynt file in your home directory is deleted. This is so the next time you restart a jupyter notebook kernel (e.g. in another notebook) you won't connect to this one.

In your jupyter logging output you should also see something like the following:

[I 11:28:55.196 NotebookApp] Attaching bdd7833f-1196-4dc5-8a45-f46629f0bb1f to an existing kernel...                 
[I 11:28:55.373 NotebookApp] Latest kernel = /Users/ebanner/Library/Jupyter/runtime/kernel-854.json from dir = /Users
/ebanner/Library/Jupyter/runtime                                                                                     
[D 11:28:55.373 NotebookApp] Loading connection file /Users/ebanner/Library/Jupyter/runtime/kernel-854.json 

There are also a bunch of authentication errors, but it doesn't look like these have any effect on the result.

Clone this wiki locally