Back to the video

Python on your phone: Building mobile apps with Kivy by Derek Payton

Transcript

By Derek Payton, Speaker 2, Speaker 3, Speaker 4, Speaker 5, Speaker 6, Speaker 7, Speaker 8. From DjangoCon US 2018. Published November 8, 2018.

Watch the video Markdown version 5,149 words · auto-generated · en

Summary

Derek Payton explains how Kivy lets Python developers build cross-platform, multitouch applications for Android, iOS, desktop systems, kiosks, and touchscreens. He demonstrates Kivy widgets, event handling, the KV design language, and KivyMD’s Material Design components, then shows how Buildozer packages, deploys, and runs apps on mobile devices. Hardware access can use PyJNIus and PyObjC, or the platform-independent Plyer API, although platform coverage varies; he presents his guitar-practice app ChordWise as a practical example. He argues that Kivy makes mobile development accessible to Python programmers, while noting limitations around native UI, accessibility, background services, push notifications, and platform-specific build requirements.

Transcript

Automatically transcribed, so expect mistakes in names and technical terms. Filler words are removed.

0:00 Introduction and Motivation

Yeah, I think that's a good thing.

Derek Payton

Yeah, so my name is Derek. By day I'm the director of development at Order Slip. We build apps for small restaurants to have mobile ordering. By night and all of my other free time, I do things like co-found a hackerspace. which to my knowledge is the only one between LA and the Bay Area, which is kind of cool. I'm on the board of directors for an organization called 59 Days of Code. We do a lot of tech education and trying to grow the technology scene in the Central Valley. And then I also run my local Friends No Python chapter. All of the slides as well as the code that we're gonna take a look at is online on GitHub,

bit. ly slash qb-djangocon. Definitely recommend checking that out. Grab a picture, look it up, follow along. There'll be some URLs in there as well that you can check out as we go through the talk. So I'm not a mobile app developer. My whole background is web development. I've been using Django since 0. 95 or 6. And so that's that's kind of where I'm coming from in this, right? I've been using Python for quite a while. It's a language that I really like. But I when I lived in San Diego,

there's a classical radio station called KUSC based out of LA. And We live just outside of their broadcast range, right? There's this one hill in Escondido where there's a target and in the in the parking lot you can you can get this classical station. But so KUSC they stream online they didn't have a mobile app they also didn't have a mobile friendly website And I thought to myself, this is a solvable problem. I can do this. And so I dove in, started learning Android. And I put this together. This was just something to scratch my own itch.

Stream their the grab their MP3 stream. Play it on the phone, script their website once a minute to get what's now playing. It was native Android, XML layouts, and so forth. But there was a problem. And the problem was that it wasn't enjoyable. I've been doing Python. I love Python. I've been a part of the community. That that excitement that you feel when you're learning new things, when you're when you're building new stuff, it wasn't there for me. And I'm not trying to knock Java or native development. But the problem is that I really like Python. And ultimately, I want to build software that runs on my phone, but I would also prefer

3:21 Python Mobile Frameworks

Derek Payton

to build things with Python. So that kind of leaves me figuring out what options do we have? What are the options that we have? And the first one that I have to mention is the BWAR project. So Beware is a project aimed at building applications in Python across multiple platforms using native UIs. You should definitely check it out. Beware developers are here at DjangoCon, Russ Keith McGee, Philip James, Kitty McLaughlin, they're gonna be sprinting on it. Definitely seek them out, ask them questions, learn more about the Beware project. And that brings us to Kiwi, which is a framework for building multi-touch applications across platforms.

So it's all geared towards multi-touch. It's not specifically mobile development. You can build something that runs on your phone, yes of course. Pinch to zoom, all of that is there. But it's also for things like if you need to build out a kiosk running on a touchscreen TV. That's a really good application for Givi. QV is open source under the MIT license and it's cross-platform, runs on Android and iOS, also Windows, Linux Mac OS what have you. And Kivi is pretty fast. And it's fast because of Python, right? The bulk of the bottleneck in developing mobile apps with Python

4:58 Kivy UI and Widgets

Derek Payton

isn't your application code. It's gonna be the graphics and the rendering. And so Cython kind of bridges that gap, allows you to add static type definition or declarations to your Python code, effectively compiling it into C And then you're basically just running C at that point. So we'll do sh quick little demo. Woo. And kind of take a look at what kiwi looks like. So there's tons of examples in the kibi codebase. And we're gonna look at one the Kiwi catalog. Kiwi catalog,

is just a collection of different layouts and widgets kind of showcasing what Kiwi looks like. These are all Kiwi widgets. With Kivi you're not building native UIs. They have their own UI layer. But you can do things, different layouts, box layout, stack things up, put them in a grid. There's different things you can do with buttons, kind of showcasing. Different buttons and events there, things like progress bars and sliders, all of this implemented kind of in an extensible manner and available to use. Now the QV UI is not super exciting. Very grayscale with some blue accents.

And it's it's totally customizable. You can change all the colors and whatnot. But maybe you want something a little more complex without having to put a ton of work into it. And that is where There's a project called Kivi MD. That implements material design in Kiwi. So if something like this is more up your alley. There's a lot of material design widgets included with this, all styled according to the material design spec. Everything, you know, buttons, again, a lot of the same things that you see in regular Kiwi. menus, it all looks decent and is a way to kind of give you a good starting point for building out more of your app's

7:18 Kivy Installation and Hello World

Derek Payton

UI. And with that, we kind of go into looking at the code. So this is where it becomes kind of beneficial to follow along. The first thing that we need to do is install Cython and Kivi. The specific versions of Kivi work with specific versions of Cython. So you want to make sure look at the documentation, see what version of Cython you need. There's other useful but optional dependencies as well, things like Pygame. So remember when Kiwi implements its own UI layer on the desktop it's using Pygame to draw the window and write to that canvas.

And this is our first hello world. We have seven lines of Python that display hello world. Not a whole lot of excitement going on here. Very simple. Just writing some text onto the window. Seven lines of code doesn't get a whole lot simpler than that. First thing we want to do as we step through this is import the app class and the label widget. Your application must inherit from the at class. You need to implement a build method that returns a single widget. This is the root widget of your entire widget hierarchy, the widget tree. And then we run it. Not a whole lot going on here besides that.

8:52 Multitouch Interactions

Derek Payton

Now QV is meant for multi-touch uses. So It makes it really easy to do interesting things with zooming, rotating, scaling and so forth. So looking at this example, same thing. We have hello world displayed in the window. But we can also set touch points and Play around with the text a little bit. So it's this is kind of the core of all of the multi-touch interactions on Kiwi. is this thing called the scatter layout. This is how it implements those multi-touch functionality. So the same thing

import the app and the label and the scatter layout. Only this time we're going to instantiate the scatter layout We have our label, we're going to add that as a child onto the layout and return that layout. That's all we have to do to get some neat multi-touch functionality into our app. And anything can go into a scatter layout. Events. So We don't just want to show things on the page. We want to allow people to interact with buttons, right? What do you do when you press a button? What happens next? The code for this slide is a little too long to look at once on one slide, but what we can do.

10:23 Events and Pop-ups

Derek Payton

And kind of showing a demo. We have a button that gets rendered to the very middle of the window. The button says click me. When we click the button, we have a pop-up that has another button that says close me. Very simple event handling here. What we're gonna end up doing is importing the float layout. So the float layout works very much like CSS in terms of positioning. We also want to import the pop-up widget. Going into our build method, we instantiate the float layout, create a button, the size hint and the position hint do exactly what you would expect. We add the button to the float layout and then we have this pop-up.

Now, one thing that you might notice in here As we define the pop-up and the button that goes inside of it, we're not attaching the pop-up to the float layout. And the reason for that we'll get to in the next example, but it's something that I wanted to point out here. The event handling itself is pretty simple. We have the open button and we want to bind the on-release event to pop-up. open, and on the close button we want to bind the on-release event to pop-up. dismiss. And then finally we return the layout. Now As you build out your Python app, doing all of your widgets in Python, as your app grows and becomes more complex,

11:58 KV Design Language

Derek Payton

Doing all of this stuff in Python can become a little bit messy, very verbose and you know can be kind of tough to navigate. So Kibi tries to solve this with the KV design language The KV design language is a very YAML-esque language where you define the tree of your widgets and it uses that to instantiate the actual Python objects. So now in our main. py this is all the Python that we have. We have five lines of Python, there's nothing in our demo app, and we just run it We have this demo. kv, right? Very YAML-esque. The name demo. kv, the name of your KV file is derived from the name of your application class.

So we have demo app, we get demo. kv. This is the same hierarchy that we had before with the buttons, the pop-up, the buttons inside the pop-up. And as we go into the specifics, we see the same attributes. The size hint, position hint, on release. What we're doing with the on-release now from within KV, we can access the root widget, look up the pop-up by ID, and call open on it. Now an interesting thing to keep in mind here is that everything to the right of the colon is valid Python syntax You can use data structures, lists, tuples, dictionaries list comprehensions, you can call functions, you can do math. There's all sorts of things that you can do. You can treat it as a valid Python expression.

We have our pop-up again that we are initializing. Same attributes as before with the addition of the on parent that we're adding. Now just like the build method needs to return a single widget that's your root widget same with dem your KVLang files. You need one widget at the very top. So the pop-up currently is now inside the float layout, right? It has a parent. And that's not what we want. We want the pop-up to have no parent. If it has a parent, it's going to get rendered to the screen. And we don't want that to happen until we press the button. So all we're doing here is saying when it gets parented, if the parent is the layout, just automatically remove itself

from the layout, and we get the same functionality that we had before. And then of course we have the same button, same on release, look up the pop-up by ID and dismiss it. So as you can see, updating our example three to work with the KV Lang. It works exactly the same. We have something that's a little bit easier to read and understand. It works just as it did when we were doing everything straight in Python. So

15:08 Mobile App Packaging

Derek Payton

we've gone through, we have put an app together and now we want to get it on our phone, right? So We've built a Kiwi app, we need to package it for mobile devices. The answer to that the question, how do we get from Kiwi app to mobile app On Android, Python for Android. So Python for Android is part of the Kiwi project. It does exactly what it what it says. It packages Python apps for Android. It does only run on Linux, which can be problematic. The Kiwi project does have a virtual box disk image. With everything needed to run it. So Windows developers, Mac developers there is a way forward. Similarly on the iOS side, there is Kivi

iOS. So it's a tool chain for compiling Python apps to run on iOS. Same problems, obviously. You are probably aware that you can only package iOS apps on a Mac. So you kind of run into the situation where you're you need the specific platforms to build for those devices. But you shouldn't use either of those. And the reason why is because if you use them specifically, now you have this whole build process for Android. and a whole bunch of custom stuff for that. And then this whole build process for iOS, a bunch of stuff for that. And it can get really unwieldy. So instead of using those directly, you should use builddozer. So builddozer is a tool for creating application packages.

It's another part of the Kiwi project. You can build for Android and iOS using a common spec file. So you define all of your configuration and attributes in one place and then go from there. It's really easy to install. Pip install builddozer. And then run builddozer init. Builddozer init gives you a default builddozer. spec file. And from what I've found out, in most cases, that seems to work just fine. I haven't run into any major issues with that. So the builddozer. spec file is basically just an INI file where you can configure the name of your application, the package domain, source directory, any other assets you want to bundle in. Also platform-specific configuration as well, Android permissions, API versions, so forth.

There's similar iOS specific options for Apple devices as well. So when we want to build on our phone, we've we've filled out our builddozer. spec file and now we run builddozer android debug. What that does is it goes through If you haven't already installed the Android SDK and the Android NDK and Python for Android, this command will do all of that for you in one go. So it's a lot to download. This can take quite a while to run the first time you run it. But once you've run it, once you've done it, now you have everything you need and it becomes a lot faster as you start iterating on your app and testing on your mobile device. So this command will package up your APK file and drop it into a bin

directory, which is nice, but you might want to run it on your phone to test. And so all you do is you tack on deploy run as an additional commands to that. And so deploy will then take the APK, use ADB, put it on your device, and then run will open it up. So you have one command to build your entire app, build the APK, put it on your phone, and open it up in just a couple of seconds. So building UIs is great. Phones these days have a lot of really neat hardware. You have accelerometers, gyroscopes, camera functionality, all phones vibrate. You want to do things with those.

19:05 Platform Hardware APIs

Derek Payton

How do you do hardware things? And so the example that we're going to look at is the accelerometer. Enter Pygenius. So Pygenius allows you to access Java classes through the Java native interface. And the way that you do this is that you for the accelerometer example, you have to build a Java class to interact with the Android sensor manager. There's a lot more code here than I can fit onto this slide. It's also Java. I don't know Java that well. So you have to build all this out in Java, create that bridge, and then on the Python side, import auto class, instantiate it, enable the accelerometer, and you can go

