What is the purpose of the application context in Flask?
The application context in Flask holds data related to the app itself, like configuration and resources, making them accessible during a request or CLI command.
Click to reveal answer
beginner
What does the request context in Flask contain?
The request context contains information about the current HTTP request, such as form data, headers, and session, allowing code to access request-specific data.
Click to reveal answer
intermediate
When is the Flask application context pushed and popped?
The application context is pushed before handling a request or CLI command and popped after the request or command finishes, ensuring resources are available only when needed.
Click to reveal answer
intermediate
Explain the order of context lifecycle during a Flask request.
First, Flask pushes the application context, then the request context. After processing, it pops the request context followed by the application context, cleaning up resources in reverse order.
Click to reveal answer
advanced
How can you manually manage Flask contexts in your code?
You can manually push and pop contexts using 'app.app_context()' and 'app.test_request_context()' with 'with' statements to control when contexts are active.
Click to reveal answer
Which context in Flask holds the current HTTP request data?
AApplication context
BDatabase context
CRequest context
DSession context
✗ Incorrect
The request context contains data specific to the current HTTP request, such as form inputs and headers.
When does Flask pop the application context?
ADuring app startup
BBefore handling a request
CBefore pushing the request context
DAfter handling a request
✗ Incorrect
Flask pops the application context after the request or CLI command finishes to clean up resources.
Which of these is true about Flask context lifecycle?
AApplication context is pushed before request context
BApplication context is popped before request context
CRequest context is pushed before application context
DContexts are never popped automatically
✗ Incorrect
Flask pushes the application context first, then the request context, and pops them in reverse order.
How can you manually activate an application context in Flask?
AUsing 'with app.app_context():'
BUsing 'with app.request_context():'
CUsing 'app.push_context()'
DUsing 'app.activate_context()'
✗ Incorrect
You manually activate the application context using 'with app.app_context():' to push it temporarily.
What happens if you try to access request data outside the request context?
AFlask returns default values
BFlask raises a RuntimeError
CFlask silently ignores the access
DFlask creates a new request context automatically
✗ Incorrect
Accessing request data outside the request context raises a RuntimeError because the data is not available.
Describe the lifecycle of Flask contexts during a web request.
Think about the order of pushing and popping contexts around request handling.
You got /5 concepts.
Explain how you can manually manage Flask contexts in your code and why you might do that.
Consider situations outside normal request handling where context is needed.
You got /4 concepts.
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?