FastAPI Tutorial
Selected Reading
- FastAPI - Discussion
- FastAPI - Useful Resources
- FastAPI - Quick Guide
- FastAPI - Deployment
- FastAPI - Mounting Flast App
- FastAPI - Middleware
- FastAPI - Mounting A Sub-App
- FastAPI - FastAPI Event Handlers
- FastAPI - Websockets
- FastAPI - Using GraphQL
- FastAPI - Using MongoDB
- FastAPI - SQL Databases
- FastAPI - Crud Operations
- FastAPI - CORS
- FastAPI - Dependencies
- FastAPI - Nested Models
- FastAPI - Response Model
- FastAPI - Header Parameters
- FastAPI - Cookie Parameters
- FastAPI - Uploading Files
- FastAPI - Accessing Form Data
- FastAPI - HTML Form Templates
- FastAPI - Static Files
- FastAPI - Templates
- FastAPI - Request Body
- FastAPI - Pydantic
- FastAPI - Parameter Validation
- FastAPI - Query Parameters
- FastAPI - Path Parameters
- FastAPI - Rest Architecture
- FastAPI - IDE Support
- FastAPI - Type Hints
- FastAPI - Uvicorn
- FastAPI - OpenAPI
- FastAPI - Hello World
- FastAPI - Introduction
- FastAPI - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
FastAPI - Mounting Flast App
FastAPI - Mounting Flask App
A WSGI apppcation written in Flask or Django framework can be wrapped in WSGIMiddleware and mounted it on a FastAPI app to make it ASGI comppant.
First install the Flask package in the current FastAPI environment.
pip3 install flask
The following code is a minimal Flask apppcation −
from flask import Flask flask_app = Flask(__name__) @flask_app.route("/") def index_flask(): return "Hello World from Flask!"
Then declare app as a FastAPI apppcation object and define an operation function for rendering Hello World message.
from fastapi import FastAPI app = FastAPI() @app.get("/") def index(): return {"message": "Hello World from FastAPI!"}
Next, mount the flask apppcation as a sub apppcation of FastAPI main app using mount() method.
from fastapi.middleware.wsgi import WSGIMiddleware app.mount("/flask", WSGIMiddleware(flask_app))
Run the Uvicorn development server.
uvicorn flaskapp:app –reload
The main FastAPI apppcation is available at the URL http://localhost:8000/ route.
{"message":"Hello World from FastAPI!"}
The Flask sub apppcation is mounted at the URL http://localhost:8000/flask.
Hello World from Flask!Advertisements