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 domain if you have purchased one. I will use the domain pythonflasktutorial.herokuapp.com for the website that I built throughout this tutorial. Note that if you have troubles deploying to Heroku you can try another cloud service which is PythonAnywhere. I have written a tutorial on how to deploy on PythonAnywhere.
So, let’s go and publish our website online on Heroku. Here are the steps:
heroku login
Enter your heroku account credentials when asked about them.
virtual\Scripts\pip install gunicorn
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 web server so that the web server knows what libraries to install so that application runs correctly.
git config --global user.email “[email protected]”
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.
git init
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.
git commit –m “First commit”
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.
git push heroku master
heroku ps:scale web=1
If Heroku doesn’t work, you can try PythonAnywhere using my PythonAnywhere tutorial
Next Lecture
Python Mega Course: Learn Python in 60 Days, Build 20 Apps
Learn Python on Udemy completely in 60 days or less by building 20 real-world applications from web development to data science.