📖 Scenario: You are building a simple Flask web app that shows how Flask manages request context during a web request.
🎯 Goal: Create a Flask app that sets up a request context, uses a configuration variable, accesses the context data inside a route, and finally runs the app.
📋 What You'll Learn
Create a Flask app instance named app
Add a configuration variable GREETING with value 'Hello'
Create a route / that returns the greeting plus a fixed name
Run the Flask app with debug mode enabled
💡 Why This Matters
🌍 Real World
Web developers use Flask to build web applications that handle user requests and maintain data during each request using context.
💼 Career
Understanding Flask's context lifecycle is essential for backend developers working with Python web frameworks to build scalable and maintainable web apps.
Progress0 / 4 steps
1
Create the Flask app instance
Import Flask from flask and create a Flask app instance called app.
Flask
Hint
Use Flask(__name__) to create the app instance.
2
Add a configuration variable
Add a configuration variable called GREETING to app.config with the value 'Hello'.
Flask
Hint
Use app.config['GREETING'] = 'Hello' to add the config.
3
Create a route that uses the config
Create a route for '/' using @app.route. Define a function called index that returns the greeting from app.config['GREETING'] plus the string ' World!'.
Flask
Hint
Use @app.route('/') and a function index that returns the greeting plus ' World!'.
4
Run the Flask app
Add the code to run the Flask app with debug=True inside the if __name__ == '__main__' block.
Flask
Hint
Use the standard Python entry point check and call app.run(debug=True).
Practice
(1/5)
1. What is the main purpose of Flask's before_request function in the context lifecycle?
easy
A. To run code before each request to prepare data or resources
B. To clean up resources after a request finishes
C. To handle errors during a request
D. To define the URL routes for the application
Solution
Step 1: Understand the role of before_request
This function runs before each request to set up anything needed during the request.
Step 2: Compare with other lifecycle hooks
after_request cleans up after requests, error handlers manage errors, and routes define URLs.
Final Answer:
To run code before each request to prepare data or resources -> Option A
Quick Check:
before_request runs before request [OK]
Hint: Remember: before_request runs setup code before handling requests [OK]
Common Mistakes:
Confusing before_request with after_request
Thinking before_request handles errors
Mixing route definitions with lifecycle hooks
2. Which of the following is the correct way to store data for the current request using Flask's context?
easy
A. Store data in the g object inside a before_request function
B. Use a global variable outside the request
C. Save data in a class variable of the Flask app
D. Write data directly to the response object
Solution
Step 1: Identify how Flask manages request-specific data
Flask provides the g object to store data unique to each request.
Step 2: Confirm correct usage
Using g inside before_request ensures data is set up before route handlers run.
Final Answer:
Store data in the g object inside a before_request function -> Option A
Quick Check:
g stores request data [OK]
Hint: Use g for request data, not globals [OK]
Common Mistakes:
Using global variables that persist across requests
Trying to store request data in app class variables
Modifying the response object directly for data storage
3. Given this Flask code snippet, what will be printed when a request is made to /hello?