Search code examples
pythonjupyter-kernel

Get raw Python objects from KernelManager


When running code in a Jupyter kernel it is possible to get the iopub or the shell messages from the respective channels. Is is possible to extract the raw python objects from the kernel as well?

Say for the below code

code = """a = {'a': 1}
b = {'b': 2}
"""


class Shell:
    def __init__(self):
        km = KernelManager()
        km.start_kernel()

        client = km.client()
        client.start_channels()

        client.execute(code)

        print(client.get_iopub_msg())
        print(client.get_shell_msg())

I want to retrieve the actual objects a and b so that they can be used in the current python application. Is that possible?


Solution

  • I found a workaround for it by using dill to dump the entire session inside the kernel

    dill.dump_session()
    

    And then load it in the main application

    dill.load_session()