0
0
Flaskframework~20 mins

Before_request hooks in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Before_request Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a before_request hook returns a response?
Consider a Flask app with a before_request hook that returns a response object. What will Flask do next?
Flask
from flask import Flask, jsonify
app = Flask(__name__)

@app.before_request
def check():
    return jsonify({'error': 'blocked'})

@app.route('/')
def home():
    return 'Welcome!'

# What will be the output when accessing '/'?
AThe 'home' route runs and returns 'Welcome!'
BThe before_request response is sent; 'home' route is skipped
CFlask raises an error because before_request returned a response
DThe before_request hook runs after the 'home' route
Attempts:
2 left
💡 Hint
Think about what happens if a before_request returns a response instead of None.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this before_request hook
Which option contains a syntax error in defining a Flask before_request hook?
Flask
from flask import Flask
app = Flask(__name__)

@app.before_request
def check():
    print('Checking request')

@app.route('/')
def home():
    return 'Hello!'
A
@app.before_request
def check():
    print('Checking request')
B
@app.before_request()
def check():
    print('Checking request')
C
@app.before_request
def check:
    print('Checking request')
D
)'tseuqer gnikcehC'(tnirp    
:)(kcehc fed
tseuqer_erofeb.ppa@
Attempts:
2 left
💡 Hint
Check function definition syntax carefully.
state_output
advanced
2:00remaining
How many times does the before_request hook run on multiple requests?
Given this Flask app, how many times will the before_request hook run after 3 separate HTTP GET requests to '/'?
Flask
from flask import Flask
app = Flask(__name__)
counter = 0

@app.before_request
def increment():
    global counter
    counter += 1

@app.route('/')
def home():
    return f'Count: {counter}'
A3 times total, once per request
B1 time total, because the app runs once
C0 times, before_request hooks don't run automatically
DDepends on the number of routes, so 1 time
Attempts:
2 left
💡 Hint
Think about when before_request hooks run in relation to each HTTP request.
🔧 Debug
advanced
2:00remaining
Why does this before_request hook not block unauthorized access?
This Flask app tries to block access if a header is missing, but unauthorized users still get through. Why?
Flask
from flask import Flask, request, abort
app = Flask(__name__)

@app.before_request
def check_auth():
    if 'X-Auth' not in request.headers:
        abort(403)

@app.route('/')
def home():
    return 'Welcome!'
Aabort() is not imported from flask
Babort(403) is called but the route still runs
Cbefore_request hook is not registered correctly
Dabort(403) raises an exception that stops the request
Attempts:
2 left
💡 Hint
Consider what abort(403) does in Flask.
🧠 Conceptual
expert
2:00remaining
Order of execution with multiple before_request hooks
Given two before_request hooks in this Flask app, in what order do they run?
Flask
from flask import Flask
app = Flask(__name__)

@app.before_request
def first():
    print('First hook')

@app.before_request
def second():
    print('Second hook')

@app.route('/')
def home():
    return 'Done'
AFirst hook runs, then second hook
BOnly the last defined hook runs
CHooks run in random order
DSecond hook runs, then first hook
Attempts:
2 left
💡 Hint
Think about the order decorators are applied in Python.