- FastAPI 教程
- FastAPI - 首頁
- FastAPI - 簡介
- FastAPI - hello world
- FastAPI - OpenAPI
- FastAPI - Uvicorn
- FastAPI - 型別提示
- FastAPI - IDE 支援
- FastAPI - REST 架構
- FastAPI - 路徑引數
- FastAPI - 查詢引數
- FastAPI - 引數驗證
- FastAPI - Pydantic
- FastAPI - 請求正文
- FastAPI - 模板
- FastAPI - 靜態檔案
- FastAPI - HTML 表單模板
- FastAPI - 訪問表單資料
- FastAPI - 上傳檔案
- FastAPI - Cookie 引數
- FastAPI - 標頭引數
- FastAPI - 響應模型
- FastAPI - 巢狀模型
- FastAPI - 依賴
- FastAPI - CORS
- FastAPI - CRUD 操作
- FastAPI - SQL 資料庫
- FastAPI - 使用 MongoDB
- FastAPI - 使用 GraphQL
- FastAPI - WebSocket
- FastAPI - FastAPI 事件處理程式
- FastAPI - 安裝子應用程式
- FastAPI - 中介軟體
- FastAPI - 安裝 Flask 應用程式
- FastAPI - 部署
- FastAPI 有用資源
- FastAPI - 快速指南
- FastAPI - 有用資源
- FastAPI - 討論
FastAPI - 安裝 Flask 應用程式
可以使用 WSGIMiddleware 封裝在 Flask 或 Django 框架中編寫的 WSGI 應用程式,然後將其安裝在 FastAPI 應用程式上以使其與 ASGI 相容。
首先在當前 FastAPI 環境中安裝 Flask 包。
pip3 install flask
以下程式碼是一個最小的 Flask 應用程式 −
from flask import Flask
flask_app = Flask(__name__)
@flask_app.route("/")
def index_flask():
return "Hello World from Flask!"
然後,將 app 宣告為 FastAPI 應用程式物件,併為呈現 Hello World 訊息定義一個操作函式。
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def index():
return {"message": "Hello World from FastAPI!"}
接下來,使用 mount() 方法將 Flask 應用程式安裝為 FastAPI 主應用程式的子應用程式。
from fastapi.middleware.wsgi import WSGIMiddleware
app.mount("/flask", WSGIMiddleware(flask_app))
執行 Uvicorn 開發伺服器。
uvicorn flaskapp:app –reload
主要的 FastAPI 應用程式位於 URL https://:8000/ 路徑中。
{"message":"Hello World from FastAPI!"}
將 Flask 子應用程式安裝在 URL https://:8000/flask 中。
Hello World from Flask!
廣告