Deploying a Python Flask App on Heroku: A Step-by-Step Guide

Posted by

How to Deploy a Python Flask App on Heroku

How to Deploy a Python Flask App on Heroku

If you have built a Python Flask web application and you are looking to deploy it on the web, Heroku is a great platform to do so. Heroku is a cloud platform that allows you to easily deploy, manage, and scale applications. In this article, we will guide you through the steps to deploy a Python Flask app on Heroku.

Step 1: Install Heroku CLI

First, you need to install the Heroku Command Line Interface (CLI) on your computer. You can download and install the CLI from the official Heroku website.

Step 2: Create a Heroku Account

If you don’t already have a Heroku account, you will need to create one. Sign up for a free account on the Heroku website.

Step 3: Prepare Your Flask App

Before deploying your Flask app, make sure it is set up properly. Your Flask app should include a requirements.txt file with all the dependencies needed for your app.

Step 4: Create a Procfile

Next, create a Procfile in the root directory of your Flask app. The Procfile tells Heroku how to run your app. Here is an example Procfile for a basic Flask app:

web: gunicorn app:app

Step 5: Deploy Your App

Now that your app is set up and ready to go, you can deploy it to Heroku. Use the following commands in your terminal to deploy your app:

heroku login
heroku create
git add .
git commit -m "Initial commit"
git push heroku master

After running these commands, your Flask app should be deployed to Heroku. You can access your app at the URL provided by Heroku.

Step 6: Scale Your App (Optional)

If you expect a large number of users to access your app, you may need to scale it to handle the traffic. You can scale your app on Heroku using the following command:

heroku ps:scale web=1

This command will scale your app to run one web dyno, which should be enough for most small to medium-sized apps. You can increase the number of dynos if needed.

Conclusion

Deploying a Python Flask app on Heroku is a straightforward process that allows you to easily share your app with the world. By following the steps outlined in this article, you can have your Flask app up and running on Heroku in no time.