How to Deploy Flask Web App Python 3 to Heroku Full Step by Step Tutorial For Beginners 2020

How to Deploy Flask Web App Python 3 to Heroku Full Step by Step Tutorial For Beginners 2020

 

Welcome folks today in this tutorial we will be looking at how to deploy flask app to heroku. All the steps will be listed below. A step by step youtube video will be also shown below.

 

 

 

 

 

 

Get Started

 

 

In order to get started you need to install the pipenv (Python virtual environment) which is responsible for managing your dependencies when you deploy to heroku

 

Install this package from pip like this

 

pip install pipenv

 

After this we can start our python virtual shell like this

 

pipenv shell

 

This will start the python virtual environment for you. Now you can install the packages for your application

For this demo purpose we will be developing a simple flask hello world app and will be deploying to heroku

 

Install these packages which are necessary for that

 

pip install flask gunicorn

 

flask will be the web server on which the app will run

And gunicorn is a special package which is necessary for deployment purpose to heroku

 

Now after installing it make a app.py file inside your directory and copy paste the following code to it

 

app.py

 

 

from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello World!'


if __name__ == '__main__':
    app.run()

 

 

 

Now we need to create a very important file inside our root directory. Make a Procfile inside your root directory and copy paste the following code

See also  Python Tkinter GUI Script to Make a GST Tax Finder Calculator Full Project For Beginners

 

 

web: gunicorn app:app

 

 

 

Basically here app refers to the filename of the application that you have used. In this case we have used app.py that is why we have put app here. If you name your file different change this value accordingly.

 

Now just go to command prompt execute this command to make the requirements.txt file of your application. From the name itself it will contain all the dependencies of your projecrt

 

pip freeze > requirements.txt

 

Just execute this and you will find your root directory will contain a requirements.txt like this

 

 

click==7.1.2
Flask==1.1.2
gunicorn==20.0.4
itsdangerous==1.1.0
Jinja2==2.11.2
MarkupSafe==1.1.1
Werkzeug==1.0.1

 

 

These are the packages which will get deployed to heroku with the app

 

Now we simply need to deploy this app to heroku. But for that we first of all need to push this to github. For this create a basic repository on github and after this come to command prompt and execute this commands which are given below

 

git init

 

git add .

 

git commit -m "first commit"

 

git remote add origin ###yourrepoaddress###

 

git push -u origin master

 

 

Now your app code is deployed to github. Now we simply need to log in to heroku and create a new project. And you can also do it through heroku-cli through these below commands

See also  Python 3 Script to Resize and Crop Image Using resize-and-crop Library Full Project For Beginners

 

heroku login

 

heroku create ###yourappname###

 

After this command executes your app will be created and now after this you need to go to your heroku dashboard and go to settings like this

 

And add a python buildpack to your project like this

 

 

 

 

 

 

 

 

 

 

 

 

 

And after this just execute this simple command to deploy your project to heroku

 

 

 

git push heroku master

 

 

 

 

 

 

 

 

Leave a Reply