# Programmatically control jupyter server

**URL:** <https://discourse.jupyter.org/t/programmatically-control-jupyter-server/12465>\
**Category:** JupyterLab\
**Created:** [January 5, 2022, 7:19pm UTC](https://discourse.jupyter.org/t/programmatically-control-jupyter-server/12465 "2022-01-05T19:19:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vv111y](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/vv111y/32/6144_2.png) [@vv111y](https://discourse.jupyter.org/u/vv111y)\
**Post date:** [January 5, 2022, 7:19pm UTC](https://discourse.jupyter.org/t/programmatically-control-jupyter-server/12465/1 "2022-01-05T19:19:44Z")

</div>

Hi,  
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](https://discourse.jupyter.org/t/programmatically-open-new-notebook/2636).

Any help is appreciated. Thank You.

---

<div class="post-metadata">

**Author:** ![bollwyvl](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/bollwyvl/32/904_2.png) [@bollwyvl](https://discourse.jupyter.org/u/bollwyvl)\
**Post date:** [January 5, 2022, 10:07pm UTC](https://discourse.jupyter.org/t/programmatically-control-jupyter-server/12465/2 "2022-01-05T22:07:42Z")

</div>

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](https://blog.dask.org/2019/09/13/jupyter-on-dask) or [the deeper gist](https://gist.github.com/bollwyvl/bd56b58ba0a078534272043327c52bd1). That says `NotebookApp` all over, but one can replace it with [`LabApp`](https://github.com/jupyterlab/jupyterlab/blob/812cf28dfb9e8eb98ecb0ecf4563cd89c82459a2/jupyterlab/labapp.py#L801) 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](https://robotframework-jupyterlibrary.readthedocs.io), raw selenium, or even `galata`… but all of these are going to be _far_ more brittle than real APIs.

---

<div class="post-metadata">

**Author:** ![krassowski](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/krassowski/32/1685_2.png) [@krassowski](https://discourse.jupyter.org/u/krassowski)\
**Post date:** [January 5, 2022, 10:29pm UTC](https://discourse.jupyter.org/t/programmatically-control-jupyter-server/12465/3 "2022-01-05T22:29:29Z")

</div>

Not really for controlling server, but might be relevant/interesting: [GitHub - jtpio/ipylab: Control JupyterLab from Python Notebooks with Jupyter Widgets 🧪 ☢️ 🐍](https://github.com/jtpio/ipylab)
