Deploy Python Flask App on Heroku
×


Deploy Python Flask App on Heroku

540

Introduction

Hosting your Python Flask application online makes it accessible to the world, and Heroku is a popular cloud platform that makes deployment incredibly straightforward. Whether you're building a personal project, a portfolio app, or a prototype, Heroku helps you launch your Flask application quickly without managing servers manually.

Prerequisites

Before getting started, ensure the following are installed:

  • Python and Flask installed locally
  • Git installed on your machine
  • A Heroku account
  • Heroku CLI set up

Step 1: Create a Flask Application

Start with a basic Flask application. Here's an example:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return "Welcome to your Flask App on Heroku!"

if __name__ == '__main__':
    app.run()

Step 2: Create Required Files for Deployment

You need to add a few additional files to prepare your app for deployment:

requirements.txt

Generate it with:

pip freeze > requirements.txt

Procfile

Create a file named Procfile without any file extension and add:

web: gunicorn app:app

Make sure your file is named app.py or update accordingly.

runtime.txt (Optional)

Specify the Python version:

python-3.10.8

Step 3: Initialize Git Repository

git init
git add .
git commit -m "Initial commit"

Step 4: Create a Heroku App

heroku login
heroku create your-app-name

Step 5: Deploy Your App to Heroku

git push heroku master

Or if you are using the main branch:

git push heroku main

Step 6: Open the Deployed App

Once deployment is complete, open your app in the browser using:

heroku open

Tips and Troubleshooting

  • Ensure gunicorn is in your requirements.txt
  • Check the Heroku logs with heroku logs --tail if issues arise
  • Don’t forget to commit any changes before pushing

Conclusion

Deploying a Flask application to Heroku is simple and fast when the required setup is properly followed. With just a few configuration files and Git commands, your app can be live and accessible from anywhere. It's a great way to share your projects or prototypes without managing complex infrastructure.



If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!



Best WordPress Hosting


Share:


Discount Coupons

Unlimited Video Generation

Best Platform to generate videos

Search and buy from Namecheap

Secure Domain for a Minimum Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat