In this tutorial, I will show you how to deploy your Flask app to Vercel for free in a few simple steps.
This tutorial works as of 2024 as there are some tutorials out there that are outdated when Vercel changed its features. So don't worry, this tutorial will work for you.
Please note that Vercel only supports Flask as a serverless function, which means it can only serve as an API.
Contents
How to Deploy Flask App to Vercel for Free
1. Create a Flask app
First, let's create a simple Flask app. If you already have one, you can skip this step.
The project structure should look like this:
flask-vercel ├─ vercel.json ├─ requirements.txt └─ api └─ index.py
To create a Flask app, run the following commands:
mkdir flask-vercel cd flask-vercel python -m venv venv source venv/bin/activate # use this for Linux/Mac # venv\Scripts\activate # use this for Windows pip install Flask
Then, create a file named api/index.py
with the following content:
from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello from Minh Vu!"
Let's test if the app works by running the following command:
flask --app api/index.py run
Then, open your browser and go to http://localhost:5000. You should see the following message:
Hello from Minh Vu!
That's all, keep it simple for now. Let's move on to the next step.
2. Create a Vercel account
Vercel is a cloud hosting service that offers a generous free plan for developers. So you don't have to pay anything while following this tutorial.
To create an account, visit the registration page and follow the instructions.
This is a simple step so I won't go into details.
3. Deploy Flask app to Vercel using CLI
Now, let's deploy the Flask app to Vercel using the Vercel CLI.
Here are the steps:
-
Install the Vercel CLI.
console npm install -g vercel
-
Create the
vercel.json
file and add the following content:vercel.json { "rewrites": [{ "source": "/(.*)", "destination": "/api/index" }] }
-
Now, create the
requirements.txt
file:console pip freeze > requirements.txt
-
Login to Vercel.
console vercel login
-
Choose
Login with Email
. Then enter your email and wait for the confirmation email from Vercel.console ? Enter your email address: <your_email>
-
As you log in, you can deploy the Flask app to Vercel by running the following command:
console vercel --prod
Enter the same information with the image below.
Figure: Deploy Flask app to Vercel
Cool, the Flask app is deployed at https://flask-vercel-dun.vercel.app.
Conclusion
In this tutorial, you have learned how to deploy a Flask app to Vercel for free in a few simple steps. The post is updated as of 2024, so you can follow it without any issues.
If you have any questions, feel free to leave a comment below.
Comments
Be the first to comment!