-
Notifications
You must be signed in to change notification settings - Fork 548
Building multi‐platform apps in Python powered by Flutter
Flet allows Python developers to easily add awesome user interface to 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, but, instead, it relies on Flutter framework by Google. Initially born as a mobile framework, Flutter recently got 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 could be easily extended with a wide array of 3rd-party packages and plugins provided by a vast Flutter ecosystem.
How easy to use Flet?
Flet UI is built of stateful controls which fire events triggered by user interaction. In event handlers developer updates control properties thus mutating user interface.
Here is a simple Flet program which prompts 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 "Flet" app in both App Store and Google Play that could be installed on a user 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?
Flet app consists of "backend" and "frontend" parts. Backend is a Python runtime running user app and frontend is a Flutter app rendering the user interface. Frontend and backend are communicating via 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 backend.
For desktop and mobile both backend and frontend are parts of the same app (embedded Python runtime inside a Flutter host) and for web Flutter frontend is running in the browser and communicates with Python backend on the server via WebSockets.
Flet does not only adds an awesome UI to a Python app, but it also solves the problem of packaging Python app, all its dependencies and Python runtime into a standalone app bundle that will be delivered to an end user and run in desktop, mobile or web environment. Running Python on mobile devices is the biggest challenge, mainly because of 3rd-party Python packages written in C, Rust or other compiled language. Flet collaborates with companies like Kivy and Beeware bringing their multi-year experience of running Python on mobile devices. Acceptance of (PEP 730)[https://peps.python.org/pep-0730/] and PEP 738 will soon add iOS and Android as Python supported platforms and further extend the reach of Python on mobile platforms.