Programmatically control jupyter server

I have spent the day looking at the docs of all the jupyter components and trying to decipher what the api might mean.
I am not sure I should be using python packages or REST API, but it seems python is the way to go.
I would like to be able to do things like

  • open/close/create notebooks
  • load /save notebooks
  • register new kernels (ie. environment kernels)
  • start/stop jupyterlab server
  • etc

Right now I can’t figure out how to access a currently running server and do something like what is asked here Programmatically open new notebook.

Any help is appreciated. Thank You.

It kind of depends on the lifespan and impact you want to have.

Since you mention starting a server, I guess we can assume that your control process can live outside of that. If you wish to write it in (asynchronous) python, you can take full control of the lifecycle of the server, and keep it in the same event loop with techniques like this dask blog or the deeper gist. That says NotebookApp all over, but one can replace it with LabApp and pretty much get the same experience: it’s probably worth revisiting… but maybe I’ll wait until nbclassic isn’t in there complicating matters.

Once you have a server running, then the REST API is the most predictable, and will correctly fire all the hooks: it takes a bit of messing about to figure out which Manager instances contain the specific things you’d want, from watching the browser traffic.

There aren’t a lot of ways to command a browser to do a lot of things… unless you actually take control of the browser… while not a “real” online solution, there are some examples in e.g. robotframework-jupyterlibrary, raw selenium, or even galata… but all of these are going to be far more brittle than real APIs.


Not really for controlling server, but might be relevant/interesting: GitHub - jtpio/ipylab: Control JupyterLab from Python Notebooks with Jupyter Widgets 🧪 ☢️ 🐍