xp1632 / VPE_IP

0 stars 0 forks source link

What is kernel in jupyter lab #1

Open Max-ChenFei opened 1 year ago

Max-ChenFei commented 1 year ago

ref: https://docs.jupyter.org/en/latest/projects/kernels.html

xp1632 commented 1 year ago

By checking jupyter's lastest kernels https://github.com/jupyter/jupyter/wiki/Jupyter-kernels, we find two more options for JAVA kernel:

image

Bacatá : Example Ganymede : Examples IJava: Binder online demo

xp1632 commented 1 year ago

image Ganymede based on JShell is most promising at first glance

xp1632 commented 1 year ago

While Ganymede requires Java 11+, Fiji is bundled with Java version 8, but it works the same with Java 11:

Here is the reference link: https://forum.image.sc/t/run-fiji-or-imagej-headless-mode-with-java-11/73989 image

xp1632 commented 1 year ago

Tutorial of Running Java in a Jupyter Notebook based on IJava or Ganymede.

IJava is also based on JShell

Max-ChenFei commented 1 year ago

image Ganymede based on JShell is most promising at first glance

ganymeda is better

xp1632 commented 1 year ago

We first learn the theory of kernel:

1. What is kernel in jupyter?

- Kernels:

- Kernels are programming language specific processes that run independently 
and interact with the Jupyter Applications and their user interfaces.

- the default kernel called ipykernel,is the reference Jupyter kernel built on top of IPython.
    which provides powerful environment for interactive computing in Python

- In addition to Python, other programming language can also be used in the notebook
    based on different language kernels

- jupyter-client

-jupyter-client contains the authoritative description of the Jupyter messaging protocol
    which is used by clients to communicate with the kernels

- here we have more details of how kernel works in Jupyter:
https://jupyter-client.readthedocs.io/en/stable/messaging.html

- by checking this we could understand how kernels work in Jupyter

- Xeus

- Xeus is a framework meant to facilitate the implemtation of kernels for Jupyter
- since I don't need to implement a kernel, I will pass this part

1.1 Architecture of Jupyter projects:

https://docs.jupyter.org/en/latest/projects/architecture/content-architecture.html

1.1.1 a high-level overview of jupyter projects:

image

-we also have our simple version as :

-jupyter architecture

1.1.2 IPython Kernels

-Terminal Python:

-The IPython Kernel:

