# jupyter_kernel_client
Code execution client based on jupyter server websocket
## How to use
### 1. Start Jupyter Server(or Lab)
Enter the following command to start
```bash
jupyter server
```
By default, you'll get output like below, and you can get your server host is `localhost`, port is `8888`, token
is `fa42b5d1f787df44f5ca70a88c4fa6f2d42fdb4a1838c59b`
```
...
[C 2022-12-12 17:37:40.953 ServerApp]
http://localhost:8888/?token=fa42b5d1f787df44f5ca70a88c4fa6f2d42fdb4a1838c59b
or http://127.0.0.1:8888/?token=fa42b5d1f787df44f5ca70a88c4fa6f2d42fdb4a1838c59b
```
### 2. Start a kernel
Using Jupyter Server REST API to start a kernel
see: [POST /api/kernels](https://jupyter-server.readthedocs.io/en/latest/developers/rest-api.html#post--api-kernels)
TODO: Start kernel by client
### 3. Connect to kernel and execute code
You can get the `kernel_id` in the request to start the kernel
```python
from jupyter_kernel_client.client import KernelWebsocketClient
client = KernelWebsocketClient(
kernel_id=kernel_id, # you can get it in 2. Start a kernel
port='8888', # you can get port in 1. Start Jupyter Server(or Lab)
host='localhost', # you can get host in 1. Start Jupyter Server(or Lab)
token=token, # you can get token in 1. Start Jupyter Server(or Lab)
)
code = """
print('hello world')
"""
import asyncio
result = asyncio.run(client.execute(code))
print(result)
```
You will get
```bash
{'outputs': [{'output_type': 'stream', 'name': 'stdout', 'text': 'hello world\n'}], 'execution_count': 1}
```
Raw data
{
"_id": null,
"home_page": "",
"name": "jupyter-kernel-client",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "fastapi,jupyter,pluggy,plugins,server",
"author": "",
"author_email": "Wh1isper <9573586@qq.com>",
"download_url": "https://files.pythonhosted.org/packages/15/de/a72cd7eccf3b2f1fa4cc5c52a262b45d7d7cdf53ecd1cb551457b0880b71/jupyter_kernel_client-0.1.4.tar.gz",
"platform": null,
"description": "# jupyter_kernel_client\n\nCode execution client based on jupyter server websocket\n\n## How to use\n\n### 1. Start Jupyter Server(or Lab)\n\nEnter the following command to start\n\n```bash\njupyter server\n```\n\nBy default, you'll get output like below, and you can get your server host is `localhost`, port is `8888`, token\nis `fa42b5d1f787df44f5ca70a88c4fa6f2d42fdb4a1838c59b`\n\n```\n...\n[C 2022-12-12 17:37:40.953 ServerApp]\n http://localhost:8888/?token=fa42b5d1f787df44f5ca70a88c4fa6f2d42fdb4a1838c59b\n or http://127.0.0.1:8888/?token=fa42b5d1f787df44f5ca70a88c4fa6f2d42fdb4a1838c59b\n```\n\n### 2. Start a kernel\n\nUsing Jupyter Server REST API to start a kernel\n\nsee: [POST /api/kernels](https://jupyter-server.readthedocs.io/en/latest/developers/rest-api.html#post--api-kernels)\n\nTODO: Start kernel by client\n\n### 3. Connect to kernel and execute code\n\nYou can get the `kernel_id` in the request to start the kernel\n\n```python\nfrom jupyter_kernel_client.client import KernelWebsocketClient\n\nclient = KernelWebsocketClient(\n kernel_id=kernel_id, # you can get it in 2. Start a kernel\n port='8888', # you can get port in 1. Start Jupyter Server(or Lab)\n host='localhost', # you can get host in 1. Start Jupyter Server(or Lab)\n token=token, # you can get token in 1. Start Jupyter Server(or Lab)\n)\n\ncode = \"\"\"\nprint('hello world')\n\"\"\"\n\nimport asyncio\n\nresult = asyncio.run(client.execute(code))\nprint(result)\n```\n\nYou will get\n\n```bash\n{'outputs': [{'output_type': 'stream', 'name': 'stdout', 'text': 'hello world\\n'}], 'execution_count': 1}\n```\n",
"bugtrack_url": null,
"license": "BSD 3-Clause License",
"summary": "jupyter kernel websocket client",
"version": "0.1.4",
"split_keywords": [
"fastapi",
"jupyter",
"pluggy",
"plugins",
"server"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "2410ca93163796c9076fd73dd1556019",
"sha256": "3c1d9bd393df02919ba78a707f7fa279de6ff9ca7ea1960a691f1b5d9b95ea02"
},
"downloads": -1,
"filename": "jupyter_kernel_client-0.1.4-py3-none-any.whl",
"has_sig": false,
"md5_digest": "2410ca93163796c9076fd73dd1556019",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 5508,
"upload_time": "2022-12-20T01:32:49",
"upload_time_iso_8601": "2022-12-20T01:32:49.552379Z",
"url": "https://files.pythonhosted.org/packages/e5/7a/88da2646efb8821292e8a85ef1b79a3ed957149bc4e2a4e25e758b735fdf/jupyter_kernel_client-0.1.4-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "55d7b311da0725076b820dda604b3a75",
"sha256": "bbe496cd8aac3487784d2e07dd53f05a239ea60f643d149d23ea126a405b7450"
},
"downloads": -1,
"filename": "jupyter_kernel_client-0.1.4.tar.gz",
"has_sig": false,
"md5_digest": "55d7b311da0725076b820dda604b3a75",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 6607,
"upload_time": "2022-12-20T01:32:51",
"upload_time_iso_8601": "2022-12-20T01:32:51.769562Z",
"url": "https://files.pythonhosted.org/packages/15/de/a72cd7eccf3b2f1fa4cc5c52a262b45d7d7cdf53ecd1cb551457b0880b71/jupyter_kernel_client-0.1.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2022-12-20 01:32:51",
"github": false,
"gitlab": false,
"bitbucket": false,
"lcname": "jupyter-kernel-client"
}