Deploy your Flask web-app to Heroku

First things first! If you are here, it is obligatory for you to know what flask is. In case you don’t, let me brief it to you.

What is Flask?

Flask is a micro web framework written in Python. It provides you with numerous tools, libraries, and technologies to help you build your web application. Flask might look quite similar to that of NodeJs in terms of usage, but flask is very lightweight, with a lot of inbuilt libraries! At the same time, numerous plugins are to be added and updated frequently.

Have your flask web-app built?

Excellent! Only developers can understand the happiness and satisfaction one gets after completing a long-running project. But wait, are you not able to share your project with others? Is your project only running on YOUR localhost? Don’t worry! We do have numerous cloud platforms that would help you to deploy your web application so that everyone around the globe can see and appreciate your efforts! Let us see how…

Deploy your flask web-app to heroku

If your project is on flask, I assume that you have your virtual environment and flask installed in the directory. If you don’t know how to set a virtual environment, follow the below steps:

  1. Type in virtualenv env
  2. NOTE- If any error is detected as “virtual environment not recognized”, then type
  3. pip install virtualenv
  4. Activate the virtual environment by typing the following.
  5. env\Scripts\activate
  6. Lastly, install flask in here and then continue with your project.
  7. pip install flask

In your flask project, there are certain things that you should have in your local directory in which your flask web application resides. The first is the initializing python file, which has the __main__ command through which you run the app. For example, the name of this python file is “run.py”. Your run.py should look somewhat like this.

run.py file in your flask app. flaskblog is folder that has all the templates and static files.

Flask web app is working fine on your localhost? Cool, only a few steps more, and your work is online!

  1. Firstly create an account on Heroku. Heroku is a cloud platform that helps you deploy various applications and supports many languages like python, node, ruby, java, etc.
  2. Once your account is created, on the home page, at the top-right, click on New> Create a new app
  3. Provide them your app name (this will be the name that will be seen once you deploy it to Heroku, for example, https://{appname}.herokuapp.com)
  4. Now inside the app dashboard, you will be given a link to install Heroku CLI. This will help you access the Heroku with your command prompt! interesting right? install it!
  5. Once you are done with the installing of Heroku CLI, open your code-editor (maybe Visual Studio Code or Sublime Text). Open the terminal there. Go to your local directory using the cd command. Once you are there type “heroku login”. You have to enter your email id and then you would be redirected to chrome and click login.
  6. All your files are inside your system, then how will Heroku deploy it? The answer is, initialize your directory as an empty git repository and then connect it with the repo you just created in Heroku ( app name ). In your terminal type-
myproject is your local directory. git init will create the empty repository. Enter your app name in { }

7. Ok! so now you are almost there. Will only pushing the files to the repository be enough? Remember how you ran the flask app on localhost? U typed python run.py! So now we need to tell this to Heroku. The way it is done most of the time is by creating a file in your main directory name “Procfile” (no extension). This will tell Heroku which file to run. Here the file is run.py.

8. To do this install gunicorn. type in pip install gunicorn in the terminal. Now your Procfile should have this content.

run is the name of the file that has the run() command, and app is the name of the function that you import in run.py.

9. There are certain libraries that you installed for your flask app. You need a file that would contain all these. Type into your terminal “pip freeze > requirements.txt”. This will create a file named requirement.txt in your local directory and it will have all the libraries necessary.

This is how your requirements.txt would look like

10. Wow! You are done with 85% of the work! Whatever you have done should now be pushed into the Heroku repository. so type in the 3 lines in the terminal

These 3 lines will add the content to local repository, and commit all the changes. Finally will push these files to heroku master.

This will take some time on the flask app. If everything goes fine, then you will be provided with a Heroku link like

https://{app-name}.herokuapp.com.

At the same time, some of you might get an error will compiling the requirements.txt file stating that a particular library is not found. In that case, remove that particular library from the requirements.txt file, and run the above 3 lines again.

Congratulations!!! Finally, you have your app on Heroku which you can share with anyone you wish to. ( incase if certain issues regarding Heroku persist, type in Heroku logs — -tail to find out those errors. If solving these don’t help, type Heroku restart and you should be good to go!

Frontend Web Developer and Competitive Coder!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store