BEGIN TYPING YOUR SEARCH ABOVE AND PRESS RETURN TO SEARCH. PRESS ESC TO CANCEL

Creating a Brainfuck Kernel for IPython

You can find the code for the brainfuck kernel here.

Intro to IPython

Wouldn’t it be awesome if you were able to write some beautiful pieces of code in your browser, and have the ability to run your code and display the results in that browser?

As many people already know, and you’ve probably guessed from the heading, that’s exactly what IPython does. An incredible piece of (free) software where, using python code, you can create beautiful IPython Notebooks which can be saved, shared and viewed in any browser. It offers lots of other features for data scientists, bloggers and developers; but today, I’m only interested in the Notebook feature.

One of the best things about IPython is: It potentially supports every programming language.
This is because IPython is designed in a way which allows its community to easily create their own language kernels that can interact with the IPython core.

Some people have already created language kernels for IPython implementing the Wire Protocol in their chosen language (from scratch) to communicate with the Python backend.

Sounds like a lot of work, is there an easier way?

You’re in luck. There is a much easier way, but only if the language you want to create the kernel for can be executed from Python, or if it the language contains Python bindings.

Cool Story Bro – How?

Simple. IPython’s Python Wrapper Kernels.

Wrapper Kernels
Recently, I read this documentation about IPython’s Python Wrapper Kernels. I was pretty impressed, and seeing as I’ve had experience in building an IPython Language Kernel from scratch (IErlang)… I decided I’d give the “Simple Wrapper Kernel” a go and build a Brainfuck Kernel.

So WTF is a wrapper kernel for IPython?
A Kernel Wrapper is a piece of Python code that gives you a base framework to create a kernel for IPython.

You should create a wrapper kernel when:

  • Your language contains bindings for python, or
  • Your language can be executed from python code

Otherwise, you will have to implement your own kernel and implement IPython’s Wire Protocol. A great place to start is this blog post by the creator of IHaskell.

Brainfuck Wrapper Kernel – How?

This section will interest you if you are interested in creating a wrapper kernel for IPython. Instead of rehashing the Brainfuck Kernel that I created, I will explain, step by step, how to create a wrapper kernel. Also note that this is largely plagiarised from IPython’s official documentation on Wrapper Kernels, but contains some information that took me a while to figure out by myself.

Step 0 – Install IPython 3.1.x
To install IPython, just run the command:

Once installed, be sure to check that it works by typing the command:

Step 1 – Create Your Project
Create your project directory and add the following file called YOUR_LANGUAGEkernel.py ; then populate the file with the following:

NOTE: Anything in capital letters should be renamed by you
Step 2 – Execute Your Code

Work your magic coding abilities and implement the function function_that_will_execute_my_code_and_return_a_result(code). This can be done in a function or you can create a new module for this. What I did with IBrainfuck is, I scoured Github searching for a Brainfuck interpreter written in python, and luckily enough I found one (thanks to Joel Buchheim-Moore). I was then able to send the code to that module and retrieve a result.

Step 3 – Create a Kernel Profile

Now that you’ve created your Kernel, you need to do a couple of things before you can get it up and running. You need to create a kernel profile (json file) which will tell IPython how to run your kernel.

Firstly, create the profile directory:

Note: In that command, YOUR_LANGUAGE_NAME should be equal to language = YOUR_LANGUAGE_NAME  in your wrapper kernel (line 6)

Secondly, create a file called kernel.json in ~/.ipython/kernels/YOUR_LANGUAGE_NAME/ and populate it with the following:

 

From this, IPython should be able to discern where to find your kernel and how to start it.

That’s It

You’ve now completed the necessary steps. To start your kernel, all you have to do is run:

 

Then in the top right corner, select your kernel from the list of kernels:

bfkernel

Once you select your kernel, you can start creating awesome notebooks…

bfkernel

So there you have it, a quick and easy method for creating language kernels for IPython. If you decide to create one, then feel free to send me a link for a working version!

Leave a comment

Please be polite. We appreciate that. Your email address will not be published and required fields are marked