SlideShare a Scribd company logo
Building a custom
kernel for IPython
Hari Allamraju
anarahari@gmail.com
https://github.com/hari-allamraju
remitplan.com
rememberthebudget.in
What this talk is about
• What is an IPython kernel
• How does an IPython kernel work
• How can you build a simple wrapper kernel
• Some pointers on how to build a full fledged native
kernel
Why build a kernel?
• IPython does a lot of neat magic which makes it
invaluable for Python development
• There are no such tools for other languages, or even
applications which could benefit from having a nice
interactive front end
• We are developers, we can build such tools and share
them
• It’s fun to take things apart and see how they work and
could be the first step to you contributing to IPython itself
IPython architecture
Distributed client and kernel components talking over zeromq; separation
of client and kernel means you can add any client to the same kernel
Notebook is just another client
This can be extended to any client in any language or
interface
The two types of kernels
Changes after the recent Jupyter project
IPython messaging
zeromq sockets; all clients connected to a kernel receive
all messages and share the kernel context
Messaging spec
• You can find more details here - https://ipython.org/ipython-
doc/dev/development/messaging.html
• 4 types of channels
• Shell: request reply channel where all execution happens
• IOPub: this is where the kernel broadcasts the side effects
of any request to all clients
• Stdin: this is where the kernel requests input from the clients
• Control: for shutdown, abort etc
{
# The message header contains a pair of unique identifiers for the
# originating session and the actual message id, in addition to the
# username for the process that generated the message. This is useful in
# collaborative settings where multiple users may be interacting with the
# same kernel simultaneously, so that frontends can label the various
# messages in a meaningful way.
'header' : {
'msg_id' : uuid,
'username' : str,
'session' : uuid,
# All recognized message type strings are listed below.
'msg_type' : str,
# the message protocol version
'version' : '5.0',
},
# In a chain of messages, the header from the parent is copied so that
# clients can track where messages come from.
'parent_header' : dict,
# Any metadata associated with the message.
'metadata' : dict,
# The actual content of the message must be a dict, whose structure
# depends on the message type.
'content' : dict,
}
Full native kernel
• You have to handle all the zeromq sockets and
communication mechanism
• You have to ensure that the IPython messages are
created and parsed correctly for each type of request -
if you don’t implement something then you need to
handle that gracefully
• You have the freedom to structure your code as you
see fit
• https://github.com/dsblank/simple_kernel
Wrapper kernels
• Use IPython machinery to start the kernel and the
various zeromq channels which will be used to
communicate with the clients
• Provide the ability to create and parse the messages
as per the IPython messaging spec
• Give you specific end points or methods to implement
without having to worry about how it will all fit together
• https://github.com/takluyver/bash_kernel
Our focus - Wrapper
kernels
The base class
• from IPython.kernel.zmq.kernelbase import Kernel
• Available from IPython 3
• Provides you all the scaffolding needed to build the
kernel
The main properties
implementation
implementation_version
language
language_version
banner
Information for Kernel info replies. ‘Implementation’ refers to the kernel (e.g. IPython), and ‘language’ refers to the language it
interprets (e.g. Python). The ‘banner’ is displayed to the user in console UIs before the first prompt. All of these values are strings.
language_info
Language information for Kernel info replies, in a dictionary. This should contain the key mimetype with the mimetype of code in the
target language (e.g. 'text/x-python'), and file_extension (e.g. 'py'). It may also contain keys codemirror_mode and pygments_lexer
if they need to differ from language.
Other keys may be added to this later.
https://ipython.org/ipython-doc/dev/development/messaging.html#msging-kernel-info
The main method
do_execute(code, silent, store_history=True, user_expressions=None, allow_stdin=False)
Execute user code.
Parameters:
code (str) – The code to be executed.
silent (bool) – Whether to display output.
store_history (bool) – Whether to record this code in history and increase the execution count. If silent is True, this is
implicitly False.
user_expressions (dict) – Mapping of names to expressions to evaluate after the code has run. You can ignore this if you
need to.
allow_stdin (bool) – Whether the frontend can provide input on request (e.g. for Python’s raw_input()).
Your method should return a dict containing the fields described in Execution results. To display output, it can send
messages using send_response(). See Messaging in IPython for details of the different message types.
Launching the kernel
if __name__ == '__main__':
from IPython.kernel.zmq.kernelapp import IPKernelApp
IPKernelApp.launch_instance(kernel_class=MyKernel)
Let’s see some code!
• Some simple examples - echo and nodejs (with
pexpect)
• https://github.com/supercoderz/redis_kernel
• This is a wrapper kernel, connects to redis and
executes redis commands
• Almost complete in terms of functionality
Let’s see some code!
• Code overview of redis kernel
• More methods - https://ipython.org/ipython-doc/
dev/development/wrapperkernels.html
• This might change with Jupyter, still have to test
redis_kernel with Jupyter
Thank you!
Any questions?
Or you can reach me at anarahari@gmail.com

