How to Deploy a Flask App to Heroku
Hi Guys, see me again in this article. Now I try to make an article about how to deploy our flask application to Heroku. How to do that? Easy. Let’s read the article until it runs out. Before doing this tutorial, make sure you have a flask project ready to use.
Let’s Get Started…
STEP 1: Create a New Repository on GitHub
To create a new repository very easy, click on the profile picture then select “your repository”, then follow the following picture.
- Enter the repository name
- Add the gitignore, search and select “python”
STEP 2: Create the Project on Pycharm
Open Pycharm then select “Get from Version Control”, then paste the url in the cloned Github project.
Wait for it to finish then select “yes” if asked to access the directory.
STEP 3: Open File Manager
Move or copy all the contents of the flask project folder to the project folder from the results of the cloned GitHub project.
Then, open the PyCharm again, then we will be in our flask project. Then, use the virtual environment that has a flask installed.
STEP 4: Create “requirements.txt” and Procfile File
The way to create a “requirement.txt” file is very simple, type the following code in the terminal.
pip freeze > requirements.txt
After having the requirements.txt file, then we need to create a “Procfile” file so that Heroku can run our flask project. The way to do this is to press right-click on our project directory then select “New” then select “ File” and name it “Procfile”. After that, type this code in the “Procfile”.
web: gunicorn application:app
STEP 5: Commit and Push
After we are ready with our code, then we commit and push to our GitHub project. Press “CTRL + K” to commit the code. After that, enter the author (your name and email), and commit message then click the commit button.
After we successfully commit our code, then we push the code by pressing the “CTRL + SHIFT + K” button on your keyboard.
Wait until the push process is complete.
STEP 6: Log In to Heroku Account
Next, we will go to the Heroku website using our browser and log in there. if you don’t have a Heroku account, please register first.
After we press the “Log In” button, then we will be directed to our account homepage.
STEP 7: Create a New App
To create a new Heroku app, follow the following picture
Then, we give a name to our application. The name must start with a lowercase letter. Then, click “Create app”
STEP 8: Connect the GitHub Account
After that, we chose the deployment method with GitHub, connect your GitHub account which has the created flask application.
- The deployment method area
- Connect to your GitHub account
- Search the repository name
- Click “Connect” to connecting the application to Heroku
STEP 9: Deploy the Flask Application
After we connect Heroku with our GitHub account, scroll down then select “Enable Automatic Deploys” so that every time there is a code change in our project, automatically Heroku will also update the changes according to the latest code version of our project on GitHub. Then, click “Deploy Branch”.
After we click “Deploy Branch”, Heroku will install the library that reads in the “requirements.txt” file.
wait until the installation process is complete. After the library is successfully installed, Heroku will display a “view” button to see the results of the deployment of our application.
The result of the deployment:
Thank you for those of you who have read this article from beginning to end. Sorry, there are still many mistakes in writing this article. For this reason, the authors highly expect your criticisms and suggestions to become evaluations of learning for the next article. Once again, Thank you very much.
So, what should you do after knowing how to do web scraping using python? Start coding, start practicing.