In this blog: Learn how to create a simple App for iOS using Python frameworks. Download our “Mobile Development” build to get a version of Python 3.7 complete with the mobile development frameworks explained in this post.
The Python ecosystem has a vast array of libraries that cover almost all aspects of software development. What is particularly interesting is that some of those libraries also include tools for compiling Python into native code for specific mobile platforms such as iOS, and Android. Yes, you heard that right! It is possible to use Python to create native mobile applications.
Why Python for mobile development? Python is well known for its readability and ease of use, which can translate into faster development – a huge benefit in the mobile market where speed to market is critical to gaining and keeping market share. On the other hand, neither Android nor iOS support interpreted languages, which means they can’t natively run Python apps. That’s where a number of frameworks bridge the gap to allow Python apps to be run on mobile devices with a native look and feel.
In this tutorial, we will:
- Identify the most popular frameworks for cross-platform mobile development in Python
- Learn how to create a simple App for iOS using Python frameworks
- Understand the pros and cons of using a Python framework versus native iOS or Android tooling
Let’s get started.
Cross-Platform Mobile Frameworks for Python
When it comes to developing cross-platform mobile applications that support both Android and iOS platforms using Python, there are two main choices: Kivy and BeeWare.
Kivy promotes itself as an open source Python library for rapid development of cross-platform UI applications. It has a graphics engine that is built over OpenGL, so it can handle GPU-bound workloads when necessary. It also has a python-to-android project that lets you port Python applications to Android. It has a similar toolkit for iOS, although packages for iOS can only be generated with Python 2.7 at the moment.
BeeWare is another popular set of tools that let you write applications in Python and cross-compile them for deployment on several operating systems, including macOS, Windows and Linux GTK, as well as mobile platforms like Android and iOS.
For instance, there is a tool called briefcase that lets you create project boilerplates for specific native applications. Rubicon-C and Rubicon-Java also provide bridge interfaces for using Python to call Objective-C and Java bindings, respectively.
BeeWare’s main repository seems to be updated regularly, and they also provide commercial support that includes training, consulting and technical support.
Python Mobile Application with BeeWare
To try out the BeeWare toolkit, let’s create a basic mobile application that does something simple like selecting today’s cafeteria special chosen randomly from a pre-populated list.
First, we begin by creating a new python3 development environment:
$ python3 -m venv .env
Then, we install the briefcase tool that will help us create the project boilerplate code:
$ pip3 install briefcase
Next, we use the briefcase tool to create a new project:
$ briefcase new
Briefcase provides an interactive way to help you create your project by prompting you for key information:
Formal Name [Hello World]: FoodTracker App Name [foodtracker]: Bundle Identifier [com.example]: com.thdespou.foodtracker Project Name [FoodTracker]: Author [Jane Developer]: <Your Name> Author's Email: <Your Email> Application URL: Select one of the following:  Toga  PySide2  PursuedPyBear  None GUI Framework : 1
On completion, you can run the project that was generated in order to see the native app (Mac or Windows) representation. In this case, an empty modal window is displayed:
$ cd foodTracker $ briefcase dev
Since we want our app to run on an iOS device, we need to use the following command inside the project:
$ briefcase create iOS
At this point, we need to install Apple’s application development tools (Xcode) in order to proceed.
Once Xcode is in place, we can edit the file src/foodtracker/app.py and replace the boilerplate with code that implements our “daily special” app:
""" Keep my favourite meals """ import toga import random from toga.style import Pack from toga.style.pack import COLUMN, ROW, LEFT class FoodTracker(toga.App): recipes = ["Baked chocolate orange cheesecake", "Baked Brie", "Chicken Tikka Masala", "Grilled Chicken Salad"] def startup(self): """ Construct and show the Toga application. Usually, you would add your application to a main content box. We then create a main window (with a name matching the app), and show the main window. """ main_box = toga.Box() message_box = toga.Box() message_input = toga.TextInput(readonly=True) message_label = toga.Label('Todays special: ', style=Pack(text_align=LEFT)) def button_handler(widget): message_input.value = random.choice(self.recipes) button = toga.Button('Get Recipe', on_press=button_handler) button.style.padding = 50 button.style.flex = 1 message_box.add(message_label) message_box.add(message_input) main_box.add(button) main_box.add(message_box) main_box.style.update(direction=COLUMN, padding=20) message_input.style.update(flex=1) message_label.style.update(width=150) self.main_window = toga.MainWindow(title=self.formal_name) self.main_window.content = main_box self.main_window.show() def main(): return FoodTracker()
As you can see, imperative-style code is used to construct the window elements. We added a main box element to hold all of the child elements. Using composition, we added a button and another message box that holds a readonly text field. When a user clicks on the button, it will display a random recipe in the text field.
Finally, we define the main window and its properties in the last 3 lines of the startup method.
Now we can build our project:
$ briefcase build iOS
To see what our application looks like on our desktop, we can use Xcode to open the project and run it in an iOS emulator. We just need to tell it to use the following folder:
Here is what we will see when we run the emulator:
Note that every time we change our code, we will need to run the following commands to see the changes in the emulator:
$ briefcase update iOS $ briefcase build iOS $ briefcase run iOS
Python Mobile Tooling vs Native Tooling
Now that we have seen some examples of creating and building simple mobile apps with Python, a really important question arises:
Why should you invest in writing Python mobile applications instead of using standard mobile languages like Java or Swift?
To answer that, we need to discuss the pros and cons of using Python for mobile development.
- The Power of Python: We can leverage the power of Python and its ecosystem with the Kivi and BeeWare frameworks. This proves to be a considerable advantage if you and your development team are already familiar with Python, and just need to port your existing applications to mobile platforms.
- Cross-Platform Support: This means that we write our application once and then compile it for different platforms, promoting reusability.
- Slower Compilation Time: It seems that the development loop is longer than usual due to the need to run extra tools when changing something in our code. However, this could be improved in the future with tooling integrations or automation scripts.
- Less Supported Features: Google and Apple will always promote their native tooling first over alternatives. Users of non-native frameworks will be limited by whatever the most recent version of (for example) Kivy/BeeWare have implemented. You will need to be on top of the current issue list and the limitations of the current version in order to resolve any issues you’re seeing in a prompt manner.
- Inconsistency: As not all features are supported, there will be inconsistencies between different platforms. For example, you might have a layout that displays differently in Android versus iOS. Thus, you may have to implement many workarounds to make the designs consistent.
In general, using imperative code to describe user interfaces requires more effort. Those workflows are typically easier to implement within Apple’s Xcode or Android Studio’s built-in editors.
However, there are significant code reuse advantages in going with Python for a job like this, and there are plenty of applications written in Python that you can use as examples.
If your organization is considering writing applications in Python and cross-compiling them into native platforms like iOS or Android, you should evaluate the pros and cons carefully before proceeding.
To get started with the mobile development frameworks in this post, create a free ActiveState Platform account and then download our “Mobile Development” build. The build contains a version of Python 3.7 and the frameworks in this post so you can test them out for yourself.
NOTE: the simplest way to install the environment is to first install the ActiveState Platform’s command line interface (CLI), the State Tool.
- If you’re on Linux or Mac, you can use curl to install the State Tool:
sh <(curl -q https://platform.activestate.com/dl/cli/install.sh)
Once the State Tool is installed, just run the following command to download the build and automatically install it into a virtual environment:
state activate Pizza-Team/Mobile-Development
Frequently Asked Questions
While web applications built with Python frameworks like Django and Flask will run on Android and iOS, if you want to create a native application you will need to use a Python mobile app framework like Kivy or BeeWare.
Download the pre-built Mobile Development Python environment, which includes both BeeWare and Kivy.
Python is well known for its readability and ease of use, which can translate into faster development – a huge benefit in the mobile market where speed to market is critical.
While Android and iOS will not run interpreted code, you can use a Python mobile app framework like Kivy or BeeWare to write your mobile application in Python and then cross-compile it for Android or iOS.
If you only want to run on Android or iOS, you’re better off using their native tools to create your mobile app. However, if you want to run on both Android and iOS, you should consider using Python to develop a cross-platform mobile app. This way, you need only write your app once, and then cross-compile it to run on Android and iOS.
Both Kivy and BeeWare are excellent choices for a Python mobile app framework. However, BeeWare is updated more frequently, provides commercial support, and offers a native UI toolkit.
Get started with the Mobile Development Python environment, which includes both BeeWare and Kivy.