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 uuid import uuid4
from jkclient import CreateKernelRequest, JupyterKernelClient

client = JupyterKernelClient()
request = CreateKernelRequest(
    name="foo",
    env={
        "KERNEL_ID": str(uuid4()),
        "KERNEL_USERNAME": "jovyan",
        "KERNEL_NAMESPACE": "default",
        "KERNEL_IMAGE": "weekenthralling/kernel-py:480d2f5",
        "KERNEL_WORKING_DIR": "/mnt/data",
        "KERNEL_VOLUME_MOUNTS": [
            {"name": "shared-vol", "mountPath": "/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" : "968183bb-13ef-4faf-b7d8-30fe8d20e6a3",
    "kernel_name": "foo-0",
    "conn_info": {
        "shell_port": 52317,
        "stdin_port": 52321,
        "iopub_port": 52318,
        "control_port": 52320,
        "hb_port": 52319,
        "ip": "foo.default.svc.cluster.local",
        "key": "968183bb-13ef-4faf-b7d8-30fe8d20e6a3",
        "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": 52317,
        "stdin_port": 52321,
        "iopub_port": 52318,
        "control_port": 52320,
        "hb_port": 52319,
        "ip": "foo.default.svc.cluster.local",
        "key": "968183bb-13ef-4faf-b7d8-30fe8d20e6a3",
        "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"]
        print(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': 1}

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.7.tar.gz (21.2 kB view details)

Uploaded Source

Built Distribution

jkclient-0.0.7-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

Details for the file jkclient-0.0.7.tar.gz.

File metadata

  • Download URL: jkclient-0.0.7.tar.gz
  • Upload date:
  • Size: 21.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for jkclient-0.0.7.tar.gz
Algorithm Hash digest
SHA256 083d1ab7715e4ac3733ca0f1d6057ea69a23dca58aa950ea0e38407c81c130f4
MD5 8b7a0883f893ace179b79b2f2d53a68b
BLAKE2b-256 714a675887d3421ac7e87f6848febec204763169b56c08f44c1bbb59b82e42c1

See more details on using hashes here.

File details

Details for the file jkclient-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: jkclient-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 19.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for jkclient-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 784418d29381aff35bd440c90a2482c18610b8f9937dc1860c2605eb4a2a7dc3
MD5 a10c3b72b71e5350bf3d493e0c1f71a6
BLAKE2b-256 ec99ec6329d6caad1375763ae7a98a042afb6ee1ce8a755b8574828292a51439

See more details on using hashes here.

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