Flask on Vercel
Flask is a lightweight WSGI web application framework for Python. It's designed with simplicity and flexibility in mind, making it easy to get started while remaining powerful for building web applications. You can deploy a Flask app to Vercel with zero configuration.
You can quickly deploy a Flask application to Vercel by creating a Flask app or using an existing one:
Get started by initializing a new Flask project using Vercel CLI init command:
vc init flask
This will clone the Flask example repository in a directory called flask
.
To run a Flask application on Vercel, define an app
instance that initializes Flask
at any of the following entrypoints:
app.py
index.py
server.py
src/app.py
src/index.py
src/server.py
app/app.py
app/index.py
app/server.py
For example:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return {"message": "Hello, World!"}
Use vercel dev
to run your application locally.
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
vercel dev
To deploy, connect your Git repository or use Vercel CLI:
vc deploy
To serve static assets, place them in the public/**
directory. They will be served as a part of our Edge Network using default headers unless otherwise specified in vercel.json
.
from flask import Flask, redirect
app = Flask(__name__)
@app.route("/favicon.ico")
def favicon():
# /vercel.svg is automatically served when included in the public/** directory.
return redirect("/vercel.svg", code=307)
Flask's app.static_folder
should not be used for static files on Vercel. Use
the public/**
directory instead.
When you deploy a Flask app to Vercel, the application becomes a single Vercel Function and uses Fluid compute by default. This means your Flask app will automatically scale up and down based on traffic.
All Vercel Functions limitations apply to Flask applications, including:
- Application size: The Flask application becomes a single bundle, which must fit within the 250MB limit of Vercel Functions. Our bundling process removes
__pycache__
and.pyc
files from the deployment's bundle to reduce size, but does not perform application bundling.
Learn more about deploying Flask projects on Vercel with the following resources:
Was this helpful?