Deploying a Python App

In this simple tutorial, let's go through how we can deploy a Python application on Heroku with use of Heroku's Procfile

Set up Python App#

For this tutorial, I will use a sample python streamlit application - a mushroom classifier application

Streamlit is a popular python application for data science enthusiasts to host their datascience projects!

git clone https://github.com/realtime-apps-iap/mushroom-classifier-app
cd mushroom-classifier-app

This git repository is already initialised and tracked, so we don't have to git init it

Create Procfile#

Define a worker process - to start the website
Create a Procfile at the root of the repository (already created in the git repo)

web: sh setup.sh && streamlit run app.py

Over here, I ran sh setup.sh before running the streamlit application, for some configurations purpose.

You can add more worker processes if you want with nameOfProcess: commands

Adding Heroku#

  1. Create Heroku application
heroku create

To check if heroku is added to your git repository (you should see heroku)

git remote -v

To check the heroku apps you currently have

heroku apps
  1. Rename the created heroku application
heroku apps:rename <newname> --app <oldname>

When you first create the heroku app, a random name will be given, you will have to rename it yourself

OR if you already created a heroku application on the heroku website, you can link it directly

heroku git:remote -a <nameofherokuapp>

Test Heroku App#

Test the heroku application locally before we push it to heroku remote

heroku local web

This runs the web process (that was defined in Procfile) and checks it locally You can test and run multiple processes too

Open your localhost at specified port. This will be how it will behave on heroku web.

Deploying code#

  1. Push code to heroku
git push heroku main

main or master branch

  1. Open Heroku website!

Open the heroku website at < app name >.heroku.com!