Jupyter Kernel
How to Speak in Another Language
How to Make a
Jupyter Kernel:
1: 5 ømq sockets
2: Implement Protocol
3. Holy Shit,
YOU RUINED EVERYTHING,
LIKE EVERYTHING YOU DO
4. This Kernel
Is Like Your Life
5. Begin a Lot of Things,
Left Undone
6. And Nobody Loves You
Understanding
Jupyter Kernel
Agenda
• Jupyter, IPython, notebook, console, client, kernel
• The Interactive Computing Protocol
• Implementing a Kernel
• Live Demo
About this guy
• 廖偉涵 Adrian Liaw
• github.com/adrianliaw
• High School Homeschooler
• A Pythonista
• Student of Udacity’s

Data Analyst Nanodegree
About this guy
• Amateur Pianist
• Classical Music Lover
• Coffee Shop Lover
IPYTHON
ENHANCED PYTHON SHELL
IPYTHON NOTEBOOK
WEB-BASED NOTEBOOK ENVIRONMENT
The Big Split™
Jupyter Notebook
(With R Kernel)
-Project Jupyter
“Project Jupyter was born out of the
IPython Project in 2014 as it evolved
to support interactive data science
and scientific computing across all
programming languages.”
CLIENT KERNEL
ØMQ Sockets
CLIENT KERNEL
Execute “10 + 3”
CLIENT KERNEL
Result: 13
CLIENT
KERNEL
CLIENT
What’s Inside IPython
• The Interactive IPython Shell (No ØMQ)
• Magic Commands
• Auto Word Completer
• Beautiful Traceback
• Shell History Management
Uh, And Kernel?
IPyKernel
• Jupyter’s “Kernel 0”
• ØMQ Handlers
• github.com/ipython/ipykernel
What’s Inside Jupyter
• Web-based Notebook Interface & nbconvert
• Interactive Qt Console
• Terminal-based Console
• Spec for Notebook Document Format
• Spec for Interactive Computing Protocol
CLIENT KERNEL
ØMQ Sockets
Interactive Computing
Protocol
• i.e. Jupyter Messaging Protocol
• Communication Between Kernel and Client
• Based on ØMQ and JSON
DEALER ROUTER
CLIENT KERNEL
Shell
SUB PUBIOPub
DEALER ROUTER
stdin
DEALER ROUTER
Control
REQ REP
Heartbeat
Connection File
{
"transport": "tcp",
"ip": "127.0.0.1",
"shell_port": 56166,
"iopub_port": 56167,
"stdin_port": 56168,
"control_port": 56169,
"hb_port": 56170,
"signature_scheme": "hmac-sha256",
"key": "<hashed key>"
}
Message Format
{
'header' : {
'msg_id' : str,
'username' : str,
'session' : str,
'date': str,
'msg_type' : str,
'version' : '5.0',
},
'parent_header' : dict,
'metadata' : dict,
'content' : dict,
}
http://bit.ly/JupyterMessaging
{
"header": {
"msg_type": "kernel_info_request",
...
},
"content": {},
}
CLIENT KERNEL
Shell
CLIENT KERNEL
Shell
{
"header": {
"msg_type": "kernel_info_reply",
...
},
"parent_header": {...},
"content": {
"banner": "Python 3.5.1 ... IPython 4.0.0 ...",
"implementation": "ipython",
"implementation_version": "4.0.0",
"language_info": {
"name": "python",
"version": "3.5.1",
...
},
},
}
CLIENT KERNELShell
{
"header": {
"msg_type": "execute_request",
...
},
"content": {
"code": "print('HAI')",
"silent": False,
"store_history": True,
},
}
CLIENT KERNELIOPub
{
"header": {"msg_type": "stream", ...},
"parent_header": {...},
"content": {
"name": "stdout",
"text": "HAIn",
},
}
CLIENT KERNELShell
{
"header": {
"msg_type": "execute_reply",
...
},
"parent_header": {...},
"content": {
"execution_count": 1,
"status": "ok",
"user_expressions": {},
},
}
CLIENT KERNELShell
{
"header": {"msg_type": "execute_request", ...},
"content": {
"code": "pd.read_csv('foo.csv')",
"silent": False,
"store_history": True,
},
}
CLIENT KERNELIOPub
{
"header": {"msg_type": "execute_result", ...},
"parent_header": {...},
"content": {
"execution_count": 5,
"data": {
"text/plain": " col1 col2n A 3n....",
"text/html": "<div><table>...</table></div>",
},
},
}
CLIENT KERNELShell
{
"header": {
"msg_type": "execute_reply",
...
},
"parent_header": {...},
"content": {
"execution_count": 5,
"status": "ok",
"user_expressions": {},
},
}
CLIENT KERNELShell
{
"header": {"msg_type": "execute_request", ...},
"content": {
"code": "read.csv('table.csv')",
"silent": False,
"store_history": True,
},
}
CLIENT KERNELIOPub
{
"header": {"msg_type": "execute_result", ...},
"parent_header": {...},
"content": {
"execution_count": 1,
"data": {
"text/plain": " col1 col2n A 3n....",
"text/html": "<table>...</table>",
},
},
}
CLIENT KERNELShell
{
"header": {
"msg_type": "execute_reply",
...
},
"parent_header": {...},
"content": {
"execution_count": 1,
"status": "ok",
"user_expressions": {},
},
}
MAKING A KERNEL
Kernel Types
• Native Kernel
• Python Wrapper Kernel
• REPL Wrapper Kernel
IRKernel
11. zmqctx <<- zmq.ctx.new()
12. sockets <<- list(
13. hb = zmq.socket(zmqctx, .pbd_env$ZMQ.ST$REP),
14. iopub = zmq.socket(zmqctx, .pbd_env$ZMQ.ST$PUB),
15. control = zmq.socket(zmqctx, .pbd_env$ZMQ.ST$ROUTER),
16. stdin = zmq.socket(zmqctx, .pbd_env$ZMQ.ST$ROUTER),
17. shell = zmq.socket(zmqctx, .pbd_env$ZMQ.ST$ROUTER))
IRKernel
IRuby
IHaskell
cling
IJulia
IScala
Lua Kernel
Clojure-Kernel
ITorch
IJavascript
ipykernel
Wrapper Kernel
• Python-driven
• ipykernel.kernelbase.Kernel
• http://bit.ly/WrapperKernel
from ipykernel.kernelbase import Kernel
class MyKernel(Kernel):
def do_execute
def do_complete
def do_history
def do_shutdown
Hy Kernel
Redis Kernel
Xonsh
C Kernel
Matlab Kernel
SAS Kernel
REPL Wrapper
from pexpect.replwrap
import REPLWrapper
Octave Kernel
Jupyter Powershell
Bash Kernel
Mongo Kernel
Scilab Kernel
LIVE DEMO
Q&A
Reference
• P.3, P.4, P.17, P.20, P.50 images: Project Jupyter
• P.6, P.7, P.8 images: Molg H.
• P.16: Project Jupyter
• P.11 image: Toomore Chiang
• P.12 image: Virginia Cheng
• P.13 image: ipython
• P.45 image: R

Jupyter Kernel: How to Speak in Another Language