Deploying Flask Application on Heroku

Deploying Flask Application on Heroku
Author: Mohammad Amaan Abbasi | Jan. 28, 2019

Deploying a simple Flask Based Web Application on Heroku

Whenever you have to deploy a application on heroku you need to have a Procfile.

What's inside Procfile ?

web: gunicorn application:app

gunicorn: production quality server

application : your application name

app : app corresponds to app = Flask(__name__)

requirements.txt is yet another important file you may have, if your application depends on some external libraries.

Don't forget to add "gunicorn" to requirements.txt

Most of the time you will forget to put gunicorn to the requirements file. This is a common reason for the gunicorn command not found error in heroku

What's inside requirements.txt ?

It contains the dependencies that are need by our application to run. When you deploy your application to heroku, It searches for this files and installs the dependencies for you.

Uploading code to github

There are different methods by which you can upload your application code to Heroku. You can use the heroku-git cli or github.

For this tutorial, I will show you, how to upload through github. First of all you need to intialize git in the application directory. After you need to add and commit the files to git, as follows:

git init

git add .

git commit -m "uploading app to github"

Secondly you need to create a new repo in your github account. And push the code there. There will be instructions to guide you.

git remote add origin

git push -u origin master

Once your code pushed.

Connecting the code on github to Heroku

Login in to Heroku. And click on "Create new app".

Fill up the form, and submit it.

You just need to enter a unique name for your application.

Then in the deployment section, click on "Connect to Github".

If you have not linked Heroku to your Github, then you will be prompted to authorize Heroku to your github account.

Once that's done, you need to enter the name of the repository you created earlier.

And click on "Connect".

Then you need to choose a branch that you want to deploy.

Usually it will be the "master branch".

Then click on deploy.

Heroku will then start installing python and all the dependencies.

If everything goes ok, at the end you will see a url.

Copy and paste it in the browser.

Hopefully, you will see the application running.


Deployed a simple Flask based web application to heroku. We created a repository on github and pushed the application code there.Then we authorize our Github account with Heroku. Lastly we searched for the repository and deployed it on heroku.