A Flask application is an instance of the Flask
class. This instance serves as the central object that manages the application’s configuration, routes, and requests.
from flask import Flask
# Create a Flask application instance
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run()
Routes in Flask are used to map URLs to Python functions called views. When a user visits a specific URL, Flask calls the corresponding view function and returns its result as a response.
@app.route('/about')
def about():
return 'This is the about page.'
Flask uses the request
object to handle incoming HTTP requests and the Response
object to send HTTP responses back to the client.
from flask import request
@app.route('/login', methods=['POST'])
def login():
username = request.form.get('username')
password = request.form.get('password')
# Logic to handle login
return 'Login successful'
Flask is commonly used to build RESTful APIs. It can handle various HTTP methods (GET, POST, PUT, DELETE) and return data in different formats such as JSON.
from flask import jsonify
@app.route('/api/users', methods=['GET'])
def get_users():
users = [{'id': 1, 'name': 'John'}, {'id': 2, 'name': 'Jane'}]
return jsonify(users)
Flask is a great choice for building small to medium-sized web applications such as blogs, personal websites, and internal tools. Its simplicity and flexibility allow developers to quickly prototype and deploy applications.
pip
.pip install flask
app.py
) and import the Flask
class.from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'Welcome to my Flask application!'
if __name__ == '__main__':
app.run(debug=True)
python app.py
You can define routes using the @app.route
decorator.
@app.route('/products')
def products():
return 'This is the products page.'
Flask allows you to define dynamic routes by using variable parts in the URL.
@app.route('/users/<int:user_id>')
def get_user(user_id):
return f'User ID: {user_id}'
You can specify the HTTP methods that a route can handle using the methods
parameter in the @app.route
decorator.
@app.route('/contact', methods=['GET', 'POST'])
def contact():
if request.method == 'GET':
return 'This is the contact page. Fill out the form below.'
elif request.method == 'POST':
# Handle form submission
return 'Thank you for your message!'
Flask can return different types of responses, including strings, JSON, and HTML templates.
from flask import render_template
@app.route('/home')
def home():
return render_template('home.html')
Flask uses Jinja2 as its templating engine. You can create HTML templates in the templates
directory and render them using the render_template
function.
@app.route('/blog')
def blog():
posts = [{'title': 'Post 1', 'content': 'This is the first post.'},
{'title': 'Post 2', 'content': 'This is the second post.'}]
return render_template('blog.html', posts=posts)
Static files such as CSS, JavaScript, and images can be stored in the static
directory. You can link to these files in your HTML templates using the url_for
function.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Blog</title>
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
</head>
<body>
<!-- Blog content -->
</body>
</html>
Flask can be integrated with various databases such as SQLite, MySQL, and PostgreSQL. One popular library for database integration is SQLAlchemy.
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80))
@app.route('/add_user')
def add_user():
new_user = User(name='Alice')
db.session.add(new_user)
db.session.commit()
return 'User added successfully'
Running Flask in debug mode in production can expose sensitive information and security vulnerabilities. Always turn off debug mode in production.
if __name__ == '__main__':
app.run(debug=False)
Flask applications can encounter various errors. It’s important to handle errors gracefully using error handlers.
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
Flask applications are vulnerable to common web security issues such as SQL injection and cross - site scripting (XSS). Use parameterized queries when interacting with the database and sanitize user input.
Always use a virtual environment to manage your project’s dependencies. This helps to avoid conflicts between different projects.
python -m venv myenv
source myenv/bin/activate
Although Flask is a microframework, it’s a good practice to follow the Model - View - Controller (MVC) pattern. Separate your application into models (database logic), views (templates and routes), and controllers (business logic).
Write unit tests for your Flask application using a testing framework such as unittest
or pytest
. This helps to ensure the quality and reliability of your code.
import unittest
from app import app
class TestFlaskApp(unittest.TestCase):
def setUp(self):
self.app = app.test_client()
def test_index(self):
response = self.app.get('/')
self.assertEqual(response.status_code, 200)
if __name__ == '__main__':
unittest.main()
Flask is a powerful and flexible web framework for intermediate Python developers. It provides a great starting point for learning web development concepts and building real - world web applications. By understanding the core concepts, typical usage scenarios, common pitfalls, and best practices, you can effectively use Flask to build scalable and secure web applications.