![image] (https://docs.jupyter.org/en/latest/_images/ipy_kernel_and_terminal.png)

https://jupyter-client.readthedocs.io/en/stable/messaging.html

1.2 So let's check jupyter-client focusing on how kernels work and deal with messaging:

image

1.4 Socket API in ZeroMQ

Max-ChenFei commented 1 year ago

@xp1632 Kernel Example using the command line https://github.com/gap-packages/JupyterKernel

This is a solution when we need to call the function of a close-sourced software

xp1632 commented 1 year ago

After figuring ZeroMQ socket, we'll continue the rest part of jupyter-client focusing on how kernels work and deal with messaging

1.5 There are five functions of kernel based on ZeroMQ sockets:

Shell

IOPub

stdin:

Control

Heartbeat

xp1632 commented 1 year ago

1.6 General Message Format:

Message Header:

Parent header

{
    # parent_header is a copy of the request's header
    'msg_id': '...',
    ...
}

Metadata

Content

Buffers


A full message:

{
    "header" : {
        "msg_id": "...",
        "msg_type": "...",
        ...
    },
    "parent_header": {},
    "metadata": {},
    "content": {},
    "buffers": [],
}
xp1632 commented 1 year ago

Max-ChenFei commented 1 year ago
  • The plan for tommorw is to finish the rest part of kernel communication: https://jupyter-client.readthedocs.io/en/stable/messaging.html#the-wire-protocol

  • Understand

    • Wire Protocol
    • Python API
    • Msg on shell
    • Msg on Control
    • Msg on IOPub
    • Msg on stdin
    • Custom Messages for extension
  • Then after having primitive understanding how kernel works and communicates,

    • we start to install Ganymede in jupyterlab
    • If more Java, JShell knowledge needed during installing, we 'll check that

After installing Ganymeda, try official examples first. If questions, you can create issues in that repo

xp1632 commented 1 year ago

1.7 We continue with The Wire Protocol

Wire Protocol

A full message:

{
    "header" : {
        "msg_id": "...",
        "msg_type": "...",
        ...
    },
    "parent_header": {},
    "metadata": {},
    "content": {},
    "buffers": [],
}
[
  b'u-u-i-d',         # zmq identity(ies)
  b'<IDS|MSG>',       # delimiter
  b'baddad42',        # HMAC signature
  b'{header}',        # serialized header dict
  b'{parent_header}', # serialized parent header dict
  b'{metadata}',      # serialized metadata dict
  b'{content}',       # serialized content dict
  b'\xf0\x9f\x90\xb1' # extra raw data buffer(s)
  ...
]

Prefix

Auchentication

Actual Message


Python API

{
  'header' : dict,
  # The msg's unique identifier and type are always stored in the header,
  # but the Python implementation copies them to the top level.
  'msg_id' : str,
  'msg_type' : str,
  'parent_header' : dict,
  'content' : dict,
  'metadata' : dict,
  'buffers': list,
}

xp1632 commented 1 year ago

1.8 Message on the shell (ROUTER/DEALER) channel

Request-Reply Pattern

Status

this is presented when status: error

{
   'status' : 'error',
   'ename' : str,   # Exception name, as a string
   'evalue' : str,  # Exception value, as a string
   'traceback' : list(str), # traceback frames as strings
}

Execute request type

content = {

'code' : str,
# Source code to be executed by the kernel, one single string or more lines.

...

'user_expressions':dict.
# A dict mapping names to expressions to be evaluated in the user's dict
# this information could be retrieved to distinguish between:
# inner prompts, client prompts, different frontend prompts interacting with same kernel

'allow_stdin': True
# If true, code running in the kernel could prompt user for input
# with input_request message

...
}

Execute reply type

content ={

'status' : str,
# One of :'ok' OR 'error' OR 'aborted'

'execution_count': int,
# A global kernel counter that increased by one with each request
# Typically used by clients to display prompt numbers to the user

}
{

'payload' : list(dict),
# Payloads are a way to trigger frontend actions from the kernel.

'user_expressions':dict
}

Payloads


Other Message Type in shell channel:



xp1632 commented 1 year ago

1.9 Messages on Control(ROUTER/DEALER) channel


1.10 Message on the IOPub(PUB/SUB) channel

content = {

    # The data dict contains key/value pairs, where the keys are MIME
    # types and the values are the raw data of the representation in that
    # format.
    'data' : dict,

    # Any metadata that describes the data
    # In IPython, only width and height of images
    'metadata' : dict,

    # Information not to be persisted to a notebook or other documents.
    # Intended to live only during a live kernel session.
    # We could store transient key display_id for future update
    'transient': dict,
}
content = {

    # The counter for this execution is also provided so that clients can
    # display it, since IPython automatically creates variables called _N
    # (for prompt N).
    'execution_count' : int,

    # data and metadata are identical to a display_data message.
    # the object being displayed is that passed to the display hook,
    # i.e. the *result* of the execution.
    'data' : dict,
    'metadata' : dict,
}
content = {
    # When the kernel starts to handle a message, it will enter the 'busy'
    # state and when it finishes, it will enter the 'idle' state.
    # The kernel will publish state 'starting' exactly once at process startup.
    execution_state : ('busy', 'idle', 'starting')
}

1.11 Messages on stdin(ROUTER/DEALER) channel


1.12 Custom Messages

#Open Comm produce this 'comm_open' msg, to be sent to the other side
{
  'comm_id' : 'u-u-i-d',
  'target_name' : 'my_comm',
  'data' : {}
}
- Every Comm has an ID and target name.
- After `comm_open` messgae has been sent,
    there should be corresponding Comm instance on both sides
#comm_msg
{
  'comm_id' : 'u-u-i-d',
  'data' : {}
}
xp1632 commented 1 year ago

The message communication protocol between the frontend and kernel, jupyter client, is finished.

image