Skip to content

Beeware: Write Apps with Python!

beeware-python

Writing apps is not a Python strength and there are not many tools doing this. For this reason, you don’t find desktop/mobile apps written in Python very frequently. Looking something that helps us to write apps, I find two tools that have this mission called Kivy and BeeWare. Today, we are going to talk about Beeware, that is totally Open Source.

The official documentation says that BeeWare allows you to write your app in Python and release it on multiple platforms. No need to rewrite the app in multiple programming languages. It means no issues with build tools, environments, compatibility, etc.

Beeware has a suite of tools and altogether let you create apps in different platforms, all the tools are open source with BSD license. You can write native apps for Android, iOS, desktop and cross-platform.

Let’s take a look at some of their mains tools:

Briefcase: Briefcase is a tool for converting a Python project into a standalone native application. You can run the project and can get a MSI installer for Windows,  an application bundle for Mac, or you can create an Android/IOS app and launch it in a simulator or device.

Toga: the Beeware page describes Toga as native system widgets, not themes. When you see a Toga app running, it doesn’t just look like a native app: it is a native app. Toga is available on Mac OS, Windows, Linux (GTK), and mobile platforms such as Android and iOS. Later in this post, you will see an easy example of using Toga

Batavia: Batavia is an implementation of the Python virtual machine, written in JavaScript. With Batavia, you can run Python bytecode in your browser. Batavia provides the option for writing client-side logic in Python, rather than JavaScript.

Cricket: Cricket is a graphical tool that helps you run your test suites.

For those tools, there are some Cookiecutter templates to be able to get started quickly.

Now, we are going to write an easy desktop app with Toga. It is just an input, a label, and a button, the user writes its name and then click the button and the label say hello to you!

As usual when working with Python, we need to start with creating a virtual environment:

mkvirtualenv beeware-test -p $(which python3)

Then install toga:

pip install --pre toga

pre : this option Include pre-release and development versions. By default, pip only finds stable versions.

Create the python file salute.py and add the code:

import toga
from toga.style.pack import *


def build(app):
   box = toga.Box()
   name_label = toga.Label('Name:', style=Pack(text_align=LEFT))
   name_input = toga.TextInput()
   salute_label = toga.Label("", style=Pack(text_align=LEFT))

   def button_handler(widget):
       salute_label.text = "Hello {}!".format(name_input.value)
  
   button = toga.Button('Salute', on_press=button_handler)
   button.style.padding = 20
   button.style.flex = 1
   name_label.style.update(width=100, padding_left=10)
   name_input.style.update(width=100, padding_top=10, padding_left=10)
   salute_label.style.update(width=100, padding_top=10, padding_left=10)

   box.add(name_label)
   box.add(name_input)
   box.add(salute_label)
   box.add(button)

   box.style.update(direction=COLUMN, width=100, padding_top=10)
   return box


def main():
   return toga.App('First App', 'org.pybee.salute', startup=build)


if __name__ == '__main__':
   main().main_loop()

There are 4 main highlights for this code:

  • toga.Box(): A box is an object that can be used to hold multiple widgets, and to define padding around widgets.
  • add(): The add function of the box, will append each widget in the final view.
  • return box: This box will contain all the UI content
  • main(): It instantiates the app

For more details visit Toga site. Finally to run your app:

python -m salute

It is great to know that with Python we can create our own apps. As you see in the example, few lines of code can set up and launch an app, with a few more of effort, you can configure to use it for Android or iOS. Now, keep in mind that Beeware maturity is low, don’t use it for critical things, it is not ready for mission critical applications yet, it is a good option for college projects or for contributing to open source, there are a lot of things to improve and you can help them.