Deploying to Production

Deploying our Stock Quote application using Heroku

Deploying to production

Now that we have an application that works, we can put it up on the internet. This book will cover a few different deployment methods later on, but for now, we're going to use Heroku, a commonly used Platform as a Service host were we can directly upload our application to take care of a lot of the behind the scenes infrastructure level work.

We're going to need to tell Heroku what command to run our Flask application. To date, we've been using the bundled server that comes with Flask to view the application within our own browser, however using it to serve real traffic is discouraged. What we'll do instead is use a production grade server library to serve our application (which Heroku will route end users to).

Herkou is going to look at our requirements.txt file to see what libraries to install, so we'll need to add our server library called gunicorn to this file.


Then we're going to need to tell Heroku how to run our web application. To use gunicorn, we need to point it to the python file that hosts our Flask application and what variable references the Flask application object.

In addition, we'll want to tell it to listen on the IP address and port which means that it'll be listening and respond on port 5000. Heroku uses that port by default to talk to our application.

Heroku uses a file called a Procfile (which is short for Process File) and we can define commands under each kind of "worker". To serve a web application, we'll want to define a "web" worker with the gunicorn command. The $PORT environment variable will be specified by Heroku and is what it'll be expecting that our Flask application is serving on.

web: gunicorn --bind$PORT server:app --log-level "debug"

To install the Heroku command line interface, you can go to the download page available at the Heroku website and walk through the installer for your operating system. The full setup instructions for Python applications is available at Heroku's Pytho n Documentation

After following the Heroku CLI setup, the next step to do deploy is run heroku create within our application folder and then push to a git repository hosted on Heroku.

(env) $ heroku create
Creating app... done, ⬢ cryptic-reef-51053 |
(env) $ git init .
Initialized empty Git repository in [your folder]/.git/
(env) $ git add .
(env) $ git commit -m 'Add untracked files'
[master (root-commit) 109b1d4] Add untracked files
 6 files changed, 47 insertions(+)
 create mode 100644 Procfile
 create mode 100644 requirements.txt
 create mode 100644
(env) $ git remote add heroku
(env) $ git push heroku master
Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 4 threads
Compressing objects: 100% (7/7), done.
Writing objects: 100% (10/10), 1.79 KiB | 917.00 KiB/s, done.
Total 10 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Compressing source files... done.
remote: Building source:
remote: -----> Python app detected
remote: -----> Installing python-3.6.10
remote: -----> Installing pip
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote: -----> Compressing...
remote:        Done: 45.9M
remote: -----> Launching...
remote:        Released v3
remote: deployed to Heroku
remote: Verifying deploy... done.
 * [new branch]      master -> master

You'll get a URL back which now your own web application serving live on the internet!

Live on the internet
Live on the internet

In the next few chapters, we're going to extend this application to support more pages and learn more about Flask on the way.

Start a new discussion. All notification go to the author.