Live Coding with Kivy and IPython

I recently began to implement some game prototypes with the help of Kivy, a very interesting Python library originally developed to prototype innovative user interfaces. Combined with IPython, Kivy could also be interesting for live coding, and so I checked out how the input hooks for other event loops work in IPython. Currently, there are hooks for Qt, glut, pyglet, GTK, wx and tk build in. That means that you can use any of those frameworks to develop GUI apps interactively from within an IPython shell.

For Kivy, I copied one of the existing hooks and came up with the following code:

https://github.com/pbouda/stuff/blob/master/kivy_livecode/helpers.py

The function enable_kivy() would return an Kivy app object that you can then use to add widget interactively. Just start an IPython shell and add a rectangle to a Kivy window, for example:

[1] import helpers
[2] app = helpers.enable_kivy()
[3] from kivy.graphics import *
[4] with app.root.canvas:
    Color(1.0, 0.2, 0.0)
    Rectangle(pos=(850,450), size=(200,200))

Unfortunately, you have to patch the Kivy sources to run in interactive mode at the moment. Kivy has a so-called slave mode, but it is not available when you call the default run() method on the App object. Here is a simple patch that just pipes the slave parameter to the runcTouchApp() method and will allow you to start the Kivy app in slave mode:

https://github.com/pbouda/kivy/commit/e1659dea1986f4a6006193d697c86d3694652cc1

Have fun!

About me

My name is Peter Bouda and I am and Angular/JavaScript coach and consultant with more than 15 years of professional experience in web application development. I help you to bring your web product to market as quickly as possible.

Read about my work
or
Contact me

Send me a message and I will get back to you.