Python + Flask (from Windows WSL) on Heroku.

roadpilot

roadpilot

Posted on August 9, 2021

Python + Flask (from Windows WSL) on Heroku.

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.

  1. 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

(all of these commands were taken from the original Heroku devcenter article)
You have now installed the Heroku CLI

  1. 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
Enter fullscreen mode Exit fullscreen mode
  1. Deploy the sample app
heroku create
Creating app... done, ⬢ serene-caverns-82714
Enter fullscreen mode Exit fullscreen mode
git push heroku main
...lots of stuff here, ultimately...
remote: Verifying deploy... done.
Enter fullscreen mode Exit fullscreen mode

Make one instance of the app to run

heroku ps:scale web=1
Enter fullscreen mode Exit fullscreen mode

And view it in your web browser

heroku open
Enter fullscreen mode Exit fullscreen mode

(If that doesn't work, just copy the URL that is generated during the 'git push heroku main' step)

  1. 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
Enter fullscreen mode Exit fullscreen mode

requirements.txt

    gunicorn
    flask
Enter fullscreen mode Exit fullscreen mode

runtime.txt

    python-3.9.6
Enter fullscreen mode Exit fullscreen mode

app.py

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"
Enter fullscreen mode Exit fullscreen mode
  1. Now one more round of
git push heroku main
Enter fullscreen mode Exit fullscreen mode

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.

💖 💪 🙅 🚩
roadpilot
roadpilot

Posted on August 9, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

What was your win this week?
weeklyretro What was your win this week?

November 29, 2024

Where GitOps Meets ClickOps
devops Where GitOps Meets ClickOps

November 29, 2024

How to Use KitOps with MLflow
beginners How to Use KitOps with MLflow

November 29, 2024

Modern C++ for LeetCode 🧑‍💻🚀
leetcode Modern C++ for LeetCode 🧑‍💻🚀

November 29, 2024