Friday 2:45 p.m.–3 p.m.

A pure Python GUI tookit based on web technology

Almar Klein

Audience level:
Intermediate

Description

This talk describes a cross-platform, pure Python toolkit for creating graphical user interfaces (GUI's), that uses web technology for its rendering. You can use it to create desktop applications, web apps, and add widgets to the IPython/Jupyter notebook.

Abstract

To create user interfaces in Python, there are multiple GUI toolkits available. While some (most notably Qt) are very powerful, they are all focussed on desktop applications. It cannot be denied that the web (i.e. browser technology) has become an increasingly popular method for delivering applications to users, also for (interactive) scientific content.

Creating applications in JavaScript is inherently painful, especially for developers that are used to a language in which things actually work as expected, such as Python. Indeed, there are several solutions to write web applications in Python, but they all involve the conversion of the whole application to JavaScript, or running the code in a JavaScript-driven Python runtime. There is currently no way for a (normal) Python process to run a user interface with JavaScript.

With Flexx (https://github.com/zoofIO/flexx) we're building a Python library for creating graphical user interfaces (GUI's) that use web technology. The library is cross-platform and written in pure Python. Its only dependencies are Tornado (also pure Python) and a web browser. It allows creating desktop applications, web apps, and can be used inside the IPython/Jupyter notebook.

To make this possible, Flexx combines a few concepts in a novel way. Firstly, it allows launching different "web runtimes" --- these can be a browser, or e.g. the XUL runtime, which is essentially a FireFox browser that looks like a desktop app. Secondly, Flexx sets up an active connection between Python and the web runtime, implemented via a websocket. Thirdly, it implements a class similar to IPythons HasTraits, where each instance of this class has a corresponding representation in JavaScript, and the properties/traits of the two are synchronised. The widgets classes are all subclasses of this class.

One key concept is the ability to implement JavaScript methods by writing Python code. In fact, there is one definition of each class, which contains both methods that run in Python and methods that run in the web runtime. To this end, Flexx provides a Python to JavaScript translator. This means that we did not write a single line of JavaScript to implement the widgets. Further it allows users to define callbacks that run entirely in JavaScript, which is crucial to get responsive behaviour for e.g. mouse-move event handlers. When an application is implemented such that it does not rely on Python callbacks, it can be exported to a single-page HTML document.

This project is currently a work in progress, and much work needs to be done to bring it anywhere near the usability of e.g. Qt. However, it provides a way to write user-interfaces that can be deployed either as desktop applications or web apps, which can be a powerful means to publish scientific results in an interactive manner. A nice side-effect is that the library is pure Python, allowing it to work on less mainstream Python implementations such as Pypy, and avoiding the often complex build process necessary by most desktop GUI toolkits.

Sponsors