flask - dwilson2547/wiki_demo GitHub Wiki
Flask is a lightweight, micro web framework for Python, designed to be simple, flexible, and easy to use. It provides the essential tools to build web applications and APIs without imposing rigid structures or dependencies. Flask is ideal for small to medium-sized projects, prototyping, and microservices, but it can also scale to larger applications with extensions.
- 1. Key Features of Flask
- 2. Flask vs. Other Frameworks
- 3. Example: Building a Simple API
- 4. Flask Extensions
- 5. Strengths of Flask
- 6. Weaknesses of Flask
- 7. When to Use Flask
- 8. Example: Flask with SQLAlchemy
- 9. Learning Resources
- 10. Summary
- Minimalist Core: Flask provides the basics (routing, request/response handling) and lets you add extensions as needed.
-
Example:
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return "Hello, Flask!" if __name__ == '__main__': app.run(debug=True)
-
URL Routing: Map URLs to Python functions using decorators.
@app.route('/about') def about(): return "About Page"
-
Dynamic Routes:
@app.route('/user/<username>') def show_user(username): return f"User: {username}"
-
HTTP Methods: Specify methods (GET, POST, etc.).
@app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': return "Login Form Submitted" return "Login Form"
-
Access Request Data: Use the global
request
object.from flask import request @app.route('/submit', methods=['POST']) def submit(): data = request.form['name'] # Form data return f"Received: {data}"
-
JSON Responses: Return JSON using
jsonify
.from flask import jsonify @app.route('/api/data') def get_data(): return jsonify({"name": "Alice", "age": 30})
-
Dynamic HTML: Flask integrates with Jinja2 for templating.
Template File (
from flask import render_template @app.route('/profile/<name>') def profile(name): return render_template('profile.html', name=name)
templates/profile.html
):<h1>Hello, {{ name }}!</h1>
-
Add Functionality: Flask supports extensions for databases, authentication, and more.
- Flask-SQLAlchemy: Database ORM.
- Flask-Login: User authentication.
- Flask-WTF: Form handling.
- Flask-RESTful: Build REST APIs.
-
Example (Flask-SQLAlchemy):
from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///mydatabase.db' db = SQLAlchemy(app) class User(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80)) @app.route('/users') def get_users(): users = User.query.all() return jsonify([{"id": user.id, "name": user.name} for user in users])
-
Built-in Server: Flask includes a development server for testing.
flask run
- Runs on
http://127.0.0.1:5000
by default. -
Debug Mode: Auto-reloads on code changes.
flask run --debug
- Runs on
-
Modular Applications: Organize routes into reusable components.
from flask import Blueprint admin_bp = Blueprint('admin', __name__) @admin_bp.route('/dashboard') def dashboard(): return "Admin Dashboard" app.register_blueprint(admin_bp, url_prefix='/admin')
Feature | Flask | Django | FastAPI |
---|---|---|---|
Type | Micro framework | Full-stack framework | Modern API framework |
Learning Curve | Easy | Steep | Moderate |
Flexibility | High (unopinionated) | Low (batteries-included) | High (focused on APIs) |
ORM | Extensions (Flask-SQLAlchemy) | Built-in (Django ORM) | Extensions (SQLAlchemy, Tortoise-ORM) |
Admin Interface | ❌ No (use extensions) | ✅ Yes (Django Admin) | ❌ No |
Performance | Moderate | Moderate | High (async support) |
Use Cases | Small apps, APIs, microservices | Large apps, CMS, enterprise | High-performance APIs |
Async Support | ❌ No (Flask 2.0+ has limited support) | ❌ No (Django 3.1+ has async views) | ✅ Yes (built on Starlette) |
pip install flask
from flask import Flask, request, jsonify
app = Flask(__name__)
# In-memory "database"
books = [
{"id": 1, "title": "The Great Gatsby", "author": "F. Scott Fitzgerald"},
{"id": 2, "title": "1984", "author": "George Orwell"}
]
# GET all books
@app.route('/books', methods=['GET'])
def get_books():
return jsonify(books)
# GET a single book
@app.route('/books/<int:book_id>', methods=['GET'])
def get_book(book_id):
book = next((book for book in books if book['id'] == book_id), None)
if book:
return jsonify(book)
return jsonify({"error": "Book not found"}), 404
# POST a new book
@app.route('/books', methods=['POST'])
def add_book():
new_book = request.get_json()
books.append(new_book)
return jsonify(new_book), 201
# PUT (update) a book
@app.route('/books/<int:book_id>', methods=['PUT'])
def update_book(book_id):
book = next((book for book in books if book['id'] == book_id), None)
if not book:
return jsonify({"error": "Book not found"}), 404
book.update(request.get_json())
return jsonify(book)
# DELETE a book
@app.route('/books/<int:book_id>', methods=['DELETE'])
def delete_book(book_id):
global books
books = [book for book in books if book['id'] != book_id]
return jsonify({"result": "Book deleted"})
if __name__ == '__main__':
app.run(debug=True)
-
GET All Books:
curl http://127.0.0.1:5000/books
-
POST a New Book:
curl -X POST -H "Content-Type: application/json" -d '{"id": 3, "title": "Brave New World", "author": "Aldous Huxley"}' http://127.0.0.1:5000/books
-
GET a Single Book:
curl http://127.0.0.1:5000/books/1
Extension | Purpose | Example |
---|---|---|
Flask-SQLAlchemy | Database ORM (SQLAlchemy integration) | db = SQLAlchemy(app) |
Flask-Login | User authentication | login_manager = LoginManager(app) |
Flask-WTF | Form handling and validation | form = MyForm() |
Flask-RESTful | Build REST APIs | api.add_resource(BookResource, '/books') |
Flask-Migrate | Database migrations (with Alembic) | migrate = Migrate(app, db) |
Flask-CORS | Cross-Origin Resource Sharing (CORS) | CORS(app) |
Flask-JWT-Extended | JWT authentication | jwt = JWTManager(app) |
✅ Lightweight: Minimal core with extensions for added functionality. ✅ Flexible: Unopinionated design allows custom solutions. ✅ Easy to Learn: Simple syntax and great for beginners. ✅ Extensible: Hundreds of extensions for databases, auth, APIs, and more. ✅ Development Server: Built-in server for quick testing. ✅ Jinja2 Templating**: Powerful HTML templating engine. ✅ Community: Large ecosystem and active community support.
❌ No Built-in Admin: Unlike Django, Flask lacks a built-in admin interface. ❌ No ORM: Requires Flask-SQLAlchemy or another extension for database operations. ❌ Less "Batteries-Included": More setup required for large applications. ❌ Performance: Not as fast as async frameworks (e.g., FastAPI) for high-load APIs. ❌ Thread-Local Storage: Can cause issues in asynchronous contexts (Flask 2.0+ adds async support).
- Small to Medium Projects: Ideal for prototyping, APIs, and microservices.
- Flexibility Needed: When you want control over components (e.g., choice of ORM, auth).
- Learning Web Development: Great for beginners due to its simplicity.
- Microservices: Lightweight and easy to deploy in containerized environments.
- Extending Existing Apps: Add a web interface or API to an existing Python application.
pip install flask-sqlalchemy flask-migrate
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
migrate = Migrate(app, db)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
def __repr__(self):
return f'<User {self.username}>'
@app.route('/')
def index():
return "Hello, Flask-SQLAlchemy!"
if __name__ == '__main__':
app.run(debug=True)
flask db init # Initialize migrations
flask db migrate -m "Initial migration" # Create a migration
flask db upgrade # Apply the migration
from flask import request, jsonify
@app.route('/users', methods=['GET'])
def get_users():
users = User.query.all()
return jsonify([{"id": user.id, "username": user.username, "email": user.email} for user in users])
@app.route('/users', methods=['POST'])
def create_user():
data = request.get_json()
user = User(username=data['username'], email=data['email'])
db.session.add(user)
db.session.commit()
return jsonify({"id": user.id, "username": user.username, "email": user.email}), 201
- Official Documentation: flask.palletsprojects.com
- Tutorials:
-
Books:
- Flask Web Development by Miguel Grinberg
- Explore Flask by Robert Picard
- Courses:
- Flask is a lightweight, micro web framework for Python.
- Key Features: Routing, templating (Jinja2), extensions, and a built-in development server.
- Strengths: Flexibility, simplicity, and a rich ecosystem of extensions.
- Weaknesses: Lack of built-in features (e.g., ORM, admin interface) and performance limitations for high-load apps.
- Use Cases: Ideal for small to medium projects, APIs, microservices, and learning web development.
Flask is a great choice for developers who want a simple, unopinionated framework to build web applications quickly. Its modular design and extensibility make it adaptable to a wide range of projects. For larger applications, consider combining Flask with extensions like Flask-SQLAlchemy or migrating to a full-stack framework like Django.