How To Deploy Flask Application to Heroku

image from unsplash.com

hello my name is Feri Lukmansyah(Feri Lukmansyah), now we talk about python programming language, do you know python is a powerful, with python we can build web applications, desktop applications, android application and many others.

In this case we will talk about how to deploy flask application to Heroku, There are many ways to deploy this flask application like deploying in pythonanywhere.com and heroku.

In this blog I will discuss how to deploy a flask application in Heroku, This is a walkthrough that illustrates how to deploy a Flask application using heroku

Flask > Backend

Heroku > Hosting

Project Preparing

  • Create Heroku Account
  • Install Heroku CLI
  • Install Requirements Project
  • Configure Heroku Application
  • Deploy to Heroku

let’s start from little step, we will create Heroku account first, this is the step

Create Heroku Account

first, open your browser, visit https://www.heroku.com/ to create heroku account.

heroku home page

Choose SIGN UP FOR FREE to create a new account in heroku, after that, fill in the form to complete the requirements for creating an account on Heroku

form requirements to create heroku account

after that click CREATE FREE ACCOUNT to create account

verification email to confirm your heroku account

after confirm you will be setting your password for heroku account and proceed

email verification from heroku
setting account password
proceed page

after you press CLICK TO PROCEED you will be redirected to term of services press agree

term of services

after accept term of services you will redirect to heroku dashboard, in this case you have completed the steps to create an account on heroku, continue to another step

Installing Heroku CLI

in this case I will use Ubuntu as an example of installing heroku CLI, There are several different installation methods for the Heroku CLI application, I will install using curl, like this

$ curl https://cli-assets.heroku.com/install-ubuntu.sh | sh
heroku CLI installation

after installed login to heroku account using command prompt

heroku login
redirect page after you typing heroku login

a redirect page will appear, and press login

login success

heroku cli application setup success, continue to other step

Configure Flask Application On Heroku

  1. Install requirements

the first step is install a requirements packages for flask application, in this case we will create basic applications, so we need a flask and gunicorn package,we can install them with pip, following this commands

pip install flask gunicorn

after that create the requirements.txt file to list what packages will be installed following this command

pip freeze > requirements.txt

2. Create Procfile on a project Folder

Create a Procfile in the project root folder and add the following line

web: gunicorn app:app

The first app represents the name of the python file that runs your application or the name of the module it is in. The second var_app represents your app name. For example if your application runs from a file run.py that looks like this:

web gunicorn myapp:app

the application in code, like this

from flask import Flaskvar_app = Flask(__name__)
@var_app.route('/')
def hello_world(): return 'DEPLOY FLASK APP TO HEROKU'if __name__== '__main__': var_app.run()

then add the following line in your Procfile

web: gunicorn run:var_app

3. Create New Application Instance in Heroku Dashboard

to create app, click new then Create New App

button to create new app instance on heroku dashboard

fill in the application instance name form in the name form, then press Create app

create new app instance for flask project

Note: use a different app instance name

4. Initialize Flask Application Project with git

Initialize a git repository in a new or existing directory following this command

git init
Initialize a git repository

remote the instance with heroku Cli following this line:

heroku git:remote -a flaskapp-deploy

if use different app instance name, for example diff_name_app

heroku git:remote -a diff_name_app
remote git repository

5. Add Project To Git add Commit

after remote add project source code to git use this command

git add .
git commit -am "Initial Commit"
add project source code to git

6. Deploy And Push To Heroku Master Branch

Push the changes from your local master branch to heroku’s master branch.

git push heroku master

If you want to push from a different branch, say develop branch, you can use the following command

git push heroku develop:master
deploy to Heroku

after deploy success, click open app or open URL, example my project url: https://flaskapp-deploy.herokuapp.com/

flask application display on heroku

Conclusion

Excited With Python Linux, and Love Code, Writing and Many others

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store