Flask is one of the most popular web frameworks for Python, known for its simplicity and flexibility. Whether you're building a simple API or a complex web application, Flask provides the tools you need to get started quickly.
Why Flask?
Flask is often called a "microframework" because it provides a simple core with the ability to extend functionality as needed. This makes it perfect for:
- Rapid prototyping
- Small to medium-sized applications
- Learning web development
- Building REST APIs
Installation
Getting started with Flask is straightforward. First, make sure you have Python installed, then create a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install flask
Your First Flask App
Here's a simple "Hello World" application:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return '<h1>Hello, World!</h1>'
if __name__ == '__main__':
app.run(debug=True)
Save this as app.py and run it:
python app.py
Visit http://localhost:5000 in your browser, and you'll see your first Flask application!
Routes and Views
In Flask, you define routes using the @app.route() decorator. Routes map URLs to Python functions:
@app.route('/about')
def about():
return 'This is the about page'
@app.route('/user/<username>')
def show_user(username):
return f'User: {username}'
Templates
Flask uses Jinja2 for templating, allowing you to separate your logic from presentation:
from flask import render_template
@app.route('/')
def index():
return render_template('index.html', name='Matt')
Conclusion
Flask is a powerful and flexible framework that makes web development with Python enjoyable. Start with the basics, and gradually explore more advanced features like blueprints, extensions, and database integration.
Happy coding!