Pyonic interpreter: a Python interpreter GUI for Android, written in Python

I’ve just released a new app, Pyonic Python 2 interpreter. Pyonic interpreter is a Python interpreter app for Android, providing a convenient interface adapted to mobile devices. The app itself is written entirely in Python using Kivy.

Screenshot of the interpreter app.

I put this together because I’ve always thought it would be nice to have a Python interpreter app that is itself written in Python, and in principle Kivy and python-for-android provide all the necessary components. In practice this worked even better than I expected, Kivy handled almost everything perfectly - I actually underestimated its maturity here! As part of the project, I’ve tried to round a number of corners that Kivy apps sometimes can to have, so that the interpreter (hopefully) behaves nicely in all situations. Within the interpreter, all of the standard library is available, and it’s possible to interrupt execution (equivalent to the normal ctrl+c behaviour) or to restart the interpreter process. No external modules are included yet except those necessary for the app to run, but I’ll probably include some major ones like numpy in a future release, and in the long term the aim is to support pip installs of new modules.

This has also been a great stimulus for working on python-for-android; I’ve fixed a number of bugs, added several new features, and improved documentation in several places, just thanks to needing these things in a real app.

On a technical level, Pyonic interpreter runs under Python 2, consisting of the app itself and a background Service running a second instance of the interpreter. I’ll be working on Python 3 support in the near future, in fact I originally wrote the app using Python 3 but switched to Python 2 partly due to incompatibilities in Kivy’s osc library (which should be easily fixed or avoided by just using a better communication library) and partly the more well-tested nature of python-for-android’s Python 2 build.

The interpreter works by passing submitted Python code to the background Python process where it is parsed as ast and compiled in ‘exec’ or ‘single’ mode as appropriate to replicate the output printing behaviour of the normal Python interpreter. Doing things this way is a little awkward and feels like reinventing the wheel, although I’m not sure how to better achieve the same thing. An alternative might be to just call the python binary in a subprocess and manipulate its stdin/stdout - I’ll be looking into this option, but it doesn’t eliminate the need for message passing and may need some small changes in python-for-android, assuming also that android doesn’t impose any important limits on subprocessing.

In the short term future, I expect to work first on releasing an improved version that adds a number of useful settings options (sneak peek in the image below), followed by working on a Python 3 version, and then to investigate some of these technical questions. I’d like to look into iOS support, as everything should work almost the same way there, but I don’t have the hardware or developer mempership for iOS development; if anyone would like to try it, let me know. Longer term, Pyonic interpreter is an experimental step towards creating a larger suite of mobile Python tools, in tandem with using this experience to improve python-for-android. There are many features to be added directly to the interpreter, but I’d also like to add surrounding tools including a full code editor, the ability to use pip to install other modules locally, and GUI support via additional Kivy activities.

Screenshot of the settings screen in the development version of Pyonic interpreter.

Settings screen in development to appear in the next release.

Pyonic interpreter: a Python interpreter GUI for Android, written in Python

I’ve just released a new app, Pyonic Python 2 interpreter. Pyonic interpreter is a Python interpreter app for Android, providing a convenient interface adapted to mobile devices. The app itself is written entirely in Python using Kivy.

Screenshot of the interpreter app.

I put this together because I’ve always thought it would be nice to have a Python interpreter app that is itself written in Python, and in principle Kivy and python-for-android provide all the necessary components. In practice this worked even better than I expected, Kivy handled almost everything perfectly - I actually underestimated its maturity here! As part of the project, I’ve tried to round a number of corners that Kivy apps sometimes can to have, so that the interpreter (hopefully) behaves nicely in all situations. Within the interpreter, all of the standard library is available, and it’s possible to interrupt execution (equivalent to the normal ctrl+c behaviour) or to restart the interpreter process. No external modules are included yet except those necessary for the app to run, but I’ll probably include some major ones like numpy in a future release, and in the long term the aim is to support pip installs of new modules.

This has also been a great stimulus for working on python-for-android; I’ve fixed a number of bugs, added several new features, and improved documentation in several places, just thanks to needing these things in a real app.

On a technical level, Pyonic interpreter runs under Python 2, consisting of the app itself and a background Service running a second instance of the interpreter. I’ll be working on Python 3 support in the near future, in fact I originally wrote the app using Python 3 but switched to Python 2 partly due to incompatibilities in Kivy’s osc library (which should be easily fixed or avoided by just using a better communication library) and partly the more well-tested nature of python-for-android’s Python 2 build.

