Python > Web Development with Python > Web Frameworks (Overview) > Introduction to Web Frameworks
Flask 'Hello, World!' Example
Flask
is a micro web framework for Python, known for its simplicity and flexibility. This snippet demonstrates the basic structure of a Flask application, creating a simple 'Hello, World!' web page.
Basic Flask Application Structure
This code sets up a minimal Flask application. It first imports the Flask class from the flask module. Then, it creates an instance of the Flask class, passing the name of the current module (__name__) as an argument. The @app.route('/')
decorator binds the hello_world
function to the root URL ('/'). When a user visits the root URL, the hello_world
function is executed, returning the string 'Hello, World!' which is displayed in the browser. Finally, app.run(debug=True)
starts the Flask development server. The debug=True
option enables debugging mode, which provides detailed error messages and automatically reloads the server when code changes are detected. This is useful during development but should be disabled in production.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True)
Concepts Behind the Snippet
Key concepts illustrated by this snippet include: 1. Routing: Mapping URLs to specific functions using @app.route
. 2. Request Handling: Defining functions that handle incoming web requests. 3. Response Generation: Returning data from functions to be displayed in the browser. 4. WSGI: Flask is a WSGI (Web Server Gateway Interface) application, meaning it communicates with a web server using the WSGI standard.
Real-Life Use Case
This basic 'Hello, World!' example serves as a starting point for any Flask application. It can be expanded to handle more complex routes, render HTML templates, interact with databases, and implement user authentication. Imagine it as the foundation for a personal blog, a simple API endpoint, or a tool for managing tasks.
Best Practices
Interview Tip
Be prepared to explain the role of the @app.route
decorator, the purpose of app.run()
, and the difference between development and production environments. Also, understand how Flask handles requests and responses.
When to use Flask
Flask is well-suited for small to medium-sized web applications, APIs, and prototypes. It's also a good choice when you need more control over your application's structure and components compared to a more opinionated framework like Django.
Memory Footprint
Flask has a relatively small memory footprint, especially compared to full-fledged frameworks like Django, making it a good choice for resource-constrained environments.
Alternatives
Alternatives to Flask include:
Pros
Cons
FAQ
-
What is the purpose of
__name__
inFlask(__name__)
?
__name__
is a special variable in Python that holds the name of the current module. When the script is executed directly,__name__
is set to__main__
. Flask uses this information to determine the root path of the application, which is important for finding templates and static files. -
How do I handle different HTTP methods (GET, POST, PUT, DELETE) in Flask?
You can specify the HTTP methods that a route should handle using themethods
argument in the@app.route
decorator. For example,@app.route('/login', methods=['GET', 'POST'])
would handle both GET and POST requests to the '/login' URL. You can then access the request method usingrequest.method
within the function.