Python + Flask (from Windows WSL) on Heroku.
roadpilot
Posted on August 9, 2021
This week I was working on a "hackathon" project that involved an application written in Python and served through Flask. I decided for testing that I would host it on Heroku. Here's what I had to do in order to get it up and runnning.
Following the steps in https://devcenter.heroku.com/articles/getting-started-with-python, revealed to me that there were no specific steps for using Flask. So going through the steps, I'll talk about where you diverge - specifically as it pertains to making this happen on Windows (WSL). ***These steps will not make the app able to run locally, this will only configure the Heroku deployment to run on Heroku.
- Installing the Heroku CLI: In Windows WSL, if you are using Ubuntu for your Linux OS, you would follow the Ubuntu instructions:
curl https://cli-assets.heroku.com/install.sh | sh
Once installed, you can use the heroku command from your command shell (you might need to close and reopen your terminal)
heroku login
heroku: Press any key to open up the browser to login or q to exit
› Warning: If browser does not open, visit
› https://cli-auth.heroku.com/auth/browser/***
heroku: Waiting for login...
Logging in... done
Logged in as me@example.com
(all of these commands were taken from the original Heroku devcenter article)
You have now installed the Heroku CLI
- Clone the Heroku sample Python app (it is a Django app, but we'll change that later)
git clone https://github.com/heroku/python-getting-started.git
cd python-getting-started
- Deploy the sample app
heroku create
Creating app... done, ⬢ serene-caverns-82714
git push heroku main
...lots of stuff here, ultimately...
remote: Verifying deploy... done.
Make one instance of the app to run
heroku ps:scale web=1
And view it in your web browser
heroku open
(If that doesn't work, just copy the URL that is generated during the 'git push heroku main' step)
- You have deployed the sample (Django) app. Now let's make some changes to make a Flask app.
When you cloned the original git sample repo, four of the files cloned are where we are going to make our changes:
procfile
web: gunicorn app:app
requirements.txt
gunicorn
flask
runtime.txt
python-3.9.6
app.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "<p>Hello, World!</p>"
- Now one more round of
git push heroku main
and Heroku will take it from there. Heroku reads the 'requirements.txt' file and knows to change the existing (remote) installation from Django to Flask. You can now write whatever Python you want and it will serve through Heroku as a Flask app.
Posted on August 9, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.