DEV Community

Trilochan Parida
Trilochan Parida

Posted on • Edited on

How to Deploy a Flask app on Heroku.

Hello Friends, In this article I will show you how to deploy a flask app on Heroku. Follow the below steps to deploy your python flask app on Heroku.

Prerequisite:

  1. You must have installed Git in your system.
  2. You must have installed Python in your system.

Step-1: Install Heroku CLI

% brew tap heroku/brew && brew install heroku
Enter fullscreen mode Exit fullscreen mode

Above command is for Mac, for other systems you can click here

Step-2: Create Python Virtual Environment

% python3 -m venv foldername
% source foldername/bin/activate
% cd foldername
Enter fullscreen mode Exit fullscreen mode

Step-3: Install Flask & Gunicorn

% pip3 install flask gunicorn
Enter fullscreen mode Exit fullscreen mode

Step-4: Create an app folder and simple python app

% mkdir app
% cd app
% vi main.py
Enter fullscreen mode Exit fullscreen mode

main.py

from flask import Flask
app= Flask(__name__)
@app.route('/')
def index():
  return "<h1>Welcome to CodingX</h1>"
Enter fullscreen mode Exit fullscreen mode

Step-5: Create an entry point to the application, wsgi.py

% cd ../
% vi wsgi.py
Enter fullscreen mode Exit fullscreen mode

wsgi.py

from app.main import app
if __name__ == "__main__":
  app.run()
Enter fullscreen mode Exit fullscreen mode

Step-6: Run the application in your local system

% python wsgi.py
Enter fullscreen mode Exit fullscreen mode

Step-7: Create requirements.txt and Procfile file

% pip3 freeze
% pip3 freeze > requirements.txt
% vi Procfile
Enter fullscreen mode Exit fullscreen mode

Procfile

web: gunicorn wsgi:app
Enter fullscreen mode Exit fullscreen mode

Step-8: Create an app in Heroku

Heroku new app

Step-9: Deploy your app to heroku

% heroku login
% git init
% heroku git:remote -a codingx-python
% git add.
% git commit -am "First python app"
% git push heroku master
Enter fullscreen mode Exit fullscreen mode

Step-10: Open your application on browser

https://app-name.herokuapp.com

It's done!

Please Subscribe to my Youtube Channel https://youtube.com/c/codingx

Top comments (16)

Collapse
 
jnshrink profile image
Jon Nordland

Thank you, Very good. More people should focus on succinct explanations like this.

Collapse
 
techparida profile image
Trilochan Parida

I am glad you like it :)

Collapse
 
kirtan0000 profile image
Kirtan Magan

Thanks omg you are thebest =ever!!!!1!!!!

Collapse
 
thms profile image
Thomas Rettig

OMG!!! THANK YOU SO MUCH!!! I was spending the past few days struggling to deploy my Flask app to Heroku, and then I stumbled across your article. After a few modifications, the site is now in production! :D

Collapse
 
coderoftheworld profile image
The Phantom Coder

Thanks, this was quick and easy and it worked perfectly for me.

Collapse
 
lawrenebrown profile image
Edung Divinefavour

Super useful!!!!!!!!!

Collapse
 
luv2bnanook44 profile image
Luv2bnanook44

thank you!!!

Collapse
 
pljay profile image
Pathum Lakshan

Neat work Thanks lot

Collapse
 
billyndirangu profile image
billy_dev

I like the tutorial, Thank you.
My question is how do is set a port and a database that isn't a local storage on like: sqlite, to using postgresql?

Collapse
 
pythondeveloper360 profile image
Hanzala Khan

Hey I have a simple endpoint on my Herokuapp bloggir.herokuapp.com/d and i want to fetch it with JS fetch API, it is giving an error.please help

Collapse
 
mohsinashraf profile image
Mohsin Ashraf

Thanks great tutorial.