How to communicate with a Jupyter server
I have begun my journey to hack into Jupyter. The first step is to understand how to send code to a Jupyter kernel and get the results back.
Preparation
I have Jupyter on my computer for sure. I first start a Jupyter server from my Linux terminal.
$ jupyter notebook
[I 11:23:19.415 NotebookApp] Serving notebooks from local directory: /home/forrest/repos/Funix/funix
[I 11:23:19.415 NotebookApp] Jupyter Notebook 6.4.8 is running at:
[I 11:23:19.415 NotebookApp] http://localhost:8888/?token=94d64e081a9423e301059714ec73b60842d26380ad1e2fae
[I 11:23:19.415 NotebookApp] or http://127.0.0.1:8888/?token=94d64e081a9423e301059714ec73b60842d26380ad1e2fae
Start a kernel
Then I start a kernel. I used httpie for simplicity from my Linux terminal.
$ http POST localhost:8888/api/kernels?token=94d64e081a9423e301059714ec73b60842d26380ad1e2fae
HTTP/1.1 201 Created
Access-Control-Allow-Origin:
Content-Length: 162
Content-Security-Policy: frame-ancestors 'self'; report-uri /api/security/csp-report; default-src 'none'
Content-Type: application/json
Date: Fri, 11 Aug 2023 16:25:38 GMT
Location: /api/kernels/107dcac0-3cdf-423f-ab89-c014e3657192
Server: TornadoServer/6.2
Set-Cookie: username-localhost-8888="2|1:0|10:1691771138|23:username-localhost-8888|44:ZmZiMzVlYTY4N2IwNDQ1YWI0NzhiNzQxYzZiMTQ2MDE=|d391c388d50e13f772216bad82cba432142e287ece96bfe3b0b267e80544f4c9"; expires=Sun, 10 Sep 2023 16:25:38 GMT; HttpOnly; Path=/
X-Content-Type-Options: nosniff
{
"connections": 0,
"execution_state": "starting",
"id": "107dcac0-3cdf-423f-ab89-c014e3657192",
"last_activity": "2023-08-11T16:25:38.650084Z",
"name": "python3"
}
The kernel ID is important because that’s where I will send code for execution.
Sending the code
Next, I will send the code for execution. Here is a post I found helpful.
Refs:
- https://github.com/jupyter/jupyter/wiki/Jupyter-Notebook-Server-API
- https://jupyter-client.readthedocs.io/en/stable/index.html