In this part of the tutorial, you will deploy your flask web application. The process of deployment consists of transferring all your website files from your computer to the web server. Once you do that, your website can be visited by anyone through a public URL.
We will be using Heroku which is a cloud platform that supports Python web applications built with various programming languages including applications built with Python flask. Heroku makes things easier by handling administrative tasks by itself so that you can focus on the programming part. Another good thing is that you can host your web applications for free. As you get more traffic later, you may want to sign up for one of the better plans so that your web application performs well in high traffic. You can also have your own free subdomain on top of herokuapp.com or use your own domain if you have purchased one. I will use the domain pythonflasktutorial.herokuapp.com for the website that I built throughout this tutorial.
So, let’s go and publish our website online. Here are the steps:
- We will be using the git tool to send our local files to the online webserver or cloud if you like. So, the first thing to do is to download git from https://git-scm.com/downloads and install it. The process should be self explanatory.
- Sign up for a free account on Heroku: heroku.com.
- Download and install Heroku Toolbelt from https://toolbelt.heroku.com/.
Heroku Toolbelt is a package that allows you to interact with the Heroku cloud through your computer command line and it needs git to do that. You already installed git in step 1.
- Start interacting with your Heroku account through the command line. Open your command line while you are inside the myblog folder and type:
Enter your heroku account credentials when asked about them.
- Heroku doesn’t have a webserver. Instead, it expects the application to use its own webserver. A Python webserver to be used is gunicorn.
Gunicorn comes as a Python library, so you need to install it with pip. For that, in the command line, type:
virtual\Scripts\pip install gunicorn
- Create an empty file named Procfile in your current folder.
Then enter this line inside the empty file:
web: gunicorn app.hello:appThe file shouldn’t have any extension, so make sure the file name is not getting a .txt extension.
- Create a requirements.txt file by typing:
virtual\Scripts\pip freeze > requirements.txt
That will generate a list of the Python libraries that are installed in your virtual environment and write the list inside the requirements.txt file. That file will then be sent and read by the webserver so that the webserver knows what libraries to install so that application runs correctly.
- Great! Now, Heroku may run Python 2 by default for the applications that are sent to it. Therefore, it would be a good idea to declare what Python version your app has been designed to work with. I built my app using Python 3.5.1. To do that, you need to create a runtime.txt file and insert the this line in there:
That will tell Heroku what Python to use when running your app.
With the two files (requirements.txt and Procfile) you created, you have now prepared all your files for deployment. Your directory tree should look like this now:
As you see, Procfile and requirements.txt are under myblog.
The next steps consist of creating an empty app on heroku and sending our local files to that app. And we will be sending the files using git. Let’s move on.
- Before using the git commands to send our files to Heroku, you need to tell git who you are. To do that, type this in the command line:
git config --global user.email “email@example.com”
Press enter and then type:
git config --global user.name "Your Name"
Make sure to replace your email address and your name appropriately in those lines keeping the double quotes.
- Create a local git repository by typing:
Add all your local files to the online repository by typing:
git add .
Make sure to include the dot after add. The dot means you are adding the entire directory to the repository.
- Commit your files with:
git commit –m “First commit”
- Create an empty Heroku app:
heroku create pythonflasktutorial
I chose pythonflasktutorial as the name for my app. Make sure you pick your own name. Once the command is executed successfully, you can see that your app has been created in your Heroku account under the Personal Apps menu.
- And the final step! Let’s push our application to Heroku:
git push heroku master
- Lastly, make sure at least one instance of the app is running with:
heroku ps:scale web=1
- If the amazing happened and you got here with no errors, your website should be now live. Mine is at live pythonflasktutorial.heroku.com