Sitemap

Flutter apps in Python — Flet

3 min readDec 10, 2022

--

Flet is a framework that allows building interactive multi-user web, desktop and mobile applications in your favorite language. You build a UI for your program with Flet controls which are based on Flutter by Google. Flet does not just “wrap” Flutter widgets, but adds its own “opinion” by combining smaller widgets, hiding complexities, implementing UI best-practices, applying reasonable defaults. At the moment you can write Flet apps in Python. — flet

Press enter or click to view image in full size
https://flet.dev/

Flet module

pip install flet

Installation specific to Linux and WSL.

After installing the module we could simply import flet and start building our App.

Getting started

Hello world

The simple Hello World! app would look like,

import flet as ft


def main(page: ft.Page):
page.title = "Hello world App"
page.add(ft.Text("Hello, Flet World!"))


ft.app(target=main)

The app will be started in a native OS window.

Windows app

If you want to run the app as a web app, then

ft.app(target=main, view=ft.WEB_BROWSER)

Controls

User interface is made of Controls (aka widgets). To make controls visible to a user they must be added to a Page or inside other controls. Page is the top-most control. Nesting controls into each other could be represented as a tree with Page as a root.

Controls are just regular Python classes. Create control instances via constructors with parameters matching their properties. — flet controls

Controls in our Hello world code

t = ft.Text("Hello World!")

To display the controls on the page that we defined, we need to append it to the Page controls and update the page.

page.controls.append(t)
page.update()

But luckily we have page.add() which does the same.

page.add(t)

Whenever we made change to the controls, we need to use page.update() method to see the changes in the UI.

Get Logesh’s stories in your inbox

Join Medium for free to get updates from this writer.

Example of changing the value of the text.

import flet as ft
from time import sleep


def main(page: ft.Page):
t = ft.Text()
page.add(t) # it's a shortcut for page.controls.append(t) and then page.update()

for i in range(10):
t.value = f"Step {i}"
page.update() # updating the change
sleep(1)


ft.app(target=main)
Step incremented from 0 to 9

Whatif the page.update() is removed, simply the change that we made to the text won’t be reflected in the UI.

Press enter or click to view image in full size
UI when the page.update() is removed

Flet almost has all the Flutter widgets in the name of Controls.

Next step

So far we have seen the introduction to Flutter Apps in Python using Flet framework, as a next step I will attach the links to the Flet docs to get to know more about Flet.

At the time of writing this article, the Flet doesn’t supports standalone Mobile apps. It uses Server-driven UI (SDUI).

Hope this article introduces you with the Flet.

Happie Flet!!!

--

--

Towards Dev
Towards Dev

Published in Towards Dev

A publication for sharing projects, ideas, codes, and new theories.

Logesh
Logesh

Written by Logesh

software_engineer@wireless_and_iot_domain