Skip to main content

Introducing Enaml Native - Write native Android apps in python

In my recent post on react-native vs kivy, I suggested an alternative that bridges some of the concepts of each of the two projects together. Well, I decided to make one. The result is enaml-native.

What is enaml-native?

The goal of enaml-native is to be the python version of react-native.  Or to change their tag line:

enaml-native lets you build mobile apps using only python. It uses the same design as enaml, letting you compose a rich mobile UI from declarative components.

How does it differ from Kivy or Rubicon? 

enaml-native borrows a lot from kivy, including pyjnius, and python-for-android. Much of the bootstrap code was based on kivy's implementation. However, unlike kivy, which includes it's own widget framework, enaml-native uses native widgets. Edit: enaml-native no longer relies on pyjnius. It now uses the JNI directly to improve startup times (or Websockets for remote debugging).

It differs from kivy and BeeWare's rubicon projects in that it uses a bridge, like react-native does. With this design, python is completely decoupled from the native app (thus preventing it from slowing down the UI).

How does it work?

It's design is based on react-native.  There are three threads, the main UI thread, the bridge thread, and the python thread.  When the app starts, it kicks off the bridge thread, and the python thread. 

Bridge thread

The bridge thread has it's own message handler. The python thread queues and then pushes a bundle of msgpack events which get's sent to the bridge thread for processing. The bridge thread unpacks each event and translates it into a create, update, or delete modification of a Java object. Results and events are packed, batched, and returned back to python by using the jnius NativeInvokationHandler. Edit: It now replies using the JNI directly (or Websockets when remote debugging).

That's about it. A detailed overview of the bridge can be found here.

Python thread

Python is run in a thread with it's own event loop (either tornado or twisted) and communicates with the bridge thread using msgpack buffers (currently transported over the JNI).  

With this implementation, objects are not converted back and forth, they are passed as references, which greatly reduces overhead. Additionally, even though python is without a doubt slower than Java and Objective C, since the the bridge is async, and python is in a thread, it prevent's python from slowing down the UI keeping animations and transitions smooth.

Finally, a toolkit for enaml was implemented that to declares the UI and handle all events and interaction required (using the bridge behind the scenes). It's extremely easy and extremely powerful.

Does it work?

Yes, try the demo app on the play store. I update it periodically. You can view the project on github, and there's a few videos I've been adding on youtube.

enaml-native app showing a toolbar, drawer layout and tabs

What does an enaml-native app look like?

Like enaml, just different widgets. See the examples.  For example:

from enamlnative.widgets.api import *

enamldef ContentView(LinearLayout):
        text = "Hello world!"

Displays the hello world.

When is iOS support coming? 

As soon as I get time. I started working on an iOS implementation but have not yet finished. The main blocker at the moment is compiling all the libraries with kivy-ios. Not yet released, but I've added some features to it already (including pip installed libraries).

Can I help? 

Yes, please do. You can pull the repo and test it out, submit new widgets. Share it, whatever! Anything helps!


Popular posts from this blog

Kivy vs React-Native for building cross platform mobile apps

I've built three apps now using Kivy and one with React-Native, just wanted to share my thoughts on both.

Just a warning, I am strongly biased towards python and this is all based on opinion and experience and is thus worth what you pay for it. I don't claim to be an expert in either of these, just have worked with each for several months.  If something is incorrect I'd love to hear advice.

Demo of one of the apps

Nice to be able to run natively on the desktop WITHOUT a simulatorPython is easy to work withUse (almost) any python libraryVery easy to create custom widgetsKivy properties and data binding just work. Way nicer than React's "state" / flux / redux whatever you want to call it (stupid?). Native interfaces (pyjnius) and (pyobjc)Runs and feels pretty smooth Cons:Default widget toolkit looks like Android 4.4. Requiring you use your own widgets or a theming kit like KivyMD if styling bothers youCreating dynamic widgets declaratively is not yet s…

Control Systems in Python - Part 1 - Bode and Step Response

I hate matlab with passion, yet sadly, nearly everyone uses it.  I'm a fan of Python and open source stuff so here's a simple article on how to do some common control systems stuff in Python.

First we need to make sure the environment is setup.
Install IPython (or you can use any other python shell, but a unicode supported shell is preferred)Install python-control (numpy, scipy)Install sympy
These should do if your on Ubuntu/debian:

sudo apt-get install python-sympy python-numpy python-scipy python-matplotlib ipython
Then you need to install python control, see How to download and install python-control
Intro to using SympyOpen ipython and run the following:

import sympy from sympy import * sympy.init_printing() s = Symbol('s')

Now we can do things like define transfer functions using the symbolic variable s.

We can expand the bottom using the .simplify() method

and we can do something more complex like...
which is really nice because it does all the multiplication for us... and it’…