# How to Deploy Flask apps for Free with Heroku

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.


![website.jpg](https://cdn.hashnode.com/res/hashnode/image/upload/v1598857925804/n7f79Se7D.jpeg)

There are a few options you have like  [PythonAnyWhere](https://www.pythonanywhere.com/)  or Nitrious.io but both of them aren't that reliable and easy. That's where  [Heroku](heroku.com)  comes in.
## What's Heroku?

 [
![heroku-logo.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1598854899836/BHP66bhCC.png)](https://devquoteflask.herokuapp.com/) 

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.

### Advantages of Heroku
- Deployable using Git
- Free of Cost with 450 Hours of runtime 

## 🛠️Tools needed on your machine

Before we begin you need to install these on to your windows/mac machine with the Gunicorn Pip Package.

- **[Github](https://desktop.github.com/)**
- **[Heroku CLI](https://devcenter.heroku.com/articles/heroku-cli#download-and-install)**
- **[Gunicorn ](https://pypi.org/project/gunicorn/)  **

## 💻Setting up Heroku


![hash3.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1598854912425/iXKYSIJB2.png)

Now that you have the essential tools installed on your PC/laptop, Go to [Heroku Signup](https://signup.heroku.com/login) 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

```bash
heroku login
```

If everything is good you will see something like this


![hash2.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1598858392095/kdsXnmzah.png)

## 🦄 Three important files for Heroku 

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

- The procfile
- Requieremnet.txt
- runtime.txt

![hash4.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1598858439864/C2_SYwuv0.png)
### The Proc File

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.](https://devcenter.heroku.com/articles/procfile)

```bash
web: gunicorn app:app
```

### Requirement.txt

This file contains all the packages which are imported in the  [app.py](https://blog.hrithwik.me/deploy-flask-apps-for-free-using-heroku)  which needs to be installed. You can generate this using [pipreqs](https://pypi.org/project/pipreqs/) utility or using the command "pip freeze". Also don't forget to add jinja2 and gunicorn inside requirements.txt file.

```jsx
Flask_Compress==1.4.0
Flask==1.1.0
Flask_SSLify==0.1.5
requests==2.20.1
gunicorn
Jinja2
gevent


```
### Runtime.txt

```bash
python-3.7.5
```


## 🚀 Deploying to Heroku

```bash
heroku create (your app name)
```

This would create a git repository and a Heroku app with the unique link generated. 

```bash
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.

```bash
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](https://www.freecodecamp.org/news/how-to-deploy-a-nodejs-app-to-heroku-from-github-without-installing-heroku-on-your-machine-433bec770efe/)  to connect your Github Repository to Heroku.

## 🌐 My Single Page Flask app
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/](http://devquoteflask.herokuapp.com/).


[![](https://github-readme-stats.vercel.app/api/pin/?username=hrithwikbharadwaj&repo=BasicFlaskQuoteApp)](https://github.com/hrithwikbharadwaj/BasicFlaskQuoteApp) 
Refer the full source code in case you have any doubt with the files 

## 😮 What's the Catch?

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](https://twitter.com/HSBTechYt)  and  [Linkedin ](https://www.linkedin.com/in/hrithwik-bharadwaj-a77810150/) to stay connected.

%[https://twitter.com/HSBTechYt/status/1300411279517990915]
 

Also, Check out my previous article on [How I Created a MEME Bot using Python ](https://blog.hrithwik.me/how-i-created-a-python-meme-bot-for-instagram) 