The interpreter works by passing submitted Python code to the background Python process where it is parsed as ast and compiled in ‘exec’ or ‘single’ mode as appropriate to replicate the output printing behaviour of the normal Python interpreter. Doing things this way is a little awkward and feels like reinventing the wheel, although I’m not sure how to better achieve the same thing. An alternative might be to just call the python binary in a subprocess and manipulate its stdin/stdout - I’ll be looking into this option, but it doesn’t eliminate the need for message passing and may need some small changes in python-for-android, assuming also that android doesn’t impose any important limits on subprocessing.

In the short term future, I expect to work first on releasing an improved version that adds a number of useful settings options (sneak peek in the image below), followed by working on a Python 3 version, and then to investigate some of these technical questions. I’d like to look into iOS support, as everything should work almost the same way there, but I don’t have the hardware or developer mempership for iOS development; if anyone would like to try it, let me know. Longer term, Pyonic interpreter is an experimental step towards creating a larger suite of mobile Python tools, in tandem with using this experience to improve python-for-android. There are many features to be added directly to the interpreter, but I’d also like to add surrounding tools including a full code editor, the ability to use pip to install other modules locally, and GUI support via additional Kivy activities.

Screenshot of the settings screen in the development version of Pyonic interpreter.

Settings screen in development to appear in the next release.

python-for-android 0.4 released, now available on PyPI

We’ve just officially released python-for-android 0.4, and pushed it to PyPI for the first time!

python-for-android is a packaging tool for turning Python scripts and apps into Android APKs. It was originally created for use with the Kivy graphical framework, but now supports multiple kinds of Python app including Kivy, PySDL2, a webview interface with Flask or other webserver backend, plain Python scripts without a GUI, or other possibilities such as Python builds for use in other applications.

This release is the culmination of all the work over the last year to replace Kivy’s old Android toolchain with something more flexible and useful for other projects. Major features added in this time include the fully Python toolchain itself, support for SDL2 and other bootstraps, (experimental) python3 support via the CrystaX NDK, multiple architecture support, and many general improvements to the backend. Many thanks to all the contributors who have made this possible!

From now on we intend to move to regular versioned releases rather than the previous rolling master branch. Short term targets for the next release include bringing the python3 build up to full functionality and stability, and some argument restructuring to make command line usage simpler and clearer.

As of this release, you can now install python-for-android with simply:

pip install python-for-android

For full instructions and further information, see the python-for-android documentation.

python-for-android 0.4 released, now available on PyPI

We’ve just officially released python-for-android 0.4, and pushed it to PyPI for the first time!

python-for-android is a packaging tool for turning Python scripts and apps into Android APKs. It was originally created for use with the Kivy graphical framework, but now supports multiple kinds of Python app including Kivy, PySDL2, a webview interface with Flask or other webserver backend, plain Python scripts without a GUI, or other possibilities such as Python builds for use in other applications.

This release is the culmination of all the work over the last year to replace Kivy’s old Android toolchain with something more flexible and useful for other projects. Major features added in this time include the fully Python toolchain itself, support for SDL2 and other bootstraps, (experimental) python3 support via the CrystaX NDK, multiple architecture support, and many general improvements to the backend. Many thanks to all the contributors who have made this possible!

From now on we intend to move to regular versioned releases rather than the previous rolling master branch. Short term targets for the next release include bringing the python3 build up to full functionality and stability, and some argument restructuring to make command line usage simpler and clearer.

As of this release, you can now install python-for-android with simply:

pip install python-for-android

For full instructions and further information, see the python-for-android documentation.

python-for-android 0.4 released, now available on PyPI

We’ve just officially released python-for-android 0.4, and pushed it to PyPI for the first time!

python-for-android is a packaging tool for turning Python scripts and apps into Android APKs. It was originally created for use with the Kivy graphical framework, but now supports multiple kinds of Python app including Kivy, PySDL2, a webview interface with Flask or other webserver backend, plain Python scripts without a GUI, or other possibilities such as Python builds for use in other applications.

This release is the culmination of all the work over the last year to replace Kivy’s old Android toolchain with something more flexible and useful for other projects. Major features added in this time include the fully Python toolchain itself, support for SDL2 and other bootstraps, (experimental) python3 support via the CrystaX NDK, multiple architecture support, and many general improvements to the backend. Many thanks to all the contributors who have made this possible!

From now on we intend to move to regular versioned releases rather than the previous rolling master branch. Short term targets for the next release include bringing the python3 build up to full functionality and stability, and some argument restructuring to make command line usage simpler and clearer.

As of this release, you can now install python-for-android with simply:

pip install python-for-android

For full instructions and further information, see the python-for-android documentation.

