Deploying Flask App on Heroku:
By: Pratik Mane
Tags: Webapplicationdevelopment, Heroku, Flask, Python3

Introduction

I was working on project with my friend. The project was webapp which gets mcq/trueflase questions from user and generates html code for form with questions. In this tutorial i am going to show you how to deploy a Flask application to Heroku from my experience. The app can be as simple as a "Hello World" app to a social media monitoring platform!. But for convenience we are going to make simple flask app with few lines which returns some message, but you can make your own.

Create Simple Flask App:

For simplicity we will create simple webApp which will show some message.

In your project directory, let’s start off by creating a virtualenv:

$ python -m venv venv/

And let’s activate it with the source command:

$ source venv/bin/activate

Then, let’s use pip to install the libraries we’re going to use - flask to build the app and gunicorn as our server:

$ pip install flask

$ pip install gunicorn

Our application is going to be a simple app returns a welcome message.

app.py

# app.py

from flask import Flask

app = Flask(__name__)

# A welcome message

@app.route(’/’)

def index():

return "<h1>Welcome to server !!</h1>"

if__name__ == '__main__’:

app.run(debug=True)

To test your application locally, run script app.py and open http://127.0.0.1:5000/ in browser. If everything is fine, we should be greeted with a welcome message.

After development and testing our app is ready to deploy. Before deploying app on heroku we have to understand requirements.

Heroku

Heroku is one of the first cloud platform as a service (PaaS) and used to deploy manage and scale Apps. It supports several languages - Ruby, Java, Node.js, Scala, Clojure, Python, PHP, and Go.

Requirements:

Before deploying Flask app on heroku we need to make two files as :

requirements.txt

Procfile

File requirements.txt define which libraries our application uses. That way, Heroku knows which ones to provide.

We need to create a requirements.txt file with all of the libraries we are using:

$ pip freeze > requirements.txt

Before running above command make sure your virtual environment is active.

This way we end up with a requirements.txt file that contains the libraries we’re using and their versions:

requirements.txt

Click==7.0

Flask==1.1.1

gunicorn==19.9.0

itsdangerous==1.1.0

Jinja2==2.10.1

MarkupSafe==1.1.1

Werkzeug==0.15.6

For Heroku to be able to run our application like it should, we need to define a set of processes/commands that it should run beforehand. These commands are located in the Procfile.

Procfile

web: gunicorn app:app

The web command tells Heroku to start a web server for the application, using gunicorn. Since our application is called app.py, we’ve set the app name to be app as well.

Setup heroku account:

Now, we should create a Heroku account. After creating account we will see dashboard.Create new app with dashboard, select New -> Create new app. Choose a name for the application and choose a region of where you’d like to host it.

Once the application is created on Heroku, we’re ready to deploy it.

Deploying Flask App on Heroku:

To deploy flask app on heroku we need to install heroku-cli on machine. To install heroku cli you can refer : https://devcenter.heroku.com/articles/heroku-cli

To upload our code, we’ll use Git. First, let’s make a git repository:

$ git init

And now, let’s add our files and commit:

$ git add . $ git commit -m "first commit"

Let’s login to our account using our credentials by running the command:

$ heroku login

At this point, while logged in, we should add our repository to the remote one:

$ heroku git:remote -a {app-name}

Be sure to replace {app-name} with the actual name of our app you selected in the earlier step.

And with that done, let’s upload the project by pushing it to Heroku:

$ git push heroku master

This Command will push our app to heroku. And now your application is deployed.

Congratulations we uploaded our first app to heroku !!!

You can check your app at https://{app-name}.herokuapp.com