Complete the code to define a Flask middleware class that initializes with the app.
class CustomMiddleware: def __init__(self, [1]): self.app = app
The middleware class needs to accept the app as a parameter to wrap it.
Complete the code to make the middleware callable by Flask.
def __call__(self, [1], start_response): return self.app([1], start_response)
The __call__ method of middleware receives the WSGI environment dictionary as the first argument.
Fix the error in the middleware call to pass the correct arguments.
def __call__(self, environ, [1]): return self.app(environ, start_response)
The second argument to __call__ is the start_response callable used by WSGI.
Fill both blanks to add a print statement before calling the app.
def __call__(self, [1], [2]): print("Middleware active") return self.app([1], [2])
The __call__ method must accept environ and start_response to work correctly.
Fill all three blanks to wrap the Flask app with the middleware.
app = Flask(__name__) app.wsgi_app = [1](app.[2]) @app.route('/') def home(): return [3]
We wrap the Flask app's wsgi_app with CustomMiddleware and return a string from the route.