Kivy at PyCon

Jacob Kovac, Kivy core developer and creator of the KivEnt game engine, is at PyCon 2016.

Click here or see below to watch his talk, Revitalizing Python Game Development: Packaging, Performance, and Platforms.

Kivy at PyCon

Jacob Kovac, Kivy core developer and creator of the KivEnt game engine, is at PyCon 2016.

Click here or see below to watch his talk, Revitalizing Python Game Development: Packaging, Performance, and Platforms.

Kivy 1.10 released

We’re pleased to announce a new stable version of Kivy, version 1.10. You can see the full detailed changelog on the mailing list announcement, and download the new version from the Kivy website or via your package manager.

This release includes many new features. Highlights include:

  • Python 3.5/3.6+ support on Windows. Support for these versions was previously available in nightly builds due to changes in the compiler toolchain, but official wheels are now available.
  • A new RecycleView widget replaces the old ListView. It is faster and more efficient, but with a simpler API.
  • Graphics backend improvements including ANGLE support on Windows and a mock backend for headless Kivy.
  • Several new Window options when using the SDL2 backend, for much improved cross-platform behaviour.
  • Improvements in Clock performance.

There are also many other new features and bugfixes, spread across over 1500 commits from almost 100 different developers. Thanks to everyone who contributed!

Kivy 1.10 released

We’re pleased to announce a new stable version of Kivy, version 1.10. You can see the full detailed changelog on the mailing list announcement, and download the new version from the Kivy website or via your package manager.

This release includes many new features. Highlights include:

  • Python 3.5/3.6+ support on Windows. Support for these versions was previously available in nightly builds due to changes in the compiler toolchain, but official wheels are now available.
  • A new RecycleView widget replaces the old ListView. It is faster and more efficient, but with a simpler API.
  • Graphics backend improvements including ANGLE support on Windows and a mock backend for headless Kivy.
  • Several new Window options when using the SDL2 backend, for much improved cross-platform behaviour.
  • Improvements in Clock performance.

There are also many other new features and bugfixes, spread across over 1500 commits from almost 100 different developers. Thanks to everyone who contributed!

Android apps with Python, Flask and a WebView

python-for-android has just gained support for a new webview app interface, an alternative to the existing SDL2 or Pygame backends. Under this mode of operation the app gui consists entirely of a browser window directed to open a webpage on localhost, and the Python backend can then run any web framework (I tested with Flask, but others like Bottle or even Django should work), serving this website and managing the app backend.

Example Flask app running on Android

This idea is not itself new; I think SL4A has supported a kind of webview interface for some time and certainly does so now, and we’ve previously seen users running web servers alongside Kivy. The difference to other projects is that apps can take advantage of python-for-android’s relatively extensive toolchain including python3.5 support, the ability to build popular libraries like numpy, support for multiple architectures, and access to the Android API via PyJNIus or Plyer rather than SL4A.

In the image of my testing app above, each of the vibration and orientation buttons sends a request to a Flask url that calls the Android API with PyJNIus to achieve the desired result.

Building a webview app

You can use the webview backend by adding --bootstrap=webview to your python-for-android command line (see the documentation for more details), or including webviewjni in your --requirements argument list. Note that this is incompatible with using SDL or Kivy because the webview bootstrap does not start or manage an OpenGL context. If for any reason you want to run a web server alongside a Kivy app, this is possible but you’ll need to use a different bootstrap and manage the webview yourself via PyJNIus from your Kivy code.

You should also add your chosen web framework to the --requirements argument, or include it your app directory so that it will be imported locally. If there isn’t a recipe for it and it’s a pure Python module, make sure you also add its Python dependencies as these aren’t automatically included right now (letting pip resolve dependencies causes issues when they include compiled modules that must be built separately). python-for-android now includes a recipe for Flask that automatically installs its dependencies (jinja2, werkzeug, markupsafe, itsdangerous and click), so you only need to add flask to the requirements in that case.

Technical details

It turns out that very little hackery is necessary to make a webview type app work. The APK seems to need the INTERNET permission to use a WebView, but Android is very happy for the Python code to run a web server with no further problems.

Making PyJNIus work required a little extra work, as it previously relied on the now-absent SDL to access a pointer to the current JNIEnv. This was fairly simple to fix by using only the relevant code from SDL2 - the important parts are only a small fraction of what SDL provides, as SDL has to worry about all the app input and output going via JNI. For now, python-for-android just patches PyJNIus before building it, but now that there are three different ways to get the JNIEnv on Android this will need addressing somehow in PyJNIus itself.