How to Deploy Flask apps for Free with Heroku

Search for a command to run...

Amazing read 🤩
Thanks Rahul
I recently got into flask development and wow this is helpful. Thanks hrithwik. Gave you a follow
Linux Commands are like GTA Cheatcodes. People with great memory remember them by heart and weaker ones write them down. I am a hybrid, I used to remember most of the linux commands and GTA Cheatcodes but I don't have them byheart anymore Here is why...

Job-Stash is an alternative to node-schedule that persists job, supports atomic execution and has inbuilt retry mechanism

Here is how I wrote this blog post. I remembered things I contributed last year. made them into points and started expanding on how anyone can implement/learn that. Then realised the blog post is getting long :) Anyways, Hey everyone, I am Hrithwik, ...

I spent the last week improving the performance of a few APIs at work and improved response Times by 5X. I just did 3 basic things which improved performance in most of the API routes. This blog is really helpful especially if you are new to Javascri...

A few days back, I uninstalled the Twitter app and switched to the web app and I noticed something, Twitter had like a different/modern Installation UI for its PWA. (Know more about PWAs here.) I was like, Woah, this looks so dope and better than t...

Let's say you have built a flask app and want to deploy it for free. For example, Here is a single page flask app which I built few days back which displays random developer quotes.

There are a few options you have like PythonAnyWhere or Nitrious.io but both of them aren't that reliable and easy. That's where Heroku comes in.
Heroku is a container-based cloud Platform as a Service (PaaS). You can use Heroku to deploy, manage, and scale modern apps. You don't need to add credit card details to deploy apps here.
Before we begin you need to install these on to your windows/mac machine with the Gunicorn Pip Package.

Now that you have the essential tools installed on your PC/laptop, Go to Heroku Signup and fill in your details by confirming your email address.
Open the project folder and open command line terminal there and do the following commands
heroku login
If everything is good you will see something like this

Before deploying the flask app to Heroku you need to create these 3 files in your project.

In simple words, this file tells the Heroku machine what program to run by which dyno. In case you are interested in reading more about the Proc File check this article.
web: gunicorn app:app
This file contains all the packages which are imported in the app.py which needs to be installed. You can generate this using pipreqs utility or using the command "pip freeze". Also don't forget to add jinja2 and gunicorn inside requirements.txt file.
Flask_Compress==1.4.0
Flask==1.1.0
Flask_SSLify==0.1.5
requests==2.20.1
gunicorn
Jinja2
gevent
python-3.7.5
heroku create (your app name)
This would create a git repository and a Heroku app with the unique link generated.
D:\Deshik\HashNode\Developer Quotes>heroku create devquoteflask
Creating ⬢ devquoteflask... done
https://devquoteflask.herokuapp.com/ | https://git.heroku.com/devquoteflask.git
Now Heroku creates its own private git repo. You can either use it by typing the following command every time you add new changes to your project.
git init
git commit -m "commit"
git push heroku master
or you can even connect your GitHub repository so that every time you push something to your main GitHub repository, Any changes made to the app will automatically be reflected in the website. Follow this article to connect your Github Repository to Heroku.
Here is a simple Flask app I built for this article, Which Gives you new Programmers Quote every single time you refresh - http://devquoteflask.herokuapp.com/.
Refer the full source code in case you have any doubt with the files
The free tier of Heroku is actually pretty good. You are limited to 10k database rows and the dyno spools down when it hasn't been used in about 5 minutes.
It makes the initial page load quite slow and it won't be able to handle heavy traffic but it's a seriously generous package and great for development.
You are allowed to create only 5 projects with 450 hours of dynos per month without adding the credit card. By adding credit card you get extra 550 hours of dynos.
Now that I helped you save some cash for deployment. Follow me on Twitter and Linkedin to stay connected.
Also, Check out my previous article on How I Created a MEME Bot using Python