Flask is a lightweight, WSGI (Web Server Gateway Interface) micro-web framework for Python, designed to make getting started with web development quick and easy, with the ability to scale up to complex applications. Unlike full-stack frameworks like Django, Flask does not include an ORM (Object Relational Mapper), form validation, or other features by default. Instead, it provides a solid core and encourages developers to choose the tools and libraries that best fit their project's needs. This 'micro' aspect means Flask aims to keep the core simple but extensible.
Key features and concepts of Flask include:
- Routing: Maps URLs to Python functions (view functions).
- Werkzeug: A WSGI utility library that Flask uses for request, response, and URL routing objects.
- Jinja2: A powerful and flexible templating engine that Flask uses to render HTML templates.
- Contexts: Flask uses application and request contexts to manage global objects, making them available to view functions without explicitly passing them around.
- Extensibility: Flask has a rich ecosystem of extensions for common tasks like database integration (SQLAlchemy), authentication (Flask-Login), and API building (Flask-RESTful).
Flask is an excellent choice for building APIs, small to medium-sized web applications, or prototypes where you want fine-grained control over the components. It promotes a modular design, allowing developers to integrate various libraries seamlessly.
Example Code
from flask import Flask, render_template_string
Create a Flask application instance
app = Flask(__name__)
Define a route for the home page
@app.route('/')
def hello_world():
"""
This view function is executed when a user navigates to the root URL '/'.
It returns a simple greeting.
"""
return "Hello, Flask!"
Define another route with a dynamic part
@app.route('/user/<username>')
def show_user_profile(username):
"""
This view function demonstrates a dynamic URL.
The '<username>' part captures a value from the URL and passes it as an argument.
"""
return f'User: {username}'
Define a route that renders a simple HTML string (for demonstration)
@app.route('/template_example')
def template_example():
"""
Demonstrates rendering a simple HTML template string using Jinja2 syntax.
"""
return render_template_string(
"<h1>Hello, {{ name }}!</h1><p>This is a template example.</p>",
name="Visitor"
)
Run the application if the script is executed directly
if __name__ == '__main__':
app.run() starts the development server.
debug=True enables debug mode, which provides an interactive debugger
and reloads the server automatically on code changes.
app.run(debug=True)








Flask