More Related Content

PPTX
ChatGPT 101.pptx
PPTX
The Flying Spaghetti Monster and Atheism
PDF
Uses of ChatGPT in Marketing
PPT
Check Six Gaming Partnership Presentation
PDF
Nielsen's Esports Playbook for Brands 2019
PPTX
The Creative Ai storm
PPTX
E sports
PDF
IPython & Jupyter
ChatGPT 101.pptx
The Flying Spaghetti Monster and Atheism
Uses of ChatGPT in Marketing
Check Six Gaming Partnership Presentation
Nielsen's Esports Playbook for Brands 2019
The Creative Ai storm
E sports
IPython & Jupyter

Viewers also liked (9)

PDF
JupyterHub for Interactive Data Science Collaboration
PDF
IPython Notebook as a Unified Data Science Interface for Hadoop
PDF
IPython
PDF
Scala and ZeroMQ: Events beyond the JVM
PDF
Cooperative Data Exploration with iPython Notebook
PDF
PLOTCON NYC: The Architecture of Jupyter: Protocols for Interactive Data Expl...
PDF
Introduction to IPython & Jupyter Notebooks
PDF
Jupyter, A Platform for Data Science at Scale
PPTX
Using IPython to Find Correlation
JupyterHub for Interactive Data Science Collaboration
IPython Notebook as a Unified Data Science Interface for Hadoop
IPython
Scala and ZeroMQ: Events beyond the JVM
Cooperative Data Exploration with iPython Notebook
PLOTCON NYC: The Architecture of Jupyter: Protocols for Interactive Data Expl...
Introduction to IPython & Jupyter Notebooks
Jupyter, A Platform for Data Science at Scale
Using IPython to Find Correlation
Ad

Similar to Building custom kernels for IPython (20)

PPTX
Python_Introduction_Good_PPT.pptx
PDF
Python quick guide1
PDF
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
PDF
Fundamentals of python
PPTX
Python final presentation kirti ppt1
PPTX
Python fundamentals
PDF
Class_X_PYTHON_J.pdf
PPT
python-ppt.ppt
PPT
python-ppt.ppt
PPTX
Introduction-to-Python-Programming1.pptx
PPTX
Module1-Chapter1_ppt.pptx
PDF
PHYTON-REPORT.pdf
PDF
Socket programming-in-python
PDF
Introduction to Python Unit -1 Part .pdf
PDF
Free Python Notes PDF - Python Crash Course
PDF
Python programming
PPTX
Introduction to Python Programming
PPTX
python programminig and introduction.pptx
PDF
kecs105.pdf
PDF
Dr. Tanvi FOCP Unit-2 Session-1 PPT (Revised).pdf
Python_Introduction_Good_PPT.pptx
Python quick guide1
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
Fundamentals of python
Python final presentation kirti ppt1
Python fundamentals
Class_X_PYTHON_J.pdf
python-ppt.ppt
python-ppt.ppt
Introduction-to-Python-Programming1.pptx
Module1-Chapter1_ppt.pptx
PHYTON-REPORT.pdf
Socket programming-in-python
Introduction to Python Unit -1 Part .pdf
Free Python Notes PDF - Python Crash Course
Python programming
Introduction to Python Programming
python programminig and introduction.pptx
kecs105.pdf
Dr. Tanvi FOCP Unit-2 Session-1 PPT (Revised).pdf
Ad

Recently uploaded (20)

PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
medical staffing services at VALiNTRY
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
Introduction to Artificial Intelligence
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Nekopoi APK 2025 free lastest update
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Digital Strategies for Manufacturing Companies
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
history of c programming in notes for students .pptx
PPTX
ai tools demonstartion for schools and inter college
PDF
Understanding Forklifts - TECH EHS Solution
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Reimagine Home Health with the Power of Agentic AI​
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Upgrade and Innovation Strategies for SAP ERP Customers
medical staffing services at VALiNTRY
wealthsignaloriginal-com-DS-text-... (1).pdf
Introduction to Artificial Intelligence
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Nekopoi APK 2025 free lastest update
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Digital Strategies for Manufacturing Companies
Computer Software and OS of computer science of grade 11.pptx
Odoo Companies in India – Driving Business Transformation.pdf
Design an Analysis of Algorithms II-SECS-1021-03
PTS Company Brochure 2025 (1).pdf.......
history of c programming in notes for students .pptx
ai tools demonstartion for schools and inter college
Understanding Forklifts - TECH EHS Solution
How to Choose the Right IT Partner for Your Business in Malaysia
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...

