How to move your Flask app from the local machine to the Heroku cloud
Tags: python, flask, heroku, beginners
Published: 2019-11-21 11:40:55

---
title: How to move your Flask app from the local machine to the Heroku cloud
published: true
series: Starter tutorials reg. websites, hosting, and techdocs
description: The Unofficial Getting-Started Guide. For newbies learning to code. The barest minimum files needed for a Python web app on Heroku.
tags: python, flask, heroku, beginner
---

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_ :unamused:

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 :sweat: 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... :disappointed: _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 :tada:

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:
```python
        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.

:walking: 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](https://github.com/AninditaBasu/flask-heroku-starter-pack). And, here's the [Heroku app](https://flask-heroku-starter-app.herokuapp.com/).