Challenge - 5 Problems
Flask Application Context Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
What does Flask's application context provide?
In Flask, what is the main purpose of the application context?
Attempts:
2 left
💡 Hint
Think about what data needs to be shared across different parts of the app during a request.
✗ Incorrect
The application context in Flask provides access to app-level data like configuration and resources. It allows code to access the current app instance globally during a request.
❓ component_behavior
intermediate2:00remaining
What happens if you access current_app outside application context?
Consider this Flask code snippet:
from flask import current_app
print(current_app.name)
What will happen when this code runs outside of an application context?
Attempts:
2 left
💡 Hint
Think about whether Flask knows which app you mean without context.
✗ Incorrect
Accessing current_app outside an application context causes Flask to raise a RuntimeError because it does not know which app instance to refer to.
❓ state_output
advanced2:00remaining
Output of code using app.app_context()
What is the output of this Flask code?
from flask import Flask, current_app
app = Flask(__name__)
with app.app_context():
print(current_app.name)
print('Done')
Attempts:
2 left
💡 Hint
Inside app.app_context(), current_app is available.
✗ Incorrect
Inside the app.app_context() block, current_app.name prints the app's name, which is usually __main__ when run directly. Then 'Done' prints after the block.
📝 Syntax
advanced2:00remaining
Identify the correct way to push application context manually
Which code snippet correctly pushes and pops the Flask application context manually?
Attempts:
2 left
💡 Hint
Check the Flask docs for app_context usage.
✗ Incorrect
The correct way is to create the context object with app.app_context(), then call push() and pop() on it.
🔧 Debug
expert2:00remaining
Why does this Flask code raise RuntimeError about app context?
Given this Flask code:
from flask import Flask, current_app
app = Flask(__name__)
def print_app_name():
print(current_app.name)
print_app_name()
Why does it raise RuntimeError: Working outside of application context?
Attempts:
2 left
💡 Hint
Think about when Flask knows which app is current.
✗ Incorrect
The error occurs because print_app_name() tries to access current_app without an active application context. The context must be pushed manually or via a request.