0
0
Flaskframework~10 mins

Why performance matters in Flask - Test Your Understanding

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

Complete the code to create a basic Flask app that runs on the default port.

Flask
from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
    return 'Hello, World!'

if __name__ == '__main__':
    app.run([1])
Drag options to blanks, or click blank then click option'
Adebug=True
Bhost='0.0.0.0'
Cdebug=False
Dport=8080
Attempts:
3 left
💡 Hint
Common Mistakes
Using debug=True in production slows down the app and can expose sensitive info.
2fill in blank
medium

Complete the code to add caching to improve Flask app performance.

Flask
from flask import Flask
from flask_caching import Cache

app = Flask(__name__)
cache = Cache(app, config={'CACHE_TYPE': '[1]'})

@app.route('/')
@cache.cached(timeout=60)
def home():
    return 'Cached response!'

if __name__ == '__main__':
    app.run(debug=False)
Drag options to blanks, or click blank then click option'
Asimple
Bredis
Cfilesystem
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing 'null' disables caching, so no performance benefit.
3fill in blank
hard

Fix the error in the code to correctly measure request processing time in Flask.

Flask
from flask import Flask, g, request
import time

app = Flask(__name__)

@app.before_request
def start_timer():
    g.start = time.time()

@app.after_request
def log_time(response):
    duration = time.time() - [1]
    print(f'Request took {duration:.4f} seconds')
    return response

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

if __name__ == '__main__':
    app.run(debug=False)
Drag options to blanks, or click blank then click option'
Astart
Bg.start
Ctime
Drequest.start
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable not defined in the current scope causes errors.
4fill in blank
hard

Fill both blanks to create a route that accepts a dynamic user ID and returns it.

Flask
from flask import Flask
app = Flask(__name__)

@app.route('/user/[1]')
def user_profile([2]):
    return f'User ID is {user_id}'

if __name__ == '__main__':
    app.run(debug=False)
Drag options to blanks, or click blank then click option'
A<int:user_id>
Buser_id
C<string:username>
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching route parameter and function argument names causes errors.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps user IDs to their names for users with IDs greater than 10.

Flask
users = {1: 'Alice', 12: 'Bob', 15: 'Charlie', 7: 'Diana'}
filtered_users = { [1]: [2] for [3] in users if [1] > 10 }
Drag options to blanks, or click blank then click option'
Auser_id
Busers[user_id]
Duser
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name not matching the dictionary keys causes errors.