11 2 Flask Framework Explained
Key Concepts
Flask is a lightweight web framework for Python. Key concepts include:
- Introduction to Flask
- Installing Flask
- Creating a Simple Flask Application
- Routing
- Templates
- Static Files
- Request Handling
- Error Handling
1. Introduction to Flask
Flask is a micro web framework written in Python. It is designed to be simple and easy to use, making it a popular choice for small to medium-sized web applications.
2. Installing Flask
Before using Flask, you need to install it. You can install Flask using pip, the Python package installer.
pip install Flask
3. Creating a Simple Flask Application
A basic Flask application consists of a Python file with a few lines of code. The application starts by importing Flask and creating an instance of the Flask class.
Example:
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return "Hello, Flask!" if __name__ == '__main__': app.run(debug=True)
Analogy: Think of creating a Flask application as setting up a simple website with a single page that says "Hello, Flask!".
4. Routing
Routing in Flask allows you to map URLs to specific functions. This is done using the @app.route
decorator.
Example:
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return "Home Page" @app.route('/about') def about(): return "About Page" if __name__ == '__main__': app.run(debug=True)
Analogy: Think of routing as creating different pages on a website, each with its own URL.
5. Templates
Templates allow you to separate the presentation logic from the business logic. Flask uses the Jinja2 template engine to render HTML templates.
Example:
from flask import Flask, render_template app = Flask(__name__) @app.route('/') def home(): return render_template('home.html') if __name__ == '__main__': app.run(debug=True)
Analogy: Think of templates as creating reusable design elements for your website, like headers and footers.
6. Static Files
Static files, such as CSS, JavaScript, and images, are served directly by Flask. These files are stored in a "static" directory.
Example:
from flask import Flask, render_template app = Flask(__name__) @app.route('/') def home(): return render_template('home.html') if __name__ == '__main__': app.run(debug=True)
Analogy: Think of static files as the decorations and interactive elements that make your website look and feel great.
7. Request Handling
Flask provides a request object that contains all the data sent by the client. You can access this data in your view functions.
Example:
from flask import Flask, request app = Flask(__name__) @app.route('/login', methods=['POST']) def login(): username = request.form['username'] password = request.form['password'] return f"Username: {username}, Password: {password}" if __name__ == '__main__': app.run(debug=True)
Analogy: Think of request handling as processing the information a user submits through a form on your website.
8. Error Handling
Flask allows you to handle errors, such as 404 (Not Found) and 500 (Internal Server Error), by defining custom error handlers.
Example:
from flask import Flask, render_template app = Flask(__name__) @app.errorhandler(404) def page_not_found(error): return render_template('404.html'), 404 if __name__ == '__main__': app.run(debug=True)
Analogy: Think of error handling as creating custom error pages to guide users when something goes wrong on your website.