1

I wish to do the following:

  1. From Python script, connect to existing Jupyter kernel.

  2. Copy an object available in Python script to Jupyter kernel.

  3. Access that object from Jupyter notebook.

So far I gather that the way to do this is via jupyter_client. I also found related question here: Executing code in ipython kernel with the KernelClient API.

However this question focused on either setting scaler value or running code. How do you copy object to Jupyter kernel instead?

4
  • 1
    why not importing it? Like from python_script import object Commented Dec 10, 2018 at 16:41
  • What Arthur said... Why not import your script as a module and create the variable from within your notebook? The effort needed to accomplish what you want should be hint to the fact that there are other (read: better) options. Or so it would seem without an explanation of your use case :) Commented Dec 11, 2018 at 6:53
  • 1
    The idea is to have Python manipulate the data in the kernel while notebook basically acts as query tool for that data. Think of Python program concurrently maintaining some dynamic data while notebook allows you to slice, dice and visualize. Commented Dec 12, 2018 at 0:15
  • question is good, its very useful when you want to start very long colculation after very long calculation and you think you may beed to interrupt second kernel, but don't know if jupyter will do it (it sometimes don't) Commented Feb 22, 2021 at 7:29

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.