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.
flask requests gunicorn
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
0.0.0.0:5000 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 0.0.0.0:$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 https://cryptic-reef-51053.herokuapp.com/ | https://git.heroku.com/crypt ic-reef-51053.git (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 server.py (env) $ git remote add heroku https://git.heroku.com/cryptic-reef-51053.git (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: remote: -----> Python app detected remote: -----> Installing python-3.6.10 remote: -----> Installing pip ... remote: -----> Discovering process types remote: Procfile declares types -> web remote: remote: -----> Compressing... remote: Done: 45.9M remote: -----> Launching... remote: Released v3 remote: https://cryptic-reef-51053.herokuapp.com/ deployed to Heroku remote: remote: Verifying deploy... done. To https://git.heroku.com/cryptic-reef-51053.git * [new branch] master -> master
You'll get a URL back which now your own web application serving 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.