0
0
Flaskframework~10 mins

Profiling Flask applications - Interactive Code Practice

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

Complete the code to import the Flask class from the flask package.

Flask
from flask import [1]
app = [1](__name__)
Drag options to blanks, or click blank then click option'
AFlask
BRequest
Crender_template
DBlueprint
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request instead of Flask
Using render_template instead of Flask
Forgetting to import Flask
2fill in blank
medium

Complete the code to add the Flask-Profiler extension to the app.

Flask
from flask_profiler import Profiler

app = Flask(__name__)
app.config["flask_profiler"] = {"enabled": True}
profiler = [1](app)
Drag options to blanks, or click blank then click option'
AProfilerApp
BProfilerMiddleware
CProfilerExtension
DProfiler
Attempts:
3 left
💡 Hint
Common Mistakes
Using ProfilerMiddleware which is not part of flask_profiler
Using ProfilerExtension which does not exist
Not passing the app instance
3fill in blank
hard

Fix the error in the route decorator to profile the home page.

Flask
@app.route([1])
def home():
    return "Hello, Flask Profiler!"
Drag options to blanks, or click blank then click option'
Aapp
B"/"
Chome
D"/home"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function name instead of a string path
Using an incorrect path like "home" without slash
Passing the app object instead of a string
4fill in blank
hard

Fill both blanks to configure Flask-Profiler to store data in a SQLite database.

Flask
app.config["flask_profiler"] = {
    "enabled": True,
    "storage": [1],
    "storage_path": [2]
}
Drag options to blanks, or click blank then click option'
A"sqlite"
B"memory"
C"/tmp/flask_profiler.sqlite"
D"/var/log/flask_profiler"
Attempts:
3 left
💡 Hint
Common Mistakes
Using "memory" storage which does not persist data
Providing a directory path instead of a file path
Not enabling profiling
5fill in blank
hard

Fill all three blanks to create a custom middleware that logs request time in Flask.

Flask
import time
from flask import request

@app.before_request
def start_timer():
    [1] = time.time()
    setattr([2], "start_time", [1])

@app.after_request
def log_request(response):
    duration = time.time() - getattr([3], "start_time", time.time())
    print(f"Request took {duration:.4f} seconds")
    return response
Drag options to blanks, or click blank then click option'
Astart_time
Brequest
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Not using the request object to store start time
Using inconsistent variable names
Not returning the response in after_request