Building custom kernels for IPython

  • 1. Building a custom kernel for IPython Hari Allamraju anarahari@gmail.com https://github.com/hari-allamraju remitplan.com rememberthebudget.in
  • 2. What this talk is about • What is an IPython kernel • How does an IPython kernel work • How can you build a simple wrapper kernel • Some pointers on how to build a full fledged native kernel
  • 3. Why build a kernel? • IPython does a lot of neat magic which makes it invaluable for Python development • There are no such tools for other languages, or even applications which could benefit from having a nice interactive front end • We are developers, we can build such tools and share them • It’s fun to take things apart and see how they work and could be the first step to you contributing to IPython itself
  • 4. IPython architecture Distributed client and kernel components talking over zeromq; separation of client and kernel means you can add any client to the same kernel
  • 5. Notebook is just another client This can be extended to any client in any language or interface
  • 6. The two types of kernels Changes after the recent Jupyter project
  • 7. IPython messaging zeromq sockets; all clients connected to a kernel receive all messages and share the kernel context
  • 8. Messaging spec • You can find more details here - https://ipython.org/ipython- doc/dev/development/messaging.html • 4 types of channels • Shell: request reply channel where all execution happens • IOPub: this is where the kernel broadcasts the side effects of any request to all clients • Stdin: this is where the kernel requests input from the clients • Control: for shutdown, abort etc
  • 9. { # The message header contains a pair of unique identifiers for the # originating session and the actual message id, in addition to the # username for the process that generated the message. This is useful in # collaborative settings where multiple users may be interacting with the # same kernel simultaneously, so that frontends can label the various # messages in a meaningful way. 'header' : { 'msg_id' : uuid, 'username' : str, 'session' : uuid, # All recognized message type strings are listed below. 'msg_type' : str, # the message protocol version 'version' : '5.0', }, # In a chain of messages, the header from the parent is copied so that # clients can track where messages come from. 'parent_header' : dict, # Any metadata associated with the message. 'metadata' : dict, # The actual content of the message must be a dict, whose structure # depends on the message type. 'content' : dict, }
  • 10. Full native kernel • You have to handle all the zeromq sockets and communication mechanism • You have to ensure that the IPython messages are created and parsed correctly for each type of request - if you don’t implement something then you need to handle that gracefully • You have the freedom to structure your code as you see fit • https://github.com/dsblank/simple_kernel
  • 11. Wrapper kernels • Use IPython machinery to start the kernel and the various zeromq channels which will be used to communicate with the clients • Provide the ability to create and parse the messages as per the IPython messaging spec • Give you specific end points or methods to implement without having to worry about how it will all fit together • https://github.com/takluyver/bash_kernel
  • 12. Our focus - Wrapper kernels
  • 13. The base class • from IPython.kernel.zmq.kernelbase import Kernel • Available from IPython 3 • Provides you all the scaffolding needed to build the kernel
  • 14. The main properties implementation implementation_version language language_version banner Information for Kernel info replies. ‘Implementation’ refers to the kernel (e.g. IPython), and ‘language’ refers to the language it interprets (e.g. Python). The ‘banner’ is displayed to the user in console UIs before the first prompt. All of these values are strings. language_info Language information for Kernel info replies, in a dictionary. This should contain the key mimetype with the mimetype of code in the target language (e.g. 'text/x-python'), and file_extension (e.g. 'py'). It may also contain keys codemirror_mode and pygments_lexer if they need to differ from language. Other keys may be added to this later. https://ipython.org/ipython-doc/dev/development/messaging.html#msging-kernel-info
  • 15. The main method do_execute(code, silent, store_history=True, user_expressions=None, allow_stdin=False) Execute user code. Parameters: code (str) – The code to be executed. silent (bool) – Whether to display output. store_history (bool) – Whether to record this code in history and increase the execution count. If silent is True, this is implicitly False. user_expressions (dict) – Mapping of names to expressions to evaluate after the code has run. You can ignore this if you need to. allow_stdin (bool) – Whether the frontend can provide input on request (e.g. for Python’s raw_input()). Your method should return a dict containing the fields described in Execution results. To display output, it can send messages using send_response(). See Messaging in IPython for details of the different message types.
  • 16. Launching the kernel if __name__ == '__main__': from IPython.kernel.zmq.kernelapp import IPKernelApp IPKernelApp.launch_instance(kernel_class=MyKernel)
  • 17. Let’s see some code! • Some simple examples - echo and nodejs (with pexpect) • https://github.com/supercoderz/redis_kernel • This is a wrapper kernel, connects to redis and executes redis commands • Almost complete in terms of functionality
  • 18. Let’s see some code! • Code overview of redis kernel • More methods - https://ipython.org/ipython-doc/ dev/development/wrapperkernels.html • This might change with Jupyter, still have to test redis_kernel with Jupyter
  • 19. Thank you! Any questions? Or you can reach me at anarahari@gmail.com