Python > Web Development with Python > Web Frameworks (Overview) > Microframeworks (e.g., Flask)
Simple Flask Application
This snippet demonstrates a minimal Flask application that handles a single route and returns a simple HTML response. It showcases the basic structure of a Flask application, including route definition and response handling.
Code
This code defines a Flask application. Let's break it down:
Hello, World!
', which Flask interprets as HTML.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return '<h1>Hello, World!</h1>'
if __name__ == '__main__':
app.run(debug=True)
Concepts Behind the Snippet
This snippet demonstrates the core concepts of a web framework like Flask: routing and request handling. Routing maps URLs to specific functions (view functions). Request handling involves processing incoming HTTP requests and generating appropriate responses. Flask handles the complexities of parsing requests and constructing responses, allowing developers to focus on the application logic.
Real-Life Use Case
Imagine you're building a personal portfolio website. You could use Flask to define routes for your homepage ('/'), your 'about me' page ('/about'), and your projects page ('/projects'). Each route would be associated with a function that renders the appropriate HTML template for that page.
Best Practices
Interview Tip
Be prepared to explain the purpose of each line of code in a simple Flask application. Understand the difference between routing and request handling. Know how to start the development server and how to configure it for production.
When to use them
Flask is a great choice for small to medium-sized web applications, APIs, and prototypes. It's lightweight and flexible, allowing you to build applications quickly. It's also well-suited for projects where you need fine-grained control over your application's architecture.
Memory Footprint
Flask has a relatively small memory footprint compared to full-stack frameworks like Django. This makes it a good choice for resource-constrained environments or when you need to optimize for performance.
Alternatives
Alternatives to Flask include:
Pros
Cons
FAQ
-
What is the purpose of `__name__` in `Flask(__name__)`?
`__name__` is a special Python variable that represents the name of the current module. Flask uses it to determine the root path of the application, which is needed for finding resources like templates and static files. -
What does `debug=True` do in `app.run(debug=True)`?
The `debug=True` option enables debugging mode. In debugging mode, Flask will provide detailed error messages and automatically reload the server when you make changes to your code. It's very helpful during development, but should be disabled in production. -
How do I define multiple routes in a Flask application?
You can define multiple routes using the `@app.route()` decorator for each function you want to associate with a URL. For example: python @app.route('/about') def about(): return 'About Me' @app.route('/contact') def contact(): return 'Contact Us'