Flask Module in Python: A Comprehensive Guide
Flask is a lightweight WSGI web application framework in Python. It is designed with simplicity and flexibility in mind, making it a popular choice for developers looking to create web applications quickly and easily. This guide covers the key features, functionalities, and examples to help you get started with Flask.
Introduction to Flask
Flask is a micro-framework for Python based on Werkzeug and Jinja2. It is designed to be easy to use and extend, providing the essentials for web development without imposing a specific structure or dependencies.
Key features of Flask: - Lightweight and modular design - Built-in development server and debugger - Integrated support for unit testing - RESTful request dispatching - Jinja2 templating - Secure cookie handling
Installation
To install Flask, you can use pip:
Basic Application Structure
A minimal Flask application consists of a single Python file:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True)
Save this as app.py
and run it using:
Routing
Routing in Flask is straightforward. You define URL patterns and associate them with view functions.
@app.route('/hello')
def hello():
return 'Hello, Flask!'
@app.route('/user/<username>')
def show_user_profile(username):
return f'User {username}'
Templates
Flask uses Jinja2 for templating. Create an HTML template in the templates
folder.
templates/index.html
<!doctype html>
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
<h1>{{ message }}</h1>
</body>
</html>
Render the template in your view function:
from flask import render_template
@app.route('/')
def home():
return render_template('index.html', title='Home Page', message='Welcome to Flask!')
Forms and Validation
Use Flask-WTF to handle forms and validation.
Install Flask-WTF:
app.py
from flask import Flask, render_template, request, redirect, url_for
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired
app = Flask(__name__)
app.config['SECRET_KEY'] = 'your_secret_key'
class NameForm(FlaskForm):
name = StringField('What is your name?', validators=[DataRequired()])
submit = SubmitField('Submit')
@app.route('/form', methods=['GET', 'POST'])
def form():
form = NameForm()
if form.validate_on_submit():
return redirect(url_for('hello', username=form.name.data))
return render_template('form.html', form=form)
templates/form.html
<!doctype html>
<html>
<head>
<title>Form</title>
</head>
<body>
<form method="post">
{{ form.hidden_tag() }}
{{ form.name.label }} {{ form.name }}
{{ form.submit }}
</form>
</body>
</html>
Database Integration
SQLAlchemy
Flask-SQLAlchemy simplifies database interactions.
Install Flask-SQLAlchemy:
app.py
from flask_sqlalchemy import SQLAlchemy
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
def __repr__(self):
return f'<User {self.username}>'
@app.route('/add_user/<username>')
def add_user(username):
user = User(username=username)
db.session.add(user)
db.session.commit()
return f'User {username} added.'
Flask-Migrate
Flask-Migrate handles database migrations.
Install Flask-Migrate:
app.py
User Authentication
Use Flask-Login to handle user authentication.
Install Flask-Login:
app.py
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user
login_manager = LoginManager()
login_manager.init_app(app)
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
@app.route('/login', methods=['GET', 'POST'])
def login():
# Here you would typically check the username and password from a form
user = User.query.filter_by(username='admin').first()
login_user(user)
return 'Logged in!'
@app.route('/logout')
@login_required
def logout():
logout_user()
return 'Logged out!'
@app.route('/dashboard')
@login_required
def dashboard():
return f'Hello, {current_user.username}!'
RESTful API
Use Flask-RESTful to create RESTful APIs.
Install Flask-RESTful:
app.py
from flask_restful import Resource, Api
api = Api(app)
class HelloWorld(Resource):
def get(self):
return {'hello': 'world'}
api.add_resource(HelloWorld, '/api/hello')
Error Handling
Custom error pages can be defined in Flask.
templates/404.html
<!doctype html>
<html>
<head>
<title>Page Not Found</title>
</head>
<body>
<h1>404 - Page Not Found</h1>
</body>
</html>
Deployment
Deploy your Flask application using a WSGI server like Gunicorn.
Install Gunicorn:
Run your app with Gunicorn:
For more robust deployment, consider using platforms like Heroku, AWS, or Docker.
Conclusion
Flask is a versatile and easy-to-use framework for building web applications in Python. Its simplicity and flexibility make it suitable for both beginners and experienced developers. By mastering the core features and functionalities of Flask, you can create powerful web applications with minimal effort.