Skip to content

(Video) Running Python Scripts on the Android Frontend with Chaquopy

 

Transcript:

Python is an amazing language for quite a number of reasons. It’s easy to pick up and learn, even as a first programming language, large applications can have development times significantly reduced when written in Python, it’s amazing for machine learning and data science, and there are a huge number of amazing python scripts out there written by other developers.

That being said, Python is primarily a language used for programming on the backend of an application. OR so I thought

But quite a number of you seem to be at the very least curious about the idea of running Python code on the frontend. Particularly in Android apps.

What’s up lads. This is Eric Decanini. Welcome to the first episode of…. Eric Decanini

 

Before anything else, just a little disclaimer, I’m not associated or sponsored by Chaquopy in any way. I wish I was.

But it is a pretty interesting idea to run Python scripts in an Android application. After all, there are plenty on Python scripts that offer complex functionality such as machine learning functions that you may not find so easily as a library you can just dump into Gradle.

There are plugins out there that let you write entire applications in Python but we’re not about that life. We still want to keep Kotlin as our main codebase okay? We just want to take advantage of a script or two in Python. That’s why we’ll be using Chaquopy.

I like Chaquopy because it’s so simple and does exactly what we want to do. Let me show you how it’s done.

 

So first you need to set it up. In your project-level build.gradle file, add the chaquo maven repository, as well as the python gradle dependency. All code snippets are linked below in the description.

Next in your app-level build.gradle file, you need to apply the chaquo python plugin at the top of your file, but after apply plugin com.android.application. In defaultConfig, add these sourceSet, python, and ndk attributes as shown.

A few pointers here. srcDirs in the sourceSets brackets points to where you’re keeping your Python scripts in the app. It doesn’t have to be in src/main/python, It can be anywhere you like okay.

Each version of the Chaquopy plugin comes with a version of Python but some features require Python 3.5 or higher to be available on the build machine. To meet that requirement, we have the python brackets pointing to where I keep my Python on my Mac OS device along with the matching version. You don’t have to specify the specific minor version. If Chaquopy doesn’t find this, it will attempt to match the major version using the standard command of your operating system.

Here’s a list of Chaquopy versions, the Python version included with them, as well as the Gradle plugin versions they support. Note that this is fairly limited for each version.

In the ndk brackets here, you are enabling the ABIs or Application Binary Interfaces you want to support with Python. Essentially specifying the devices you want Python to work with. Armeabi-v7a is supported by virtually all Android devices, while x86 or x86-64 are Android Emulator ABIs. These are all specified in the Chaquopy installation docs linked down in the description.

And that’s all for the set up. It’s at this point you want to put in all your Python scripts in the folder you specified with the source directory attribute.

 

To load in our Python scripts and call the functions within them, we must call Python.start. You can do this in the creation of your Activity but if you’re gonna be using Python across multiple activities, you would rather do it in your Application class.

Now here I have an extremely bare Python script I’m putting in my app called helloworldscript.py and all it has is a single function that returns a string. Nothing else, we just keeping it simple. Just for any other curious guys out there, this is the utmost extent of my Python programming.

Now let’s go back to our activity and run this function. Get an instance of Python, get your Python script with getModule passing in the file name of your script without the py extension, then run your function with callAttr passing in the name of your function. If you need to pass in input parameters to your function, just append any amount of them as parameters here.

This returns a PyObject value and this can be converted into quite a number of different types, you know, not too unlike a JsonObject when you’re parsing your apis and all.

And there you have it! This is my application right here taking advantage of Python functionality and it is indeed displaying the string returned by the script.

So if you don’t want to run your entire app in Python but just want to run a script or two while keeping your app nice and Kotlin, Chaquopy is the plugin for you. I would like to hear how this is being used by you guys, please leave it as a comment below if you’re one of those people. Please give me an onClick on that like button and subscribe if you want more Android content and I will see you in the next video.

 

Tags: