Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get kernel_id from a running Python kernel? #1004

Open
fleming79 opened this issue Jan 23, 2024 · 1 comment
Open

How to get kernel_id from a running Python kernel? #1004

fleming79 opened this issue Jan 23, 2024 · 1 comment

Comments

@fleming79
Copy link

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")
)
@cknoll
Copy link

cknoll commented Mar 27, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants