Skip to main content

Flet - a better UI for FastAPI

Flet for FastAPI allows adding interactive real-time dashboards to your FastAPI app as well as host any Flet web app inside FastAPI with production-grade reliability.

Installation

pip install flet-fastapi

First app

Create counter.py with the following content:

import flet as ft
import flet_fastapi

async def main(page: ft.Page):
    counter = ft.Text("0", size=50, data=0)

    async def add_click(e):
        counter.data += 1
        counter.value = str(counter.data)
        await counter.update_async()

    page.floating_action_button = ft.FloatingActionButton(
        icon=ft.icons.ADD, on_click=add_click
    )
    await page.add_async(
        ft.Container(counter, alignment=ft.alignment.center, expand=True)
    )

app = flet_fastapi.app(main)

That's a simple app displaying a counter and a button at the right bottom to increment that counter.

flet_fastapi.app() configures a single Flet app at the root of FastAPI app with main() sessions handler and the following endpoints:

/ws (WS) - WebSocket handler for the Flet app.

/upload (PUT) - file uploads handler.

/oauth_callback (GET) - OAuth flow callback handler.

/ (GET) - Flet app static files with SPA catch-all handler.

Running the app locally

Install Uvicorn web server:

pip install uvicorn

Start uvicorn with:

uvicorn counter:app

Open the browser and navigate to http://127.0.0.1:8000 to see the app running.

Hosting multiple Flet apps under the same domain

import flet as ft
import flet_fastapi


async def root_main(page: ft.Page):
    await page.add_async(ft.Text("This is root app!"))


async def sub_main(page: ft.Page):
    await page.add_async(ft.Text("This is sub app!"))


app = flet_fastapi.FastAPI()


app.mount("/sub-app", flet_fastapi.app(sub_main))
app.mount("/", flet_fastapi.app(root_main))

Sub-apps must be mapped before the root Flet app as it configures catch-all index.html for SPA.

Run the app with uvicorn and visit http://127.0.0.1:8000 and then http://127.0.0.1:8000/sub-app/ to see both Flet apps running. Notice the trailing slash in /sub-app/ URL - without the slash the request will be routed to a root app.

Adding Flet to the existing FastAPI app

from contextlib import asynccontextmanager

import flet as ft
import flet_fastapi
from fastapi import FastAPI

@asynccontextmanager
async def lifespan(app: FastAPI):
    await flet_fastapi.app_manager.start()
    yield
    await flet_fastapi.app_manager.shutdown()

app = FastAPI(lifespan=lifespan)

async def main(page: ft.Page):
    await page.add_async(ft.Text("Hello, Flet!"))

app.mount("/flet-app", flet_fastapi.app(main))

When adding Flet app to the existing FastAPI app you need to call flet_fastapi.app_manager.start() on app start and flet_fastapi.app_manager.shutdown() on shutdown. Use the way that best suites you: lifespan (in the example above) or app events.

app_manager.start() method starts background tasks cleaning up expired sessions and OAuth flow states.

app_manager.shutdown() method removes any temporary files created by a Flet app.

Running the app in production

It is recommended to run FastAPI in production with Hypercorn which is ASGI web server, but it is also possible to run FastAPI apps with Gunicorn which is a WSGI server, but has more features, like passing proxy headers.

To install Gunicorn:

pip install gunicorn

Start gunicorn with:

gunicorn -k uvicorn.workers.UvicornWorker counter:app

Reference

Environment variables

FLET_SECRET_KEY - secret key to sign upload requests. Must be set if upload directory is configured.

FLET_SESSION_TIMEOUT - the number of seconds to keep session alive after user has disconnected. Default is 3,600 seconds.

FLET_OAUTH_STATE_TIMEOUT - OAuth state lifetime, in seconds, which is a maximum allowed time between starting OAuth flow and redirecting to OAuth callback URL. Default is 600 seconds.

FLET_MAX_UPLOAD_SIZE - max allowed size of an uploaded file, bytes.

Release files for flet-fastapi-proxy-path 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flet-fastapi-proxy-path 0.1.2
File Size Uploaded
flet_fastapi_proxy_path-0.1.2.tar.gz 13.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flet-fastapi-proxy-path 0.1.2
File Interpreter ABI Platform
flet_fastapi_proxy_path-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 30.0 kB

Release files / flet_fastapi_proxy_path-0.1.2.tar.gz

Download URL flet_fastapi_proxy_path-0.1.2.tar.gz
Size 13.8 kB
Tags Source
SHA-256 checksum
How to use checksums
685a279b93655a91fc6442809f3a3b9c74508bb35e9edd8f0a7bfcfb874fae7d
BLAKE2b-256 checksum
How to use checksums
c32e9b83d2a3166db875ba530bd5c61c37a3601c56b79ea067561cf2f6e8bf5e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.2

Release files / flet_fastapi_proxy_path-0.1.2-py3-none-any.whl

Download URL flet_fastapi_proxy_path-0.1.2-py3-none-any.whl
Size 16.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e438835cd6c385ad2a5b509f559ab93d39e97bab91fc0d19e1aa22f01f74babe
BLAKE2b-256 checksum
How to use checksums
d28b317b0185ce908d0670c840af96a1fb45f1640f07cd348f529af4197ac7d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.2

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page