Deploying A Flask App On Heroku

Deploying a Flask App on Heroku: A Comprehensive Guide for Python Enthusiasts

Flask is a lightweight and powerful web framework for Python. Its simplicity and flexibility make it an excellent choice for both beginners and experienced developers. Once you have built your Flask application, the next step is to make it available to others. That’s where Heroku comes in. Heroku is a popular platform-as-a-service (PaaS) that simplifies app deployment. In this tutorial, we walk through the step-by-step process of deploying a Flask app on Heroku.


Deploying A Flask App On Heroku
Deploying A Flask App On Heroku

Prerequisites

Before we begin, ensure you have the following:

  • Basic understanding of Python and Flask.
  • Your Flask application ready for deployment.
  • Git installed on your local machine.
  • A Heroku account. It’s free and easy to setup.
  • Heroku CLI installed on your local machine.

On Your Local Environment

You’ll first need to prepare your Flask application locally before deploying it to Heroku.

Step 1: Git Configuration

First, initialize a Git repository in your project folder. Open your command prompt or terminal, navigate to the project directory, and run the following command:

git init

Step 2: Project Structure

Ensure your project has the following structure:

/myproject
    /static
    /templates
    app.py

Step 3: Requirements.txt

Heroku recognizes Python applications by the presence of certain files in the repository, one of which is requirements.txt. This file lists the Python dependencies your app needs to run.

To create one, use pip freeze command:

pip freeze > requirements.txt

Step4: Procfile

Another special file that Heroku requires is the Procfile. It tells Heroku which command to run to start your app.

Create a file named Procfile (no extension) in the project root directory, containing the following line:

web: gunicorn app:app

Here ‘app’ is the name of the python file that runs your application or the name of your module, followed by :app, the name of the variable in the python file that holds your Flask app instance.

Step 5: Gunicorn

The Procfile uses gunicorn, which is a Web Server Gateway Interface (WSGI), as the server. So, we need to install it to the local environment:

pip install gunicorn

Remember to update requirements.txt after installing gunicorn:

pip freeze > requirements.txt

Deploy Application to Heroku

Step 1: Create Heroku App

Login to your Heroku account through CLI:

heroku login

Create a new application:

heroku create <app_name>

Step 2: Deploying App

After all the setup is done, it’s time to deploy. We commit our code to the Git repository and push this repo to the Heroku:

git add .
git commit -m "first commit"
git push heroku master

Step 3: Open App

If your app was built successfully, you could open it with:

heroku open

Congratulations! Your flask application is now deployed on Heroku.

Common Troubleshooting And Tips

  • If deployment fails, you can check for errors using heroku logs --tail.
  • Every time you make changes to your application, you need to commit your changes to git before you deploy to Heroku.
  • You can scale your application up or down on Heroku via: heroku ps:scale web=1, where web=1 indicates the number of active instances.

Conclusion

Deploying a Flask app on Heroku can be done in a few steps: configuring the project structure, setting up requirements, creating a Procfile, and pushing the code to Heroku. If problems occur on the way, Heroku’s error logs should provide sufficient information to troubleshoot. Meanwhile, familiarizing yourself with these steps can make your journey of Flask app development and deployment smoother and rewarding.

Thank you for sticking with us through this comprehensive guide. Happy programming! Keep learning and keep implementing.

Share this article:

Leave a Comment