jupyter/jupyter_client

How to get kernel_id from a running Python kernel?

fleming79 opened this issue · 1 comments

I'm currently experimenting with ipylab, that uses ipywidgets for comms and am wondering if there is simple way to obtain the kernel_id in the Python kernel.

I found it can be extracted from the kernel config, but this is a bit hacky.

from IPython.core.getipython import get_ipython

ip = get_ipython()
kernel_id = (
    ip.kernel.config["IPKernelApp"]["connection_file"].rsplit("kernel-", 1)[1].removesuffix(".json")
)

I used a similar trick:

import ipykernel
import re
kernel_id = re.search('kernel-(.*).json', ipykernel.connect.get_connection_file()).group(1)

However, I think there should be a cleaner solution.