# REST API for executing cells in a notebook

**URL:** <https://discourse.jupyter.org/t/rest-api-for-executing-cells-in-a-notebook/21346>\
**Category:** JupyterLab\
**Tags:** jupyterlab, how-to, help-wanted\
**Created:** [September 10, 2023, 1:12pm UTC](https://discourse.jupyter.org/t/rest-api-for-executing-cells-in-a-notebook/21346 "2023-09-10T13:12:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kfiroo](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/kfiroo/32/10984_2.png) [@kfiroo](https://discourse.jupyter.org/u/kfiroo)\
**Post date:** [September 10, 2023, 1:12pm UTC](https://discourse.jupyter.org/t/rest-api-for-executing-cells-in-a-notebook/21346/1 "2023-09-10T13:12:41Z")

</div>

Hey all,

We are using JupyterHub / Lab for remote code execution in our service.

I am looking for a REST API for executing arbitrary cells in a notebook, something like the “Restart Kernel and Run All Cells…” button in the notebook toolbar

Thanks 🙏

---

<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:** [September 10, 2023, 4:13pm UTC](https://discourse.jupyter.org/t/rest-api-for-executing-cells-in-a-notebook/21346/2 "2023-09-10T16:13:14Z")

</div>

It’s unclear _what_ actor this is asking to run the cell:

- a Jupyter _Kernel_, e.g `ipykernel`, with a fully-managed lifecycle
- a Jupyter _Client_, e.g. `jupyterlab`, with an ad-hoc lifecycle

For the former, one can use:

- connect the the `jupyter_server`
- `POST` to [create a session](https://jupyter-server.readthedocs.io/en/latest/developers/rest-api.html#post--api-sessions)
- use the provided info to open a [WebSocket](https://jupyter-server.readthedocs.io/en/latest/developers/websocket-protocols.html)
- use the [Kernel Message protocol](https://jupyter-client.readthedocs.io/en/latest/messaging.html) to execute `execute_request`

If one would expect a _client_ to actually be doing this on your behalf, the closest thing I know of (disclaimer: author) is [jyg](https://github.com/deathbeds/jyg), which is barely more than a prototype. But it does offer a [REST API](https://github.com/deathbeds/jyg#rest-api) which would allow running the run all cells command, given a browser is (and stays) open.

---

<div class="post-metadata">

**Author:** ![kfiroo](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/kfiroo/32/10984_2.png) [@kfiroo](https://discourse.jupyter.org/u/kfiroo)\
**Post date:** [September 11, 2023, 10:38am UTC](https://discourse.jupyter.org/t/rest-api-for-executing-cells-in-a-notebook/21346/3 "2023-09-11T10:38:11Z")

</div>

Hey bollwyvl,  
Thank you for you answer 🙏  
You rightfully deduced the actor is a Jupyter Client

We have a “browserless” server application that manages user sessions via JupyterHub REST Api.  
When we have a running user server we use `@jupyterlab/services` typescript SDK to interact with the server and execute some code.

Since servers can start cold, we want to have all previous execution context ready before executing the next code block, hence we want to “execute all previous code blocks”.

We are currently using the `execute_request` to execute code, but if I understand correctly this API directly executes code on the kernel and does not interact with any persistent file.

I would like to have something that is more like a notebook, meaning, when the user requests to execute code, we add the code as a new cell in the notebook and then run all cells sequentially (if needed).

I’m wondering what is the recommended approach here?  
Is there an API for interacting with a notebook / cells?  
Should we us the `contents` API to read/update the contents of some notebook and use the kernel to execute it manually?

Thank you so much for your help  
Kfir

---

<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:** [September 11, 2023, 1:13pm UTC](https://discourse.jupyter.org/t/rest-api-for-executing-cells-in-a-notebook/21346/4 "2023-09-11T13:13:44Z")

</div>

At the protocol level, a kernel doesn’t know anything about a notebook document. The exceptions:

- the kernel process is started with the `cwd` of the folder holding the notebook
- _some_ clients provide the `cellId` in the metadata of _some_ requests, and _some_ kernels do something with it
- but as metadata, this is not propagated back on replies from the kernel

So without a full client in the loop, your custom client would need to:

- parse the document itself
- find all the code cells
- punch them into the kernel
- remember which cell goes with which message id
- put them into the correct place
- write back to the contents manager

_With_ a standard client in the loop, this becomes… a lot more complicated, as the state of the truth of the document can become out-of-sync.

There has been discussion on `jupyter_server` of some this… perhaps the [Thursday call](https://discourse.jupyter.org/t/jupyter-community-calendar/2485) would be a space to raise more of these concerns and questions.
