DEV Community

Anindita Basu
Anindita Basu

Posted on • Updated on

How to move your Flask app from the local machine to the Heroku cloud

Why this post?

Because I could not find any resource that tells me how to deploy a Flask app to Heroku through the Heroku web interface. Through the web interface, not through the CLI 😒

Also because the official Heroku starter pack for Flask has way too many files; it intimidated me no end. Are so many files really needed, I asked myself 😓 And the Heroku documentation for that starter pack (and the documentation otherwise) assumes a level of knowledge that I do not have.

Also because the Flask app that ran perfectly on my laptop would not work on Heroku and none of the resources Google threw at me could tell me how to solve it. I wanted something beyond the hello world apps, something that took in a user input and returned a result. I went on to the 5th and 6th page of the search results, looking... 😞 Why isn't this part clearly documented anywhere, I asked myself.

So, this post.

This post is for people like me, people who are just learning to code, are not professional coders, and will not use the CLI until you make them an offer they can't refuse.

This post is for people who want minimal stuff on their laptops, and prefer to do things on the cloud through graphical user interfaces. UIs for the win 🎉

This post assumes you know the basics of Python (and Flask). That you already know how to get a Hello world app in Flask running on your local machine.

TL;DR

(i) In your Flask code, replace app.run() with host and port instructions.
(ii) In your repo, have a pip file to pip install the required packages at the Heroku end.
(iii) In your repo, also have Procfile and requirements.txt to specify a web server for the app.

What does the app do?

It takes a user input, processes it, and returns the results of that process. To do so, it uses the Flask framework of Python.

To test a Flask app locally, you use the app.run() command. By default, Flask starts a web server on the local machine and uses port 5000 to deploy the app.

To run the app on Heroku, three additional steps are needed:

  1. Heroku does not have a web server for Flask, so you need to tell it how to start a web server. This app uses gunicorn as HTTP server. So, for the app to run on Heroku, it needs a Procfile that contains the following command: web: gunicorn flaskStarter:app where web tells Heroku this is a web app, gunicorn tells it to use that server for app deployment, flaskStarter is the name of the Python program that contains the app code, and app is the name of the app routine inside the code.
  2. Heroku needs to install gunicorn before it compiles the app, and you tell it to do so through a file called Pipfile.
  3. Then, you need to modify your Python file to include the host and port info for app deployment. You do so by replacing app.run() with the following lines of code:
        port = int(os.environ.get("PORT", 5000))
        app.run(host='0.0.0.0', port=port)

Both Procfile and Pipfile must be named exactly so, and not have any file extension.

That's it. Now the app, which ran so beautifully on the local machine, will also run on Heroku.

🚶 Armed with this knowledge, I'll now fine-tune an app that I am building around an API and deploy it to Heroku for the world to use ...

If you'd like to get started with Heroku, I created a GitHub repo for the files you'd need: flask-heroku-starter-pack. And, here's the Heroku app.

Top comments (0)