I made a pwa which I now want to publish to my website which uses flet. The problem is that I have no idea how to combine flet with with flask. I imagine the code would look something like this:
@app.route('/flashcards')
def flashcards():
def main(page: ft.Page):
page.title = "title"
page.add(ft.Text("Test text"))
ft.app(target=main, view=None, port=5000)
but every time I run it it says
Please wait while the application is being started...
and the error
Exception: Could not connected to Flet server in 30 seconds
Here is a log:
127.0.0.1 - - [26/Jun/2023 18:45:11] "GET /flutter_service_worker.js?v=2289608434 HTTP/1.1" 404 -
127.0.0.1 - - [26/Jun/2023 18:45:12] "GET /ws HTTP/1.1" 404 -
127.0.0.1 - - [26/Jun/2023 18:45:14] "GET /ws HTTP/1.1" 404 -
127.0.0.1 - - [26/Jun/2023 18:45:16] "GET /ws HTTP/1.1" 404 -
127.0.0.1 - - [26/Jun/2023 18:45:18] "GET /ws HTTP/1.1" 404 -
127.0.0.1 - - [26/Jun/2023 18:45:20] "GET /ws HTTP/1.1" 404 -
127.0.0.1 - - [26/Jun/2023 18:45:22] "GET /ws HTTP/1.1" 404 -
127.0.0.1 - - [26/Jun/2023 18:45:24] "GET /ws HTTP/1.1" 404 -
127.0.0.1 - - [26/Jun/2023 18:45:26] "GET /ws HTTP/1.1" 404 -
127.0.0.1 - - [26/Jun/2023 18:45:27] "GET /ws HTTP/1.1" 404 -
127.0.0.1 - - [26/Jun/2023 18:45:29] "GET /ws HTTP/1.1" 404 -
127.0.0.1 - - [26/Jun/2023 18:45:31] "GET /ws HTTP/1.1" 404 -
127.0.0.1 - - [26/Jun/2023 18:45:33] "GET /ws HTTP/1.1" 404 -
127.0.0.1 - - [26/Jun/2023 18:45:34] "GET /ws HTTP/1.1" 404 -
127.0.0.1 - - [26/Jun/2023 18:45:36] "GET /ws HTTP/1.1" 404 -
127.0.0.1 - - [26/Jun/2023 18:45:38] "GET /ws HTTP/1.1" 404 -
127.0.0.1 - - [26/Jun/2023 18:45:40] "GET /ws HTTP/1.1" 404 -
127.0.0.1 - - [26/Jun/2023 18:45:42] "GET /ws HTTP/1.1" 404 -
127.0.0.1 - - [26/Jun/2023 18:45:44] "GET /ws HTTP/1.1" 404 -
127.0.0.1 - - [26/Jun/2023 18:45:46] "GET /ws HTTP/1.1" 404 -
127.0.0.1 - - [26/Jun/2023 18:45:48] "GET /ws HTTP/1.1" 404 -
127.0.0.1 - - [26/Jun/2023 18:45:50] "GET /ws HTTP/1.1" 404 -
127.0.0.1 - - [26/Jun/2023 18:45:53] "GET /ws HTTP/1.1" 404 -
127.0.0.1 - - [26/Jun/2023 18:45:55] "GET /ws HTTP/1.1" 404 -
127.0.0.1 - - [26/Jun/2023 18:45:57] "GET /ws HTTP/1.1" 404 -
127.0.0.1 - - [26/Jun/2023 18:45:59] "GET /ws HTTP/1.1" 404 -
127.0.0.1 - - [26/Jun/2023 18:46:00] "GET /ws HTTP/1.1" 404 -
127.0.0.1 - - [26/Jun/2023 18:46:02] "GET /ws HTTP/1.1" 404 -
127.0.0.1 - - [26/Jun/2023 18:46:04] "GET /ws HTTP/1.1" 404 -
127.0.0.1 - - [26/Jun/2023 18:46:06] "GET /ws HTTP/1.1" 404 -
Flet doesn't officially support Flask, but recently it added support for FastAPI which is easy to implement and easy use.
if there is any chance, I would recommend you to switch to FastAPI from Flask.
to get started with implementing Flet with FastAPI,
install flet-fastapi and uvicorn
pip install flet-fastapi
pip install uvicorn
a basic app should look like this
import flet as ft
import flet_fastapi
async def main(page: ft.Page):
await page.add_async(
ft.Text("Hello, Flet!")
)
app = flet_fastapi.app(main)
to run the above, use the command
uvicorn hello:app
which will open the app at http://127.0.0.1:8000/
I recommend you to check out the official documentation:
https://flet.dev/docs/guides/python/deploying-web-app/running-flet-with-fastapi/
Note:
Flet app must be async in order to work with FastAPI WebSocket handler.