Deploy Python Flask to Heroku

Download and install the Heroku CLI.

If you haven’t already, log in to your Heroku account and follow the prompts to create a new SSH public key.

$ heroku login

Add the following files to the application’s root folder:

Procfile

Heroku apps include a Procfile that specifies the commands that are executed by the app on startup.

Add the following line:

web: python run.py

Note: Change the run.py to your startup file.

Pipfile

Pipfile is used to specify the preferred python version and all the dependencies of the application.

Add the following lines:

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
flask = "*"
adaboost-model = "0.1.0"
[dev-packages][requires]
python_version = "3.7"

Note: Make sure to specify the correct python version and include all the required packages of you applicaiton.

Change the host of the flask application to this:

if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
application.run(host='0.0.0.0', port=port)

Generate the Pipfile.lock file using the following command:

$ pipenv lock

Note: Every time you have a changes in the Pipfile, you must run this command.

Create Heroku Application

  1. Login to Heroku site
  2. Go to Heroku dashboard
  3. Click New -> Create new app
  4. Enter the App name
  5. Click Create app button
  6. Go to Settings tab of you app
  7. Click the Add buildpack button
  8. Select buildpack (in this article, choose python)
  9. Click Save changes button

Deployment

Initialize a git repository in a new or existing directory

$ cd my-project/
$ git init
$ heroku git:remote -a app-name

Note: Change the app-name to your application name from Heroku.

Commit your code to the repository and deploy it to Heroku using Git.

$ git add .
$ git commit -am "make it better"
$ git push heroku master

Heroku will generate a new link for your application’s public access.

--

--

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