Welcome to the world of web development with Flask! As a micro web framework for Python, Flask is perfect for building lightweight applications. Let's dive into the essentials.
1. Install Flask 📦
Start by installing Flask using pip:
pip install Flask
📌 Tip: For production, use pip install Flask==2.0.1
to ensure stability.
2. Create Your First App 🧱
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return "Hello, Flask!"
if __name__ == '__main__':
app.run(debug=True)
Run this script and visit http://localhost:5000 to see your first "Hello, Flask!" message. 🎉
3. Route & Template 🧭
- Define routes with
@app.route()
- Render templates using
render_template()
- Example:
@app.route('/about') def about(): return render_template('about.html')
Application_Structure
4. Database Integration 🗄️
Use SQLAlchemy for ORM:
from flask_sqlalchemy import SQLAlchemy
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
db = SQLAlchemy(app)
🔗 Explore Flask-SQLAlchemy documentation for advanced features.
5. Run & Debug 🚀
app.run()
starts the development serverdebug=True
enables real-time reloading- Always disable debug mode in production!
Flask_Logo
For more tutorials, check out our Flask tutorial series. 📚