Executing 2 notebooks against the same kernel

Hi all,

I’m trying to execute 2 notebooks against same kernel from jupyter(lab).

nb1 defines a variable (a=1) and nb2 prints it (print(a)). nb1 executes successfully and, while nb2 executes, it does not seem to attach to the newly started kernel, for I get “variable ‘a’ not found”. No other errors or problems.

I assume that, if both nbs were running on the same kernel, nb2 should be able to print a (just as when you attach 2 notebooks to the same kernel via the GUI).

I’ve tried several variations to no avail.

I’m using nbconvert 5.6.1 / jupyter_client 6.1.6.

I used the executenb notation “kernel=(km,kc)” that I found online, but I’m not sure that’s the right way to pass the kernel manager/client to executenb (at least I don’t get any errors).

Any ideas of what might be wrong?



from nbconvert.preprocessors.execute import executenb
from nbformat import read as nbread
from jupyter_client.manager import start_new_kernel

nb1 = nbread('nb1.ipynb', as_version=4)  <--- defines "a"
nb2 = nbread('nb2.ipynb', as_version=4)  <--- prints "a"
kernel_name = nb.metadata.get('kernelspec', {}).get('name', 'python')
km, kc = start_new_kernel(kernel_name=kernel_name)

executenb(nb1, kernel=(km, kc))
executenb(nb2, kernel=(km, kc))