How to connect ipython kernel with custom frontend?

  • ipython profile create my_kernel --ipython-dir="./ipython_dir"
  • ipython kernel -f="./confs/c1.json" --matplotlib --ipython-dir="./ipython_dir" --profile="my_kernel"

This gives me file c1.json

{
  "shell_port": 33135,
  "iopub_port": 45121,
  "stdin_port": 33991,
  "control_port": 37215,
  "hb_port": 60399,
  "ip": "127.0.0.1",
  "key": "4ec201d6-fa1cefbd31693c09aa044b2a",
  "transport": "tcp",
  "signature_scheme": "hmac-sha256",
  "kernel_name": ""
}

Here is the requirements:

ipython==8.22.2
jupyter_client==8.6.1

So the questions are

  1. What’s the most convenient way to communicate with an IPython kernel externally (say client.py file)? I’m currently using the jupyter_client library to communicate.
import json
from jupyter_client.asynchronous.client import AsyncKernelClient
from jupyter_client.client import KernelClient


json_file = open("confs/c1.json", "r")
data = json.load(json_file)
shell_port = data["shell_port"]
iopub_port = data["iopub_port"]
stdin_port = data["stdin_port"]
control_port = data["control_port"]
hb_port = data["hb_port"]

kc = KernelClient(
    ip="127.0.0.1",
    transport="tcp",
    shell_port=shell_port,
    iopub_port=iopub_port,
    stdin_port=stdin_port,
    control_port=control_port,
    hb_port=hb_port
)
code = """import os

current_dir = os.getcwd()

print("Current working directory:", current_dir)"""
msg_id = kc.execute(code)
  1. I need to establish an asynchronous communication channel with the IPython kernel to enable real-time output streaming. I believe from jupyter_client.asynchronous.client import AsyncKernelClient is a suitable option. Can you provide a code snippet demonstrating this approach?

  2. Last one - How to dockerize the ipython kernel for each user and make in a connection until user kill kernel or timeout. (I want to use django channel)

frontend ↔ Ajax ↔ django channel ↔ jupyter-client ↔ dockerized ipython kernel

Hello @shraysalvi I’m trying to do something very similar, did you manage to do it? If so, would you help me out on how did you do it?

I also have a custom frontend, I’m trying to make async APIs with jupyter-client to do the tasks that jupyter-notebook can do but with a custom frontend.