FastAPI - Mounting Flask App
Last Updated :
06 Dec, 2023
A modern framework in Python that is used for building APIs is called FastAPI while a micro web framework written in Python that is used for developing web applications is called Flask. The FastAPI and Flask provide different features. Sometimes, it becomes crucial to use features of both in a single app. This can be possible by mounting the Flask app on the FastAPI app.
Syntax:
- from fastapi.middleware.wsgi import WSGIMiddleware
- main_app.mount("/flask", WSGIMiddleware(flask_app))
Here,
- main_app: It is the main app on which you want to mount the Flask app.
- flask_app: It is the Flask app that has to be mounted on the main app.
FastAPI - Mounting Flask App
In this example, we have created a main app called, app and a flask app called, flaskapp. Then, we mounted a Flask app on the main app using WSGIMiddleware.
main.py: This Python code demonstrates how to combine a FastAPI application with a Flask application using FastAPI's middleware. It defines two routes:
- The FastAPI route at "/app" responds with "Main app called!" when accessed.
- The Flask route at "/app2" responds with "Flask app called!" when accessed.
Python3
from fastapi import FastAPI
from flask import Flask
from fastapi.middleware.wsgi import WSGIMiddleware
app = FastAPI()
flaskapp = Flask(__name__)
@app.get("/app")
def main_app():
return "Main app called!"
@flaskapp.route("/app2")
def flask_app():
return "Flask app called!"
app.mount("/flask", WSGIMiddleware(flaskapp))
For calling the value returned from main app function, we use the following code in test file
Test 1: This code sends an HTTP GET request to the URL "http://127.0.0.1:8000/app" using the requests
library. It tries to parse the response as JSON using the .json()
method. The output will be the response from the server at that URL in JSON format.
Python3
import requests
print(requests.get("http://127.0.0.1:8000/app").json())
Output:

Browser Output:
-660.png)
For calling the value returned from sub-app function, we use the following code in test file:
Test 1: This code sends an HTTP GET request to the URL "http://127.0.0.1:8000/flask/app2" using the requests
library. It fetches the content from that URL and prints it as text. The output will be the text content of the response from the Flask app running at that URL.
Python3
import requests
print(requests.get("http://127.0.0.1:8000/flask/app2").text)
Output

Output

Similar Reads
FastAPI - Mounting A Sub-App A Python web framework that is used for building APIs is called FastAPI. The creation of two or more APIs and using them in a project consumes more memory. The more efficient way is to mount the APIs on each other, which will consume less memory and give a different path to each API. Syntax of FastA
2 min read
Flask App Routing App Routing means mapping the URLs to a specific function that will handle the logic for that URL. Modern web frameworks use more meaningful URLs to help users remember the URLs and make navigation simpler. Example: In our application, the URL ("/") is associated with the root URL. So if our site's
3 min read
How to Run a Flask Application After successfully creating a Flask app, we can run it on the development server using the Flask CLI or by running the Python script. Simply execute one of the following commands in the terminal:flask --app app_name runpython app_nameFile StructureHere, we are using the following folder and file.Dem
4 min read
Deploy Flask App on AWS EC2 Windows Do you want to host a Windows-based Python Flask application on Amazon Web Services (AWS) Elastic Compute Cloud (EC2)? Flask is a frequently utilized framework for building web apps, APIs, and prototypes because of its ease of use and versatility. AWS EC2 provides virtual machines that are scalable,
5 min read
Todo list app using Flask | Python There are many frameworks that allow building your webpage using Python, like Django, flask, etc. Flask is a web application framework written in Python. Flask is based on WSGI(Web Server Gateway Interface) toolkit and Jinja2 template engine. Its modules and libraries that help the developer to writ
3 min read