4

I am attempting to connect a jupyter notebook to a IPython kernel which was started outside of a jupyter notebook server via IPython.kernel_embed().

I can attach to it just fine with jupyter console --existing and jupyter qtconsole --existing but I cannot do it with jupyter notebook as the notebook does not support the --existing flag. As mentioned in this issue this is not because of any technical limitation, but rather because it would be confusing from a UI perspective.

I am successfully able to interact with a kernel from a jupyter notebook with

from jupyter_client import BlockingKernelClient
client = BlockingKernelClient()
client.load_connection_file('/Users/ebanner/Library/Jupyter/runtime/kernel-10962.json')
client.start_channels()

and issue client.execute_interactive(). However I would really like to avoid running client.execute_interactive() in each and every cell.

I have tried several things. First I tried changing the c.ConnectionFileMixin.*_-port variables in jupyter_notebook_config.py and also writing my own custom kernel manager and setting it via c.NotebookApp.kernel_manager_class to

from tornado import gen, web
from jupyter_client import KernelManager
from notebook.services.kernels.kernelmanager import MappingKernelManager

class ExistingMappingKernelManager(MappingKernelManager):
    """A KernelManager that just connects to an existing kernel."""

    @gen.coroutine
    def start_kernel(self, kernel_id=None, path=None, **kwargs):
        kernel_id = 1
        km = KernelManager(kernel_name='python3')
        kc = km.client()
        kc.load_connection_file('/Users/ebanner/Library/Jupyter/runtime/kernel-10962.json')
        kc.start_channels()
        try:
            kc.wait_for_ready()
        except RuntimeError:
            kc.stop_channels()
            raise
        raise gen.Return(kernel_id)

but the approaches have all failed thus far.

The most promising route seems to be overriding KernelManager._launch_kernel(), though I am not sure what to override it with as it currently returns an instance of subprocess.Popen() on the kernel process started by ipykernel.

Any help would be greatly appreciated.

1 Answers1

1

I wound up creating a hacky kernel manager which seems to do the job.

https://github.com/ebanner/extipy

The only caveat is that you have to disable message authentication to get it to work because jupyter was complaining that the expected message signature didn't match.

More discussion here

https://groups.google.com/forum/#!topic/jupyter/qamkem52Xn0