app Object (Flask)
Definition
The Flask Application Object is an instance of the Flask()
class in the Flask web framework. It serves as the core structure that holds the configuration, routes, request handlers, and various components necessary to manage a Flask web application.
Description
The Flask Application Object acts as the central interface for a Flask project. It is responsible for:
- Managing the application's configuration settings.
- Registering URL routes and view functions.
- Handling HTTP requests and generating responses.
- Integrating middleware, error handling, and hooks.
- Starting the web server using the run() method.
The object is typically initialized using the following syntax:
from flask import Flask
app = Flask(__name__)
Attributes
app.config
- Access configuration variables.app.secret_key
- Secure key for session and cookies.
Methods
app.route()
– Registers URL routes to specific functions.app.before_request()
– Adds functions to execute before each request.app.after_request()
– Adds functions to execute after each request.app.run()
– Launches the development web server.
Example
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return "Hello, World!"
if __name__ == '__main__':
app.run()
In this example, the app variable is the Flask Application Object that controls the routing and execution of the web application.
Related Objects
request
response