How to start IPython kernel and connect using ZMQ sockets?
Asked Answered
S

1

6

I am working on a frontend to IPython in C++ (Qt).

I managed to embed Python in my application and retrieve plots and show these in my GUI. Now I want to start an IPython kernel and connect to it through ZMQ sockets.

I found a description for the communication protocol with IPython kernels. However, it doesn't say anywhere which ports to connect to. So it's nice and dandy to have a communication protocol, but totally useless if I don't know which ports to use.

The documentation mentions 'kernel specs' and tells me to use the jupyter kernelspec list command. This indeed shows me one directory, which only contains two files: logo-32x32.png and logo-64x64.png ...

How do I find the ports I need to connect to, to communicate with my IPython kernels?

I start my IPython kernel by running the following Python code from my C++ Qt app:

import IPython
IPython.start_kernel(argv=[])
Sporophyll answered 20/8, 2016 at 15:32 Comment(2)
Take a look at my updated version of ipy_repl.py from the SublimeREPL plugin for the Sublime Text programming editor. There's no Sublime-specific API stuff in there. Basically, it works with IPython up to 4.1.1 and jupyter_console up to 4.1.0 (it doesn't seem to work with later versions of IPython 4 or any version of 5) to set up a connection in a Sublime view with a running IPython/Jupyter kernel, including utilizing its autocomplete capability.Receive
start_kernel starts a kernel in the current process. What it sounds like you want is a kernel in a separate process which you connect to from your application process. For that, you'd use the jupyter_client module. You could look at applications like Spyder to see what they do.Ginkgo
S
2

It turns out Thomas K was right. The right approach to start an IPython/Jupyter kernel in a different process is: (with python3 as example)

import jupyter_client
kernel_manager, kernel_client = jupyter_client.start_new_kernel(kernel_name='python3')

When I initially tried this, I got a permission error. This was fixed by installing the python3 kernel spec (apparently Jupyter does not do this automatically...):

python3 -m ipykernel install --user

And then you can get the ports by

print(kernel_manager.get_connection_info())

It should be possible to use these ports to connect to the kernel via zero-mq. However, the kernel_client exposes a few methods to communicated with the kernel, so it may be easier to use that approach...

Sporophyll answered 21/8, 2016 at 9:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.