Wednesday, 22 October 2025

#14 Develop and Deploy Flask

Flask: Build & Deploy Fast—From Local App to Git Hosting

Flask: Build & Deploy Fast

This guide walks you through developing a simple Flask app and deploying it to production using Git and a web dashboard such as Render or Heroku.

Step 1: Set Up Your Flask App Locally

  1. Install Flask:
    pip install flask
  2. Create app.py:
    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/')
    def hello():
        return "Hello, Flask World!"
    
    if __name__ == '__main__':
        app.run(debug=True)
    
  3. Test your app locally:
    python app.py
    Open http://127.0.0.1:5000/ in your browser. You should see “Hello, Flask World!”

Step 2: Prepare for Deployment

  1. Create a requirements.txt file:
    pip freeze > requirements.txt
  2. Add a Procfile (for platforms like Heroku or Render):
    web: gunicorn app:app
  3. Install Gunicorn (for production server):
    pip install gunicorn

Step 3: Push Your Code to GitHub

  1. Initialize and push to a new Git repo:
    git init
    git add .
    git commit -m "Initial Flask app"
    git remote add origin https://github.com/yourusername/your-repo.git
    git push -u origin master
    

Step 4: Deploy Using a Web Dashboard (Heroku/Render)

  1. On Render or Heroku:
    • Create a new Web Service or App
    • Connect your GitHub repository
    • Build command: pip install -r requirements.txt
    • Start command: gunicorn app:app
    • Click Deploy!
  2. Visit your live app URL:
    You’ll receive a unique URL (e.g., https://your-app.onrender.com) to access your Flask app in production.

Summary

  • Develop Flask locally, test, and verify
  • Prepare deployment files needed for cloud hosting
  • Push code to GitHub
  • Deploy from a browser-friendly dashboard—no CLI required!
  • Share your app with the world!

No comments:

Post a Comment

#21a Dunder Method - Samples

Python Dunder (Magic) Methods – Complete Guide with Demos Python Dunder (Magic) Methods – Complete Guide with Demos ...