read from the accelerometer. Similarly, you have PyObjus for the iOS side. So PyObjus, similar to PyGenius, lets you do iOS or Objective-C things from within Python. So I'm not an Objective-C developer either. This is the example that's given. You have to use core motion on the iOS side. In order to interact with the accelerometer. And then the Python side looks somewhat similar to how you would do it on Android. Right, you import auto class,

in this case we're gonna instantiate the bridge start the accelerometer and we can start reading values from it. But we have another problem is that I don't want to write Java. I don't want to write Objective C. I just want to write some Python. And so again, we have another layer of abstraction that the Kivi project has provided called Plier. So it's platform-independent APIs for common hardware features. It supports a number of features on both Android and iOS. If you're not running on one of those platforms, say you're doing something on Linux or Windows, Mac OS, it will use available libraries on those platforms to kind of fill in the gaps. So if you have an accelerometer

on a desktop or you know whatever you know Linux thing that you're doing, you can still do hardware things with Plier through one simpler interface. And so now instead of having to write a bunch of Java and Objective C, you have six lines of Python with no platform dependent code. Which becomes really nice, right? I can just, cool, import the accelerometer, enable it, go. I didn't have to dip into any platform-dependent code. So that's kind of the gist of

22:08 CordWise Guitar App

Derek Payton

Kiwi. And I'm going really fast. Wow. So kind of showing off the one of the reasons why I started messing with Kivi is I really like playing guitar. I'm really bad at playing guitar, but I really like playing guitar. And as I was going through trying to learn, build up muscle memory, learn chords, there's this trick where you go from guitar chord to guitar chord, just kind of strumming chord to chord to chord. Trying to build up muscle memory. And so a Kiwi app that I put together is this thing called CordWise, which is just that.

So chord wise lets you choose guitar chords. This is all a kibble app. Choose what guitar chords you want to practice. You can select and deselect chords and then dumps you into practice mode, sending them at you in 4-4 time at 100 beats a minute. There is audio, there's a little metronome that plays as well, so you can kind of keep time. And This has been kind of helpful for my own playing. Trying to learn new chords and build up that muscle memory. And it's it's been a neat experience, right? I being able to build this, put it on my phone, I have it with me anywhere. There's a couple guitars at my office, I have my one at home.

23:41 Questions and Discussion

Derek Payton

And I can just pull up my phone and start practicing using this Kiwi app running on Android. So we can totally build mobile apps in Python. You totally should build mobile apps in Python. I would love to see the Python ecosystem. Grow in that regard. There's have been a lot of efforts to doing interesting things in Python, both on mobile apps, things like in the browser. I would love to see Python everywhere. I probably like the language a little too much. But a lot of that has to do with the community and all the support here. So that's kind of it. Questions so far?

Speaker 2

So once you've built your app, how do you get it to your phone?

Derek Payton

Yeah, so that takes us back to Builddozer, right? So all of all of that, getting your app onto your phone happens inside Builddozer. This command, specifically this command, will build your app in debug mode. It will deploy it to your phone, it'll put the APK on your phone. There's a similar command for iOS and then run it directly. So that's that's pretty much all that's needed. It's a single command to get it everything up and running on your device.

Speaker 3

Does KV require Python?

Derek Payton

Yes.

Speaker 3

Okay. And then another question is can you mix a match The Python and design designer widgets? Like if you write your own widgets in Python versus that designer app that you show, can you mix and match them? those widgets?

Derek Payton

Yeah, so you can you can obviously you know design your own widgets you know and you probably want to do that if you want to make things extension You don't want if you have a button with certain parameters, certain position, colors, and whatnot, you probably don't want to pass all of those options in to every time you instantiate that button. So of course you can you know through object inheritance define your own custom button and set those yourself and use those throughout your app.

Speaker 4

Okay, I guess next question would be in your plier example, I saw that you were using X range.

Derek Payton

So this example, let's see where's that example so yeah, so this code example is a little bit older. Kiwi itself does support Python 3. Builddozer and so specifically Python for Android, it does have Python 3 support sort of. I haven't been able to quite get it to work yet. You have to use a custom NDK. For that. So I would say if you specifically are targeting Android, for now you're probably stuck doing Python 2. If you can get that the Christax NDK to work, which I wasn't able to, then by all means use Python 3.

Speaker 4

Cool, thanks.

Speaker 5

So it looks like everything that you've shown primarily is oriented toward user interface type work. Does Kivi support background services and other non-UI-based applications?

Derek Payton

I haven't had a need for that. So I haven't really come across it. So the answer honestly is I don't know. I don't I'm I'm afraid I don't have a more a better answer than that. I haven't I haven't come across anything in Kiwi that you know is like background work so

Speaker 6

The material design demo you showed at the beginning, is that just how deep is that theme I presume it's a theme. How deep is that theme? Is it just sort of a coat of white and blue paint or is it actually changing behavior in any way.

Derek Payton

So what they've done is the QVMD project and I don't have a link in my slides. So I I'll update my slides online. But they've they've created a custom set of widgets, right? So instead of button, you have MD button that you use throughout your application. So with this, you can absolutely mix and match regular Kiwi widgets with material design widgets if you wanted to.

Speaker 6

So but they're actually material design widgets. They're not just Kiwi widgets painted.

Derek Payton

Yeah, so I'm not entirely certain about how they're set up kind of behind the scenes. I know that this looks pretty darn close to material design. And I'm not really sure what you mean by actual material design widgets because MD is more of a specification, right? Than here's a library to use in things.

Speaker 6

Would it be would I notice any difference whatsoever?

Derek Payton

I know in using material design for web stuff, this looks pretty close. From everything that I've seen.

Speaker 6

Okay. Related to that, accessibility, what's the accessibility story with Kiwi Widgets?

Derek Payton

I would imagine there isn't a very strong accessibility story there simply because it is implementing its own UI layer. It's a canvas and it's just kind of drawing onto the screen. None of it is native, so I don't know how well you can tap into accessibility features on the platform that you're building on.

Speaker 6

Sure. Thanks.

Derek Payton

So yep.

Speaker 7

My question is about push notifications. Like I use Firebase for push notifications in native Android and iOS. Is that anyhow subordinate?

Derek Payton

Yeah, so push notifications, again, I'm not I'm not super familiar with how they're implemented on native Android and iOS. I know that in my experience like at work we had to go through and like create a Firebase account and do all this weird stuff. I don't think that there's anything that would stop you from doing that. Again, because we have Pyginius and we have PyObjust You can hook into any Objective-C libraries and call classes and whatnot with that. So I would imagine it's doable. There isn't anything out of the box in Kivi that provides that purely from the Python side.

Speaker 8

Yeah, so I was wondering what's going on under the hood. Is it packaging the standard Python interpreter into the application packages. Is it running on Jython under Android? Yeah.

Derek Payton

So more or less it's packaging, my understanding is that it's packaging CPython. Right. And I kind of want to I should have qualified this in the beginning. I am by no means a Kivi expert, right? I occasionally want to do mobile app development, I would love to do Python with that. And so this is kind of what I've discovered along the way is I've done a couple things with Kivi. So again, my understanding is that yes, I believe it is packaging regular CPython up to run on Android. Again, I'm I'm a little light on specifics of how it's doing that.

Speaker 3

regarding plier, you said that's gives you access to the hardware. What kind of hardware? You showed one with the accelerometer, but. Camera or I don't know what we're doing.

Derek Payton

kind of see what's available on what platform. So Android is let me zoom in here. So Android is pretty well covered in terms of what you can access. IOS a little bit less so and then it gets you know less is as you go down into desktop operating systems. But there's a lot here that you can do things like the proximity sensor on Android Sending SMS messages on both iOS and Android. Again, Android is much more fleshed out in terms of what plier supports than iOS, but there's a lot here that you can work with. with.

Speaker 8

Hey, thank you. Really good talk. I just had a question. What are some like good apps that have been made with Plier that you know about alongside your cord?

Derek Payton

Sure, yeah. So there was, and I'm I'm a little bit bummed, I went I originally had a slide in my talk saying if you get bored, go download. Does anybody remember 2048? Threes when that was all the rage. So there was a really cool implementation of that built-in KV that was on the App Store and in preparing for this talk the other day I went to go you know get it and add it as my slide and it's not on the App Store anymore. Which is super unfortunate because that would have been really cool. But I mean searching for Kiwi in the App Store will yield some results as well. They have a showcase on their website kind of showing what's been done with it. I've seen screenshots from several different games. There

is a an event library called Kivent which is great for you know event handling and it has been used to make some games in kiwi.

Speaker 2

Well in that case thank you derek for a great talk let's give derek a big hand