flet-dev / flet

Flet enables developers to easily build realtime web, mobile and desktop apps in Python. No frontend experience required.
https://flet.dev
Apache License 2.0
10.87k stars 419 forks source link

Serverless Flet (Edge functions, Vercel) #1786

Open srang992 opened 1 year ago

srang992 commented 1 year ago

Recently I tried flet-fastapi and created a sample app. It works fine locally, but when I deploy it on Vercel as a serverless function, it gives me an error. below are the files I am using for deployment.

directory structure

- api
  - index.py
- assets
  - Courgette-Regular.ttf
- requirements.txt
- vercel.json

api/index.py

import flet as ft
import flet_fastapi
import os

async def main(page: ft.Page):
    page.fonts = {
        "Courgette": "/fonts/Courgette-Regular.ttf"
    }
    page.vertical_alignment = ft.MainAxisAlignment.CENTER
    page.horizontal_alignment = ft.CrossAxisAlignment.CENTER

    await page.add_async(
        ft.Text("Welcome to Flet, FastAPI!", size=40, font_family="Courgette")
    )

app = flet_fastapi.app(main, assets_dir=os.path.abspath("assets"))

requirements.txt

flet
flet-fastapi
uvicorn

vercel.json

{
    "devCommand": "uvicorn api.index:app --port 3000",
    "builds": [
        {
            "src": "api/index.py",
            "use": "@vercel/python"
        }
    ],
    "routes": [
        {
            "src": "/(.*)",
            "dest": "api/index.py"
        }
    ]
}

the error

[ERROR] [1693543644897] LAMBDA_RUNTIME Failed to post handler success response. Http response code: 413.
Traceback (most recent call last):
File "/var/runtime/bootstrap.py", line 60, in <module>
main()
File "/var/runtime/bootstrap.py", line 57, in main
awslambdaricmain.main([os.environ["LAMBDA_TASK_ROOT"], os.environ["_HANDLER"]])
File "/var/runtime/awslambdaric/__main__.py", line 21, in main
bootstrap.run(app_root, handler, lambda_runtime_api_addr)
File "/var/runtime/awslambdaric/bootstrap.py", line 405, in run
handle_event_request(
File "/var/runtime/awslambdaric/bootstrap.py", line 176, in handle_event_request
lambda_runtime_client.post_invocation_result(
File "/var/runtime/awslambdaric/lambda_runtime_client.py", line 83, in post_invocation_result
runtime_client.post_invocation_result(
RuntimeError: Failed to post invocation response
RequestId: c23a7f13-c079-4de8-a835-4dc6b4671969 Error: Runtime exited with error: exit status 1
Runtime.ExitError
FeodorFitsner commented 1 year ago

I don't think it's currently possible to run Flet on lambda-like backends as they are 1) stateless and 2) don't support WebSockets. However, in the future we might add another "lambda" backend that works like send request from the browser -> dehydrate page state in lambda -> process event -> send page update delta -> hydrate state.

Python runtime for reference: https://vercel.com/docs/functions/serverless-functions/runtimes/python

FeodorFitsner commented 1 year ago

Alternatively, you can publish entire Flet app as a static website and use Vercel edge functions as a backend.