How to deploy a Flask App using Heroku?

Shubhayan S
5 min readApr 1, 2020

This article is for all the developers wondering how to host a flask app without spending money on buying a server and domain. This blog will guide you through the process. Follow it carefully and you will get your app up and running in no time.

Pre-requisites: a free Heroku account, Python 3.7 installed locally.

Step 1: Getting started with Heroku

First things first, create a free account in Heroku by heading to their official website https://www.heroku.com .

Download Heroku CLI for your system by going to their website https://devcenter.heroku.com/articles/getting-started-with-python#set-up

Note: Dont forget to add heroku to the path for while installing for windows user.

Step 2: Setting up Flask App for Heroku

Open up your favourite text editor preferably Visual Studio and on the root of your project file create a new file named as “ Procfile ”.

After creating the Procfile type “ web: gunicorn flaskblog:app ”. Here it means we are defining “web” means a webapp and we will use “gunicorn” for it. Then we specify the main file of python for me it was flaskblog.py hence the name “flaskblog” and “app” as it starts with app function.

Step 3: Setting up your local machine for Heroku

Open your command prompt with file location at the root of our project. Type the following commands.

virtualenv env

Note: If any error is detected as virtual environment not recognized, type
pip install virtualenv

Activate virtual environment by typing the following.

env\Scripts\activate

The virtual environment needs to be setup by doing the same initial steps.

pip install flask

pip install gunicorn

For those wondering about Gunicorn it is a Python-based https server for UNIX.

Step 4: Setting up the app on Heroku

Continuing from the previous step. Now, our next step is to login to Heroku from CLI.

heroku login

You will be prompted to press a key to continue. After pressing a key browser will open and login. Congratulations, your local machine is now setup with heroku.

The next step is to create an app in Heroku by typing the following.

heroku create

You will see your app name after the app is created e.g serene-caverns-82714

You can check your app by going to the url below and you can see something like this.

Since Heroku is based on Github, we need to perform the same step here as pushing a commit in Github.

git init

git add .

git commit -m “first commit”

Disclaimer: For those of you who do not know about Github you can head to my previous post about installing Gitbash and learning more.

https://medium.com/@shubhayan1998/how-to-deploy-a-website-using-github-pages-2669e4f638ad

Step 5: Deploying your app on Heroku

Before deploying the app to Heroku most important thing is having two files — Procfile and requirements.txt . We already created a Procfile now we need to create the other file by typing the command below

pip freeze > requirements.txt

You will see something like this. The requirements.txt tells heroku what dependency it needs to have and what type of app it is Python, React etc.

Also, don't forget to specify Heroku not to work on the virtual environment “env” we created for pushing the app. We can do this by creating a file “.gitignore” and specifying “env” which should not be taken into account.

Final step, type the command in cmd to push the app.

git push heroku master

Refresh the link of the app created in step 4 to see changes of the app or type the command below.

heroku open

Step 6: Scaling up your app on Heroku

Your application is used on a free dyno by default. After half an hour of idleness, free dynos will sleep (assuming they are not trafficked). This causes the initial request to be delayed by a few seconds on awakening. Subsequent requests are usually executed. Free dynos also use free dyno hours on a monthly basis, provided that the quota is not fully implemented, all free applications are allowed to continue.

You can upgrade to an amateur or professional dyno type to avoid dyno slumber, as explained in the article under Dyno Types. For example, by editing a script that requires Heroku to execute a certain number of dynos, you may quickly expand your app to a professional dyno, each executing the web process type.

The application scale of Heroku correlates to the changes in the number of dynos running. Scale the web dynos number to nil:

heroku ps:scale web=0

Go back to the web tab to refresh the app or launch the app in the web tab to open it. You will receive a notice of error since you have no web dynos to fulfil requests anymore.

heroku ps:scale web=1

Congratulations, you have successfully deployed your website on Heroku and you can view it from anywhere using the link given. But since its a free account Heroku can only handle 50–100 requests at a time. You can even buy server space to scale more and get more dynos.

In case of any issue, you can write in the comments section and if you like my article do not forget to give me claps :) My Github Username is ShubhayanS and you can even reach me on LinkedIn as Shubhayan Saha.

--

--

Shubhayan S

People say ML and AI is the future of our generation but I believe it's much more than that. Well, I plan on integrating ML, Web and AI in the future.