How to Deploy Flask API on Heroku?

| | ,

Home » How to Deploy Flask API on Heroku?

Heroku is a container-based cloud Platform as a Service (PaaS). Developers use Heroku to deploy, manage, and scale modern apps. It provides a choice of four infrastructure providers. It uses language-specific build packs. And it hosts apps written in any language. Heroku was first deployed in 2007, and in 2008 it was released as open-source. Since then Heroku has grown to be a key player in the cloud industry, today offering a wide range of products and services to help developers. In this tutorial, I’m going to show you how to deploy your Flask API to Heroku.

Web hosting is a common stumbling block for beginners, and in this article, we’ll help you overcome it step by step. Now that might sound difficult but it’s really not. In fact, you can be up and running in minutes, and then your app will be live on the internet. We will cover as much of the process as possible and walk through each phase as smoothly as we can so you don’t miss anything important on your journey to getting your Flask app live online with Heroku!

Prerequisites

Before we get started, you should first sign-up for a Heroku account, which you can do by going here and fill-up the form. After that it’s important for you to install some software in order for your web app to run properly – specifically on Heroku, we will focus on installing some necessary software requirements for our Python web app, which in turn, is not very much as compared to any other services or apps.

Install Git

One of the most important things we need to deploy our Flask application to Heroku is Git. Git is a version control software that has to power to store and track changes made to any files. In Git, each file is its own repository. You can use it to save your work, undo mistakes, and collaborate with other users easily. Heroku uses Git to receive the data that you want to upload. It also makes it easier to make changes to the data whenever you may need to do so.

Linux

On Linux, GIt can be installed through the package management tool that comes with your distribution. If you are on a Debian based system such as Ubuntu, try apt:

sudo apt install git-all

And if you’re on Fedora or (any related RPM-based distribution) other OS such as CentOS, you can use dnf: 

sudo dnf install git-all

Mac

Git is likely to be already installed on Mac-based systems. It comes with Xcode as a binary package so it would be just a matter of trying to run git from the Terminal the very first time:

git --version

If you don’t already have it installed, you would get prompted by a dialogue box to install it.

Alternatively, if you have brew installed just run:

brew install git

Windows

There are a lot of ways you can get it on Windows, but the easiest would be to just install it from Git’s official website.

Setup Git

When you first install Git, the first thing you should do is set up your user name and email address. This information is important to Git since it’s needed to log into repositories, so you will want to make sure this information is correct before you start using Git.

git config --global user.name "example name"
git config --global user.email [email protected]

Install The Heroku CLI

After installing and setting up Git, we can proceed and install the Heroku CLI which is our main piece of software that would help us deploy our Flask application. The Heroku CLI for all Operating systems can be found on their official page, Install it for your respective OS form there.

Deploying a Flask App on Heroku

After installing and setting up all the prerequisites we can finally start to deploy our flask application and show that I have created a very basic flask application that I use to demonstrate the whole process. 

Create a Flask Application

If you want to recreate the same application that I built, you can paste the following code in your main.py and wsgi.py files respectively.

#app/main.py

from flask import Flask

app = Flask(__name__)

@app.route("/")
def home_view():
        return "<h1>Hello World!</h1>"
#wsgi.py

from app.main import app

if __name__ == "__main__":
        app.run()

It is a simple application that will display a hello world text message.

Create a Virtual Environment

Now, you have to use a virtual environment in order to deploy your Flask application. This step is absolutely necessary, without a virtual environment Heroku will not accept your application.

You can create a virtual environment in the following manner:

Install a virtual environment if you don’t already have

python3 -m pip install --user virtualenv  #Linux/Macpy -m pip install --user virtualenv  #Windows

Create and activate it in the project folder itself:

#Linux/Macpython3 -m venv env  source env/bin/activate
#Windows
py -m venv env  .\env\Scripts\activate

Add a Professional Server to your Application

In order to use Heroku to push our code, we need to make a few changes to our app, a couple of tweaks here and there. Until now we have been using the development server that comes with flask and which is fine for development purposes but it’s not a professional web server and we’ll need one to be able to push our code to Heroku. So, we can install a lightweight WSGI HTTP Server called gunicorn and integrate it into our app. 

We can install gunicorn with:

pip install gunicorn

Now we have to use this and Heroku knows that have this server to use. In order to do that, we have to make a file called a procfile, which is a Heroku-specific file. Go to your project directory and create a new file and name “Procfile”, in this specific way. Enter that file write the following line of code in it:

web: gunicorn wsgi:app  

Note: You may have to edit this according to your application

Creating the Necessary Files

We also have to add some more things to our project in order to make the deployment process go smoothly. There is a requirements.txt file that will define what modules we are using. 

There are pretty easy to make, you can write them as shown below:

#requirements.txt

gunicorn
flask

Now, in order to push our code to Heroku, it needs to be in the form of a Git repository, that is the reason we first installed Git at the starting of the article. To do that, go to your project directory, open the terminal and run the following command:

git init 

This will create an empty repository called “master”, where we can push our code. We can do that with:

git add .

The “add .” in the command specifies that we need to push all the files into the repository. After that, we just now have to commit that to the repository, by:

git commit -am 'initial commit'

You can name the commit anything you want instead of ‘initial commit’

Deploying the application

We are now ready to push our Flask application to Heroku and we start by logging in:

heroku login 

This will ask you to log in through to the browser tab it opened, login, and close the tab. You should get a result like this in your terminal.

Now let’s create a domain to publish our app too, run the following code:

heroku create  #run this for a random nameheroku create <any-name-you-want>  #or write any name you want

And it will create a domain that would look something like this:

And now, the final step is to push it all to Heroku and we can do that with: 

git push heroku master

Which, if successful, would return a response like this:

Output

Now you can go to the domain that we created, which in our case is https://limitless-wildwood-42018.herokuapp.com, and see the results for yourself. The app that I created looks like this:

Final Words

Deploying a flask application on Heroku is a breeze. All you need to do is to create a Heroku account and create an app and all the hosting-related stuff is done by Heroku itself. 

Let’s do a quick rundown of what we just did, We installed the gunicorn web server, created a requirements.txt file and Procfile, set up git version control on your machine (which is essential when it comes to deploying apps), install the free Heroku toolbelt software, and then deploy your app to Heroku!

Here are some useful tutorials that you can read:

Spread the love
 
   
Previous

How to Implement Google Login in Flask App

How to create a Word Guessing Game in Python

Next

Leave a Comment