Skip to main content

Kubernetes Jupyter Kernel CRD Client

Project description

Jupyter-Kernel-Client

The Jupyter Kernel Client provides you with the ability to create, query, and delete Kubernetes kernel custom resources. It uses the Jupyter Kernel Controller to create the necessary pod and service resources for the kernel custom resources. Finally, you can connect to the created remote kernel using the jupyter-client.

Installation and Basic usage

To install the latest release locally, make sure you have pip installed and run:

pip install jkclient

Usage - Running Jupyter Kernel Client

Create kernel

from jkclient import CreateKernelRequest, JupyterKernelClient

client = JupyterKernelClient(incluster=False)
request = CreateKernelRequest(
    name="foo-0",
    env={
        "KERNEL_ID": uuid4().hex,
        "KERNEL_NAME": f"jovyan-{uuid4().hex}",
        "KERNEL_USERNAME": "jovyan",
        "KERNEL_NAMESPACE": "default",
        "KERNEL_IMAGE": "weekenthralling/kernel-py:133fbe3",
        "KERNEL_WORKING_DIR": "/mnt/data",
        "KERNEL_VOLUME_MOUNTS": [
            {"name": "shared-vol", "mount_path": "/mnt/data"},
        ],
        "KERNEL_VOLUMES": [
            {
                "name": "shared-vol",
                "nfs": {"server": "10.0.0.29", "path": "/data"},
            },
        ],
        "KERNEL_STARTUP_SCRIPTS_PATH": "/opt/startup",
        "KERNEL_IDLE_TIMEOUT": "1800",
    },
)

client.create(request=request)

And return the kernel connection info

{
    "kernel_id" : "0c3931b4-0fc8-4c35-a4db-657e659cc04f",
    "kernel_name": "foo-0",
    "conn_info": {
        "shell_port": 56799,
        "iopub_port": 56500,
        "stdin_port": 52665,
        "control_port": 38703,
        "hb_port": 35068,
        "ip": "foo-0.default.svc.cluster.local",
        "key": "0c3931b4-0fc8-4c35-a4db-657e659cc04f",
        "transport": "tcp",
        "signature_scheme": "hmac-sha256",
        "kernel_name": ""
    }
}

Use kernel connection info

import queue
from jupyter_client.blocking import BlockingKernelClient

client = BlockingKernelClient()
client.load_connection_info(
    info={
        "shell_port": 56799,
        "iopub_port": 56500,
        "stdin_port": 52665,
        "control_port": 38703,
        "hb_port": 35068,
        "ip": "foo-0.default.svc.cluster.local",
        "key": "0c3931b4-0fc8-4c35-a4db-657e659cc04f",
        "transport": "tcp",
        "signature_scheme": "hmac-sha256",
        "kernel_name": ""
    }
)

client.start_channels()

code = """
import math
result = math.sqrt(16)
result
"""
client.execute(code)

shell_msg = None
while True:
    try:
        shell_msg = client.get_shell_msg(timeout=30)
        if shell_msg["msg_type"] == "execute_reply":
            break
    except queue.Empty:
        print(f"Get shell msg is empty.")
        break

iopub_msg = None
while True:
    # Poll the message
    try:
        io_msg_content = client.get_iopub_msg(timeout=30)["content"]
        logger.info(f"io_msg_content: {io_msg_content}")
        if (
            "execution_state" in io_msg_content
            and io_msg_content["execution_state"] == "idle"
        ):
            break
        iopub_msg = io_msg_content
    except queue.Empty:
        print(f"Get iopub msg is empty.")
        break

print(f"Got kernel output, shell_msg: {shell_msg} iopub_msg: {iopub_msg}")

Get exec code log

Got kernel output, shell_msg: {'header': {'msg_id': 'cf9f0c39-b0fbb1595399f44b68f893ab_9_39', 'msg_type': 'execute_reply', 'username': 'username', 'session': 'cf9f0c39-b0fbb1595399f44b68f893ab', 'date': datetime.datetime(2024, 7, 9, 8, 31, 56, 856558, tzinfo=tzlocal()), 'version': '5.3'}, 'msg_id': 'cf9f0c39-b0fbb1595399f44b68f893ab_9_39', 'msg_type': 'execute_reply', 'parent_header': {'msg_id': 'c65b2c1d-b8dca8ea83672d1f56b1a79d_86_3', 'msg_type': 'execute_request', 'username': 'username', 'session': 'c65b2c1d-b8dca8ea83672d1f56b1a79d', 'date': datetime.datetime(2024, 7, 9, 8, 31, 56, 846349, tzinfo=tzlocal()), 'version': '5.3'}, 'metadata': {'started': '2024-07-09T08:31:56.847191Z', 'dependencies_met': True, 'engine': 'a6d2191d-b438-44f0-a413-d8db6af4cebe', 'status': 'ok'}, 'content': {'status': 'ok', 'execution_count': 8, 'user_expressions': {}, 'payload': []}, 'buffers': []} iopub_msg: {'data': {'text/plain': '4.0'}, 'metadata': {}, 'execution_count': 8}

License

Jupyter-Kernel-Client is distributed under the terms of the Apache 2.0 license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jkclient-0.0.3.tar.gz (20.1 kB view hashes)

Uploaded Source

Built Distribution

jkclient-0.0.3-py3-none-any.whl (18.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page