Skip to content

Building multi‐platform apps in Python powered by Flutter

InesaFitsner edited this page Apr 11, 2024 · 12 revisions

Flet allows Python developers to easily implement awesome user interface in their apps and package them to run on web, desktop, and mobile devices - all with a single code base.

Why another UI library for Python? Unlike other UI frameworks for Python, Flet does not implement UI widgets from scratch or wrap native platform controls; instead, it relies on the Flutter framework by Google. Initially born as a mobile framework, Flutter recently received a serious push from Google with added support for web and desktops.

Flet brings the power of Flutter to Python developers - for free. UI developed with Flet looks aesthetically pleasant; it’s adaptive, responsive, localizable, and accessible. Flet provides more than 100 controls and can be easily extended with a wide array of third-party packages and plugins provided by the vast Flutter ecosystem.

How easy is it to use Flet?

Flet UI is built with stateful controls that fire events triggered by user interaction. In event handlers, the developer updates control properties, thus mutating the user interface.

Here is a simple Flet program that prompts the user for a name and outputs the greeting:

import flet as ft

def main(page: ft.Page):

    def btn_click(e):
        page.clean()
        page.add(ft.Text(f"Hello, {txt_name.value}!"))

    txt_name = ft.TextField(label="Your name")

    page.add(txt_name, ft.ElevatedButton("Say hello!", on_click=btn_click))

ft.app(main)

To run the code above as a desktop app:

flet run app.py

To run the same app as a web app:'

flet run --web app.py

To test the app on iOS or Android, there is an “Flet” app available in both the App Store and Google Play that can be installed on a user’s device.

Here is an example of adaptive Flet UI that uses iOS style on iPhone and Material design language on Android:

How does Flet work?

The Flet app consists of "backend" and "frontend" parts. The backend is a Python runtime running the user app, and the frontend is a Flutter app rendering the user interface. Frontend and backend communicate via the JSON protocol. Every change to app controls is encoded into JSON and transmitted to a frontend. User interactions on a frontend are transmitted as control events to the backend.

For desktop and mobile, both backend and frontend are parts of the same app (an embedded Python runtime inside a Flutter host). For the web, the Flutter frontend runs in the browser and communicates with the Python backend on the server via WebSockets.

Flet not only adds an awesome UI to a Python app, but it also solves the problem of packaging a Python app, along with all its dependencies and Python runtime, into a standalone app bundle that can be delivered to an end user and run in desktop, mobile, or web environments. Running Python on mobile devices is the biggest challenge, mainly because of 3rd-party Python packages written in C, Rust, or other compiled languages. Flet collaborates with companies like Kivy and Beeware, leveraging their multi-year experience of running Python on mobile devices. The acceptance of PEP 730 and PEP 738 will soon add iOS and Android as Python-supported platforms, further extending the reach of Python on mobile platforms.