Skip to content

datalayer/jupyter-kernel-client

Repository files navigation

Datalayer

Become a Sponsor

Jupyter Kernel Client (via WebSocket and HTTP)

Github Actions Status PyPI - Version

Jupyter Kernel Client allows you to connect via WebSocket and HTTP to Jupyter Kernels. It also provide a easy to use interactive Konsole (console for Kernels).

To install the library, run the following command.

pip install jupyter_kernel_client

Usage

Check you have a Jupyter Server with ipykernel running somewhere. You can install those packages using:

pip install jupyter-server ipykernel

Kernel Client

  1. Start a Jupyter Server.
jupyter server --port 8888 --IdentityProvider.token MY_TOKEN
  1. Launch python in a terminal and execute the following snippet (update the server_url and token).
import os

from platform import node
from jupyter_kernel_client import KernelClient


with KernelClient(server_url="http://localhost:8888", token="MY_TOKEN") as kernel:
    reply = kernel.execute(
        """import os
from platform import node
print(f"Hey {os.environ.get('USER', 'John Smith')} from {node()}.")
"""
    )
    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"

Jupyter Konsole (aka Console for Kernels)

This package can be used to open a Jupyter Console to a Jupyter Kernel 🐣.

  1. Install the optional dependencies.
pip install jupyter-kernel-client[konsole]
  1. Start a Jupyter Server.
jupyter server --port 8888 --IdentityProvider.token MY_TOKEN
  1. Start the konsole and execute code.
jupyter konsole --url http://localhost:8888 --token MY_TOKEN
[KonsoleApp] KernelHttpManager created a new kernel: ...
Jupyter Kernel console 0.2.0

Python 3.12.7 | packaged by conda-forge | (main, Oct  4 2024, 16:05:46) [GCC 13.3.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.30.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: print("hello")
hello

In [2]:

Uninstall

To remove the library, execute:

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 - will automatically enable
# The server extension.
pip install -e ".[konsole,test,lint,typing]"

Running Tests

Install dependencies:

pip install -e ".[test]"

To run the python tests, use:

pytest

Development uninstall

pip uninstall jupyter_kernel_client

Packaging the library

See RELEASE