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:
我做了一个pwa,现在我想把它发布到我的网站上,这个网站使用了Flet。问题是,我不知道如何将Flet和Full结合起来。我想代码应该是这样的:
@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.
Flet并没有正式支持Flask.但最近它又增加了对FastAPI的支持,FastAPI很容易实现和使用。
if there is any chance, I would recommend you to switch to FastAPI from Flask.
如果有机会,我会建议你从Flask转到FastAPI。
to get started with implementing Flet with FastAPI,
要开始使用FastAPI实现Flet,
install flet-fastapi and uvicorn
安装Flet-Fastapi和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
Uvicorn Hello:应用程序
which will open the app at http://127.0.0.1:8000/
它将在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/
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.
Flet应用程序必须是异步的,才能使用FastAPI WebSocket处理程序。
更多回答
我是一名优秀的程序员,十分优秀!