Getting Started with Python Flask and Heroku

In this article I will show you how to quickly get started creating your first Python Flask Application and deploying it to Heroku. The instructions use Python 3, Heroku CLI, and Git as well as the terminal.

Create a Python Virtual Environment and Install Flask

I wrote an article showing how to create a Python 3 virtual environment for all your Python apps. Let’s do that now. Create a new directory for the Flask Application, change into it, and create a new virtual environment. Install Flask after you activate the environment.

$ mkdir quick-flask && cd $_
$ python -m venv env
$ source env/bin/activate
$ (env) pip install flask

Create and Run the Python Flask Application

Using your favorite code editor, create a simple Flask Application and save it to the directory as app.py.

from flask import Flask


app = Flask(__name__)

@app.route('/', methods=['GET'])
def index():
    return '<h1>Python Flask and Heroku</h1>'


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

This Python code creates a new Flask Application and creates a default route that displays Python Flask and Heroku to the browser.

Go ahead and run the web application from the terminal and display it in the browser.

$ (env) python app.py
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 254-980-658

Open your browser to http://127.0.0.1:5000 to view the Flask Website. Press CTRL+C to quit the Flask Web Server when you are done viewing the website.

Run Python Flask Website Using Gunicorn

We have the website running locally using Flask’s development server, but let’s run it using Gunicorn, which we will use to run it on Heroku.

$ (env) pip install gunicorn
$ (env) gunicorn app:app
[INFO] Starting gunicorn 19.7.1
[INFO] Listening at: http://127.0.0.1:8000 (47944)
[INFO] Using worker: sync
[INFO] Booting worker with pid: 47947

Now you can view the Flask Website by pointing your browser to http://127.0.0.1:8000. Press CTRL+C to quit Gunicorn when you are done viewing the website.

Save Your Python Dependencies using Pip Freeze

Heroku will need to know your website is built using Python and has dependencies of Flask and Gunicorn. We can save these dependencies to a requirements.txt file using pip freeze. Heroku will see this file and realize we are using Python for the application and install the dependencies.

$ (env) pip freeze > requirements.txt

Create Heroku Procfile to run Python Flask App using Gunicorn

Heroku uses a Procfile to determine the type of application you are deploying as well as how to run it.

Create a Procfile that let’s Heroku know this is a web application and to run it using Gunicorn.

$ (env) echo "web gunicorn app:app" > Procfile

Create a Git Repository

You use git to deploy your application to Heroku so you need to initialize the directory as a git repository.

$ (env) git init
Initialized empty Git repository ...

Git uses a .gitignore file to determine files and directories to ignore. At a minimum we need to ignore the python virtual environment and cache. You may have some additional directories or files created by your editor. Create a .gitignore file to ignore certain files and directories.

$ (env) touch .gitignore

I added 3 directories to my .gitignore file.

__pycache__/
env/
.vscode/

Now you can commit the Flask application to the git repository.

$ (env) git add -A
$ (env) git commit -m "Initial commit."

We are now ready to deploy our Flask Web Application to Heroku.

Deploy Flask Web Application to Heroku

Let’s deploy our web application to Heroku using its CLI. When you git push the Flask Website to Heroku you will see that it detects the application to be a web process using Python 3 and installs the dependencies. It then runs the website using Gunicorn per the Procfile.

$ (env) heroku create
$ (env) git push heroku master
$ (env) heroku ps:scale web=1
$ (env) heroku open

Your default browser should open up and display the website.

Make sure you destroy the Flask Website when you are done viewing it. Your Flask Application was given a name so you need to specify its name when destroying the app.

$ (env) heroku apps:destroy [name] --confirm [name]

Conclusion

I’ll write Python Flask tutorials in the future that dive into more depth, but this is a good way to get started with Python, Flask, and Heroku for developing and deploying web applications.

Contents