Overview
Flet provides two main functions for running applications:run() (recommended) and the deprecated app(). These functions initialize the Flet runtime, create a session, and execute your application entry point.
Source: flet.app
run()
Runs the Flet app.Source:
app.py:60Parameters
Application entry point. Handler (function or coroutine) must have 1 parameter of instance
Page.Type: Callable[[Page], Union[Any, Awaitable[Any]]]Source: app.py:61Called after
Page is created but before main.Source: app.py:62Useful for setup tasks like authentication or configuration.Page/app name used in web URL path when applicable.Source:
app.py:63Environment Override: FLET_WEB_APP_PATHHost/IP to bind the web server to.Source:
app.py:64Environment Override: FLET_SERVER_IPCommon Values:Noneor"": Defaults to127.0.0.1"0.0.0.0": Bind to all interfaces"localhost": Local only
TCP port to bind. If
0, an available port is chosen when needed.Source: app.py:65Environment Override: FLET_SERVER_PORTDefault Ports:- Desktop app: Random free port
- Web server mode: 8000 (if forced)
Preferred app presentation mode.Source:
app.py:66Values:AppView.FLET_APP: Native desktop windowAppView.FLET_APP_HIDDEN: Hidden window (background app)AppView.FLET_APP_WEB: Desktop window with web renderingAppView.WEB_BROWSER: Open in web browser
FLET_FORCE_WEB_SERVER=true forces WEB_BROWSERA path to app’s assets directory.Source:
app.py:67Environment Override: FLET_ASSETS_DIRCan be:- Relative path (resolved from script directory)
- Absolute path
Noneto disable assets
A path to a directory with uploaded files, or where uploaded files should be saved.Source:
app.py:68Can be:- Relative path (resolved from script directory)
- Absolute path
Noneto disable uploads
The type of web renderer to use.Source:
app.py:69Values:WebRenderer.AUTO: Automatically choose best rendererWebRenderer.HTML: HTML renderer (smaller bundle)WebRenderer.CANVASKIT: CanvasKit renderer (better performance)
The strategy to use for generating URLs.Source:
app.py:70Values:RouteUrlStrategy.PATH: Use path-based routing (/page1)RouteUrlStrategy.HASH: Use hash-based routing (#/page1)
Whether not to load CanvasKit, Pyodide, or fonts from CDN.Source:
app.py:71When True, all resources are served from the local server.If
True, returns a configured ASGI app instead of running an event loop.Source: app.py:72Useful for integrating Flet with existing web frameworks.Returns
When
export_asgi_app=True, returns a FastAPI ASGI app. Otherwise, runs the app and returns None.Source: app.py:96Environment Variables
The following environment variables override parameters:| Variable | Overrides | Description |
|---|---|---|
FLET_WEB_APP_PATH | name | Web app path |
FLET_SERVER_IP | host | Server host/IP |
FLET_SERVER_PORT | port | Server port |
FLET_ASSETS_DIR | assets_dir | Assets directory |
FLET_FORCE_WEB_SERVER | view | Force web browser mode |
FLET_SERVER_UDS_PATH | - | Unix domain socket path |
FLET_DISPLAY_URL_PREFIX | - | URL prefix for display |
run_async()
Asynchronously run a Flet app using socket or web server transport.Source:
app.py:142Parameters
Same asrun(), except:
- No
export_asgi_appparameter - Must be called with
awaitfrom an async context
Usage
AppCallable Type
Source:app.py:34
Page argument. The return value is ignored.
Used for both main and before_main handlers.
Deprecated Functions
app()
DEPRECATED: Use
run() instead.Deprecated in version 0.80.0.Source: app.py:44app_async()
DEPRECATED: Use
run_async() instead.Deprecated in version 0.80.0.Source: app.py:52Usage Examples
Basic Desktop App
Web Application
Async Application
Generator-based Updates
With Setup Hook
ASGI Export for Production
Multi-platform Configuration
With Custom Assets and Uploads
See Also
- Page - Page class reference
- [AppView(/api/app) - App view modes
- [WebRenderer(/guides/web-apps) - Web renderer types
- [RouteUrlStrategy(/concepts/routing) - URL routing strategies