Setting Up a New Flet Project
To create a new Flet application, start by setting up a new directory. If you are using poetry
or uv
, ensure that your directory contains a pyproject.toml
file.
Run the following command to create a minimal Flet app:
flet create
This command generates the following directory structure:
├── README.md
├── pyproject.toml
├── src
│ ├── assets
│ │ └── icon.png
│ └── main.py
└── storage
├── data
└── temp
Important Notes
-
If you initialized your project with
uv init
orpoetry init
, the originalpyproject.toml
file will be replaced with the one from the Flet app template. -
If you encounter the error:
Error creating the project from a template: 'git' is not installed.
, install Git from git-scm.com/downloads. Verify the installation by running:git
Note: Git is different from GitHub CLI, which cannot be used as a substitute for Flet.
-
The
src/main.py
file contains the main logic of your Flet application. Themain()
function defines the UI elements and runs the app usingft.app()
.
For more details on the flet create
command, visit the official documentation.
Running a Flet App
A Flet application can be executed as either a desktop or web app using the flet run
command.
Running as a Desktop App
To run your Flet app as a desktop application, use:
flet run
This command executes main.py
in the current directory.
If your script is located elsewhere, specify its path:
flet run [script]
For example:
flet run /Users/JohnSmith/Documents/projects/flet-app
If the script file is named something other than main.py
, specify the filename explicitly:
flet run counter.py
The app will launch in a native OS window on macOS or Windows.
Running as a Web App
To run your Flet app in a web browser, use:
flet run --web [script]
A new browser window/tab will open with the app running on a random TCP port.
To specify a fixed port, use:
flet run --web --port 8000 app.py
Enabling Hot Reload
By default, Flet watches the script file for changes and reloads the app when saved. However, it does not monitor other files.
To watch all files in the same directory:
poetry run flet run -d [script]
To watch the script directory and all subdirectories recursively:
poetry run flet run -d -r [script]
For more details on the flet run
command, refer to the official documentation.
Copyright Statement: Unless stated otherwise, all articles are original to this site, please credit the source when sharing.
Article link:http://pybeginners.com/python-flet/creating-a-new-flet-app/
License Agreement:Attribution-NonCommercial 4.0 International License