0
0
Flaskframework~10 mins

Custom middleware creation in Flask - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a Flask middleware class that initializes with the app.

Flask
class CustomMiddleware:
    def __init__(self, [1]):
        self.app = app
Drag options to blanks, or click blank then click option'
Aapp
Brequest
Cself
Dmiddleware
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'request' instead of 'app' as the parameter.
Not accepting any parameter in __init__.
2fill in blank
medium

Complete the code to make the middleware callable by Flask.

Flask
    def __call__(self, [1], start_response):
        return self.app([1], start_response)
Drag options to blanks, or click blank then click option'
Arequest
Bresponse
Cself
Denviron
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'request' instead of 'environ'.
Using 'self' as the first argument.
3fill in blank
hard

Fix the error in the middleware call to pass the correct arguments.

Flask
    def __call__(self, environ, [1]):
        return self.app(environ, start_response)
Drag options to blanks, or click blank then click option'
Astart_response
Bresponse
Cself
Drequest
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'response' or 'request' instead of 'start_response'.
Omitting the second argument.
4fill in blank
hard

Fill both blanks to add a print statement before calling the app.

Flask
    def __call__(self, [1], [2]):
        print("Middleware active")
        return self.app([1], [2])
Drag options to blanks, or click blank then click option'
Aenviron
Bstart_response
Crequest
Dresponse
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'request' or 'response' instead of WSGI parameters.
Swapping the order of arguments.
5fill in blank
hard

Fill all three blanks to wrap the Flask app with the middleware.

Flask
app = Flask(__name__)
app.wsgi_app = [1](app.[2])

@app.route('/')
def home():
    return [3]
Drag options to blanks, or click blank then click option'
ACustomMiddleware
Bwsgi_app
C"Hello, Middleware!"
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning middleware directly to app instead of app.wsgi_app.
Returning a function instead of a string in the route.