Python has been a tremendously popular language for data analysis and data science. There is a good reason for this popularity. Python is simple and powerful. It’s easy to learn and it’s easy to use. But computing has changed over the last 10 years. We’ve seen the emergence of a whole new class of computing devices like mobile phones and tablets and watches where you can’t use Python out of the box. So how do you take a powerful language like Python and use that to build applications that work on these new computing devices and improve the experience for the old bold platforms that we know and love? 

BeeWare is one such tool that helps you do that. It aims to make using Python on these new platforms as simple for native application development as Django makes web development something that’s accessible to newcomers but powerful enough for heavy lifting purposes. In this tutorial, we’ll show you how you can build a Python application, which can run on multiple operating systems including Android and iPhones from a single codebase.

What is BeeWare?

BeeWare is open-source; a collection of tools that allow you to write system native Python apps from your computer. It is cross-platform to the extent possible, meaning that it should allow you to write code that runs unmodified on every platform you throw it at. BeeWare products should feel exactly like native products do when they’re running on their respective platforms and are installed like any other product based upon the operating system available (with official installers created by you). 

Installation and Setup

BeeWare is Python native if you need to install something it should be pip installable and any installation processes above and beyond pip install should leverage existing packaging metadata such as what’s provided by a disk utility and setup tools. 

Note: While BeeWare is a cell set of well-integrated tools it isn’t a monolith, you don’t install BeeWare as a whole but a series of small well-defined tools that play well together.

First, create a virtual environment and activate it in your desired folder:

mkdir beeware
cd beeware
python3 -m venv tutorial-env
tutorial-env\Scripts\activate.bat

Then in this environment, we will install a library called briefcase it’s a BeeWare tool that can be used to package applications for distribution for the end-user.

pip install briefcase

To run the project use the following command

Briefcase run

After running this it will ask you some questions, answer them or use the default settings and you should get a directory like shown below.

.
├── LICENSE
├── pyproject.toml
├── README.rst
└── src
└── new_app
    ├── app.py
    ├── __init__.py
    ├── __main__.py
    └── resources
        ├── new_app.icns
        ├── new_app.ico
        ├── new_app.png
        └── __init__.py

This skeleton is a fully functioning application without adding anything else. We’re using the skeleton from our example above (and expanding on it) as a base for our app. We’re going to chop up this project into smaller chunks for use in the next section where we introduce the concepts of separate directories and files.

First, let’s run this application with the following command:

#first cd into the folder
cd new_app

#then run in a development mode
briefcase dev

Output

Adding functions in the app:

First, go to the app.py and you will see the default code we will write in that to add some functionality like label input box and buttons using toga it’s a toolkit of BeeWare.

"""
hello this is my first application
"""
import toga
from toga.style import Pack
from toga.style.pack import COLUMN, ROW


class new_app(toga.App):

    def startup(self):

        # main box
        main_box = toga.Box(style=Pack(direction=COLUMN))
        # creating a label
        name_label = toga.Label("Enter Text : ",style=Pack(padding=(0,5)))
        self.name_input = toga.TextInput(style=Pack(flex=1))
        name_box = toga.Box(style=Pack(direction=ROW,padding=5))

        # adding label to this box
        name_box.add(name_label)
        name_box.add(self.name_input)

        # creaing a one click button
        button = toga.Button("Submit",on_press=self.say_hello,style=Pack(padding=5))

        # adding name box and button in the box
        main_box.add(name_box)
        main_box.add(button)

        self.main_window = toga.MainWindow(title=self.formal_name)
        self.main_window.content = main_box
        self.main_window.show()

    def say_hello(self,widget):
        print("hello",self.name_input.value)

 
def main():
    return new_app()

Code explanation

  1. Import dependencies.
  2. Create an app.
  3. Creating a main box and label of the app.
  4. Then add the label to the box.
  5. After that create a one-click button and add them to the box.
  6. Define the function of the text input box.

Create an app to run it on Windows and Linux:

Note: The setup we are creating here is platform-specific meaning that if you create it on windows it will create a setup for windows and if it’s on Linux it will create it for Linux.

Run the following command in your terminal in the new_app folder

Briefcase create

After running the command you will see that the new folder has been created with the name windows. This folder contains configuration files. Then check the application is running or not by running the following command

Briefcase run

Now let’s create the setup file by running the following command

Briefcase package

That’s is now you can see that a new file is created in the windows folder with the name new_app-0.0.1. You can install it and run it like normal software.

Create an app to run it on android:

First, run the following command in the terminal

Briefcase create android

This will create a new folder with the name android, and inside this folder, all the android app-related files will be configured. This process will take a little bit of time because it will install some big libraries so wait patiently.

And that’s it you can now see a new .apk file in the android folder you can install it on your android device like a normal android app.

Or you can also use the following command to run it on windows as well using an emulator.

Final Words

In this blog post, we’ll walk through how to write a cross-platform application in Python, using the BeeWare open-source library. We created applications that support multiple platforms, such as Windows, OS X, and Linux. It’s very easy but you need to know about a python library called toga so you can customize and add functions to the app. If you have any questions regarding the process feel free to comment down below.

Here are some useful tutorials that you can read: