Python mobile application with kivy / buildozer. Lecture in Yandex

    Not the fact that you need to write a serious Python application. But quickly assemble a working service in order to “sell” it to a customer - why not? Python is universal, and the experience of creating mobile software in this language can be useful. Vladislav Shashkov from Sberbank spoke about how development is being developed using the kivy framework.


    - Good afternoon. My name is Vladislav Shashkov, I work at Sberbank and, in fact, I'm a grocery store, not a developer. This may be of interest to my report, because it will clearly show that making a mobile application in Python is quite simple.

    Kivy - after all, he is “Kiwi”, because he is figuratively depicted on the covers of books in the form of fruit, and the pronunciation “Kiwi” will be closer in Russian. The framework has been developing since 2011. In fact, this is a graphical UI library for creating cross-platform applications, not only on mobile platforms. Its feature is the KV language, it is a markup language. If you figuratively try to describe what the KV language is, it turns out that HTML would look like if it were written in Python.

    We will also talk about the KivyMD library, which is a set of widgets in the style of Material Design. KivyMD allows you to create a friendly google interface with which you can work on user experience.



    Here the video shows how all the widgets that are in the library are scrolled. As you can see, this is a fairly rich set of elements. This bookmarks, and buttons, and progress bars, and pop-ups. Basically, there is everything you need to realize the customer experience you need. If you need to assemble the assembly on iOS, then it will look exactly the same. The same Material Design. In principle, such an application can be published on the App Store, although it will be similar to Google.

    Kivy without KivyMD has a very austere design. It should not even be shown to customers. Similar to Ubunt simple buttons, simple dialogs not adapted to mobile experience, to mobile users. And KivyMD well done, everything is beautifully done.

    There is some bad news. Executable application packages are assembled by a special utility - buildozer. And the bad news is that starting to use, making the first assembly with it, is not at all easy. As, probably, with any open source software, in order for everything to work out, in order to eventually reach the goal and get the APK, you need to go through a certain “path” without turning to the sides.

    This slide describes in detail what needs to be done, how to go along the “path” and not do too much. No need to try to update kivy itself or some other libraries in the image. All that is needed is indicated on the slide, the rest will catch up and work.

    Next I’ll talk a little about the structure of the Kivy project. On the slides there are QR codes where the links to GitHub are encoded so that you can read them now and directly trace the examples that I will talk about. ( The first link is approx. Ed.)



    The buildozer spec file is presented on the screen, this is the specification for the project. The most important thing here: there is a line that lists requirements, that is, libraries that are necessary for the assembly, and you need to specify Kivy, KivyMD in it, there is a record there, and it is also available on GitHub.

    An important section, it is commented out, concerns Android NDK, SDK, you do not need to touch it. The default values ​​will work.



    A bit about UI and UX. There are two concepts in the KV markup language: widgets and layouts. Widgets - visible elements, buttons, input fields and the like. Widgets receive events and can handle them. And layouts are objects that allow widgets to be positioned; there are several types of them to arrange widgets in a row or vice versa, to position them somehow arbitrarily. In general, this approach is standard for visual UI, UX, and it is also used in other libraries.



    Here is an example of the KV language ( link- approx. Ed.). As we see, the libraries are first imported behind bars, then in angle brackets ... It is wrong to say “root”, just the initial layout, to which the class will be mapped in the code. One to one names must match. Thus, the markup and the class are compared in the code. Further, indented widgets go through indents, inside widgets, property and events go indentation again, they go on the same list. Here is a sample code.



    At the top of the class is the layout class. Next is the application class, and at the bottom there are two lines for loading the markup constant, and launching the application. I know that the audience is probably not very familiar with mobile development, so I’ll tell you about the next steps, what exactly needs to be done on the phone to launch and debug your APK.

    The first step is to turn on the phone and decide the developer. He turns on quite cunningly. Usually this is a secret sequence of pressing menu items in the phone settings mode. The presentation shows one of the common sequences, but it may not work on your phone. After completing this sequence of actions, a mode will appear for the developer, and you can already install the APK, debug it and continue to act.



    Buildozer runs in a virtual machine, the virtual machine needs to be updated to make it fresher. After that, integration with USB will appear, then it will be possible to connect the phone to a virtual machine.

    A sign that you have a connection - the phone will ask for permission to debug. The project directory is created in the / BUILD folder. To build a project, you need to run the buildozer android debug command in the project directory. Installing the APK on the phone is performed by the utility from Android Studio, which is already in the image, by the adb install command.



    And the phrase success says that so far so good, the download to the phone has been completed.



    Code debugging is performed as follows. It is necessary to start collecting debugging information with the adb logcat command so that the collector works in parallel with the application. Your application after the first launch “with a high degree of probability” will crash, after which you can stop debugging and go read the log file.

    How to search for an error message? By the python keyword. The most recent entry with the python keyword will usually tell you what went wrong.



    About what can be done on the phone. The mobile platform allows you to make not just buttons that go to the API, but to work with a wide variety of gadget sensors. To work with mobile peripherals there is a standard plyer library. It is platform-independent, that is, everything that is written on it will be rebuilt from Android to iOS. The slide lists all available options. There GPS and battery and camera ... But with the camera plyer allows you to take only a static picture.



    And the following example expands this opportunity. GitHub Link, an example that allows you to receive a video stream in an application. I had a business task to recognize the QR code from the image. Also, this example can work with video recognition, augmented reality, etc.



    Also, kivy has a space for custom add-ons - Garden. Anyone can do widgets and the like. A useful widget that I used is working with maps. He is active, you can scale, spin the map. Here is an example of how to connect it. There are many other widgets in Garden.



    When I was working on my project, I conducted corridor testing with colleagues to collect feedback on the use of the application. And he received such wonderful objections as “I won’t put the left APK,” “Mom doesn’t allow it,” “I just bought a phone.” In order to continue testing the customer experience in essence, I had to go all the way to create a mobile application, namely to release on Google Play. On the slide it is described. The main points are that you need to tinker with the keys, and publishing on Google Play is not free. To register as a developer, you need to prepare $ 25. And this is still very democratic, because Apple is asking for $ 99.



    The link on this slidefor an example application, you can see my project. This is still an alpha prototype, so please do not judge strictly if the application crashes. The link to the current documentation is fresh, it seems to me that it is good. There is a community, the VKontakte group , its admin on Habré periodically releases articles.

    Also popular now: