From external program, append cell, execute, and show output in a running Jupyter notebook

My desired outcome is a jupyter notebook running in the browser.
From another program (say a local editor) that has access to the kernel/back end of notebook, I would like to be able to call an api to append a cell with specified contents, execute the cell, and show the output.

The examples for programmatically doing seem to require running in the notebook itself. I would like to be able to just send code to be executed to the kernel, and have it output the result in the already running jupyter notebook. This should not require refreshing the page or anything like that, ideally.

This might be a good post to peruse:

tl: dr; this is not (and probably won’t be) a built-in feature, due to security considerations, but would make a fine discretionary plugin.

1 Like

I am going to test this, but I found:

It seems to indicate that you can connect to the same kernel from another program and send it commands, which will then be displayed in the code console, if you choose a particular configuration.

Yes, acting as kernel client works fine, there’s just a number of keys that need to passed around, etc. However, the state of a notebook client is separate from that of the kernel… there is set_next_input but it won’t run anything, and not all clients implement it.

An approach that might work for your use case is ipylab which can run commands, which are probably the most stable way to interact with the lab client. Again, you’d need a handle to the kernel, but you’re probably going to need that anyway.