Reputation: 1252
Here is what i'd like to do: From a running python process, upgrade it to an IPython Jupyter kernel, which shares the local and global variables and can be accessed by other Jupyter clients.
I have found IPython.embed
which upgrades the session (but does not give me a Kernel connection information afaics). I've also found ipykernel.ipkernel.IPythonKernel
, which also appears to start an embedded ipython shell but also does not give me a jupyter kernel connection info.
Context: I want to do this from an external wrapper program which interacts with CPython via the python C api; this is why I can't simply start an IPython kernel in the first place.
Upvotes: 0
Views: 1047
Reputation: 1252
Ok, this is a typical case of RTFM. The IPython embedding docs mention IPython.embed_kernel
which appears to do exactly what I wanted. So it's a question of inserting
from IPython import embed_kernel
embed_kernel()
which will print connection info, and will give the kernel access to variables already defined in the global namespace. Neat!
There is still one important shortcoming for my use case: I want to be able to close the embedded kernel connection, do some more stuff in plain python, and then reopen the same kernel connection with the new state available. Alternatively, I could start the kernel in a sort of background demon mode: A kernel connection would open but the original python interpreter and variable namespace would remain open to interaction from the original python session, and new values would get shared with the kernel clients.
Does anyone know if either of these options is possible to achieve?
Upvotes: 2