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
-
Install Flask:
pip install flask -
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) -
Test your app locally:
python app.pyOpen http://127.0.0.1:5000/ in your browser. You should see “Hello, Flask World!”
Step 2: Prepare for Deployment
-
Create a
requirements.txtfile:pip freeze > requirements.txt -
Add a
Procfile(for platforms like Heroku or Render):web: gunicorn app:app -
Install Gunicorn (for production server):
pip install gunicorn
Step 3: Push Your Code to GitHub
-
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)
-
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!
-
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