🪐 Jupyter Kernel Client
Jupyter Kernel Client through HTTP and WebSocket
Jupyter Kernel Client allows you to connect to live Jupyter Kernels through HTTP and WebSocket.
A
Kernelis the process responsible to execute the notebook code.
Jupyter Kernel Client also provides a easy to use interactive Konsole (console for Kernels aka REPL, Read-Evaluate-Print-Loop).
To install the library, run the following command.
pip install jupyter_kernel_client
Jupyter Server
Check you have a Jupyter Server with ipykernel running somewhere. You can install those packages with the following command.
pip install jupyter-server ipykernel
- Start a Jupyter Server.
# make jupyter-server
jupyter server --port 8888 --ServerApp.port_retries 0 --IdentityProvider.token MY_TOKEN
- Launch a IPython REPL in a terminal with
ipython(orjupyter console). Execute the following snippet (update the server_url and token if needed).
import os
from platform import node
from jupyter_kernel_client import JupyterKernelClient
with JupyterKernelClient(server_url="http://localhost:8888", token="MY_TOKEN") as kernel:
code = """import os
from platform import node
print(f"Hey {os.environ.get('USER', 'John Smith')} from {node()}.")
"""
reply = kernel.execute(code)
print(reply)
assert reply["execution_count"] == 1
assert reply["outputs"] == [
{
"output_type": "stream",
"name": "stdout",
"text": f"Hey {os.environ.get('USER', 'John Smith')} from {node()}.\n",
}
]
assert reply["status"] == "ok"
Check the response.
{"execution_count": 1, "outputs": [{"output_type": "stream", "name": "stdout", "text": "Hey echarles from eric.\n"}], "status": "ok"}
Instead of using the kernel client as context manager, you can call the start() and stop() methods.
from jupyter_kernel_client import JupyterKernelClient
kernel = JupyterKernelClient(server_url="http://localhost:8888", token="MY_TOKEN")
kernel.start()
reply = kernel.execute(code)
print(reply)
kernel.stop()
To connect to an existing Jupyter Kernel, first start JupyterLab, open a Notebook with a Kernel and take not of the Kernel ID.
TODO: Document how to get the
Kernel ID.
make jupyterlab
You can now connect to the existing Kernel and run code (do not invoke stop).
from jupyter_kernel_client import JupyterKernelClient
kernel = JupyterKernelClient(server_url="http://localhost:8888", kernel_id="83ef59b7-9c78-40bd-8cc2-4447635e7d0b", token="MY_TOKEN")
kernel.start()
reply = kernel.execute("x=1")
print(reply)
Kaggle Kernel
Kaggle supports both batch execution and interactive kernel connections from code.
- Detailed guide: Kaggle docs
- Includes auth modes, channels URL retrieval, explicit and parsed connection options, batch execution from zero, accelerator matrix, and operational notes.
Quick batch example:
from jupyter_kernel_client import KaggleKernelExecutor
executor = KaggleKernelExecutor()
result = executor.execute(
"print('hello from kaggle')",
title="jkc-demo",
# accelerator="NvidiaTeslaT4",
wait=True,
)
print(result)
print(result.status)
print(result.stdout)
print(result.kernel_reply)
print(result.to_kernel_reply())
KaggleExecutionResult includes normalized helpers:
stdout/stderrconvenience propertieskernel_reply(same normalized Jupyter-like payload asto_kernel_reply())- auto-generated notebook cell IDs in batch submissions to match modern notebook metadata expectations
Quick interactive example:
from jupyter_kernel_client import KaggleKernelClient
channels_url = (
"wss://kkb-production.jupyter-proxy.kaggle.net/k/12345678/eyJhbGci.../proxy"
"/api/kernels/11e073f0-e82d-4029-be8d-3918f7ed1a9e/channels?session_id=..."
)
with KaggleKernelClient.from_channels_url(channels_url, token=None) as kernel:
reply = kernel.execute("x = 1 + 1; print(x)")
print(reply)
Google Colab Kernel
Google Colab exposes a Jupyter-compatible kernel behind an authenticating proxy.
Use ColabKernelClient to connect to an already-running Colab runtime.
- Detailed guide: Google Colab docs
- Includes explicit-value mode, channels URL mode, parser helpers, auth behavior, and channels URL retrieval steps.
Quick example:
from jupyter_kernel_client import ColabKernelClient
channels_url = (
"wss://<colab-host>/api/kernels/<kernel_id>/channels"
"?session_id=<...>&colab-runtime-proxy-token=<proxy_token>&colab-client-agent=web"
)
with ColabKernelClient.from_channels_url(channels_url) as kernel:
reply = kernel.execute("x = 1 + 1; print(x)")
print(reply)
Jupyter Konsole aka Console for Kernels
This package can be used to open a Jupyter Console to a Jupyter Kernel 🐣.
- Install the optional dependencies.
pip install jupyter-kernel-client[konsole]
- Start a Jupyter Server.
# make jupyter-server
jupyter server --port 8888 --ServerApp.port_retries 0 --IdentityProvider.token MY_TOKEN
- Start the konsole and execute code.
# make jupyter-konsole
jupyter konsole --url http://localhost:8888 --token MY_TOKEN
[KonsoleApp] KernelHttpManager created a new kernel:...
Jupyter Konsole...
In [1]: 1+1
2
Uninstall
To remove the library, execute the following command.
pip uninstall jupyter_kernel_client
Contributing
Development install
# Clone the repo to your local environment
# Change directory to the jupyter_kernel_client directory
# Install package in development mode, this will automatically enable the server extension.
pip install -e ".[konsole,test,lint,typing]"
Running Tests
Install dependencies.
pip install -e ".[test]"
Run the python tests.
pytest
Development uninstall
pip uninstall jupyter_kernel_client
Packaging the library
See RELEASE
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jupyter_kernel_client-1.0.0-py3-none-any.whl.
File metadata
- Download URL: jupyter_kernel_client-1.0.0-py3-none-any.whl
- Upload date:
- Size: 62.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b7127a47f8e165433e5200903781879c473166473f7a2bedd825332dc1f0bd98
|
|
| MD5 |
ae58a2e5957c18a94bbc7e010ee97777
|
|
| BLAKE2b-256 |
46bf4476e62d10aaeb3f982337a7d05c23e24f9a70fd3f0d42ea38af1d801f3b
|