Actively maintained. Last release 5 days ago, commits in 7 of the last 26 weeks. A safe choice.
pip install jupyter-kernel-client🪐 > Jupyter Kernel Client
Jupyter Kernel Client allows you to connect to live Jupyter Kernels through HTTP and WebSocket.
A Kernel is 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).
Install
To install the library, run the following command.
pip install jupyter_kernel_client
Jupyter Server Kernel
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"