0
0
Flaskframework~20 mins

Application context in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask Application Context Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What does Flask's application context provide?
In Flask, what is the main purpose of the application context?
AIt stores information about the current HTTP request and user session.
BIt handles the rendering of HTML templates for the app.
CIt manages the database connections automatically for each request.
DIt holds data related to the running Flask app, like configuration and resources, accessible globally during a request.
Attempts:
2 left
💡 Hint
Think about what data needs to be shared across different parts of the app during a request.
component_behavior
intermediate
2: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?
AIt prints the Flask app's name successfully.
BIt raises a RuntimeError indicating no application context is active.
CIt returns None without error.
DIt raises a KeyError due to missing app data.
Attempts:
2 left
💡 Hint
Think about whether Flask knows which app you mean without context.
state_output
advanced
2: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')
A
__main__
Done
B
RuntimeError
Done
C
Done
__main__
D
None
Done
Attempts:
2 left
💡 Hint
Inside app.app_context(), current_app is available.
📝 Syntax
advanced
2:00remaining
Identify the correct way to push application context manually
Which code snippet correctly pushes and pops the Flask application context manually?
A
app.push_context()
# do work
app.pop_context()
B
with app.app_context.push():
    # do work
C
ctx = app.app_context()
ctx.push()
# do work
ctx.pop()
D
ctx = app.context()
ctx.enter()
# do work
ctx.exit()
Attempts:
2 left
💡 Hint
Check the Flask docs for app_context usage.
🔧 Debug
expert
2: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?
ABecause the application context was never pushed before calling print_app_name().
BBecause current_app can only be used inside a request context, not application context.
CBecause print_app_name() is called before app.run(), so no context is active.
DBecause Flask apps require a special decorator to access current_app.
Attempts:
2 left
💡 Hint
Think about when Flask knows which app is current.