Here's how far I've got:
First, a lot of things have changed since IPython 4.0 and so whatever little of relevant examples out there aren't valid more. To top it of documentation is non existent.
How to instantiate IPython kernel in Python script
You can do something like this:
from jupyter_client import KernelManager
def main():
km = KernelManager()
km.start_kernel()
cf = km.connection_file
print("To connect a client: jupyter console --existing ", cf)
kc = km.client()
kc.start_channels()
try:
kc.wait_for_ready()
except RuntimeError:
kc.stop_channels()
km.shutdown_kernel()
raise
# executes Python statement to create global variable named d
# and assign it value 32
kc.execute('d=32')
input("Press Enter to continue...")
if __name__ == '__main__':
main()
How to connect to IPython kernel from Jupyter Console
Just execute the command printed by above code:
jupyter console --existing <full json filename>
Then if you type d in Jupyter Console, you will see value 32.
How to connect to IPython kernel from Jupyter Notebook
This is still a tough one. The main issue is that Jupyter notebook insist on owning its own kernel and doesn't have any option to connect to an existing kernel. The only way around is to create your own kernel manager class, an example of which is here but it doesn't seem to work with newer IPython. You then invoke notebook by specifying to use your kernel manager class:
jupyter notebook \
--NotebookApp.kernel_manager_class=extipy.ExternalIPythonKernelManager \
--Session.key='b""'
This part still isn't working.
from python_script import object