0
0
Flaskframework~10 mins

Static file optimization 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 serve static files from the 'static' folder in Flask.

Flask
app = Flask(__name__, static_folder=[1])
Drag options to blanks, or click blank then click option'
A'static'
B'assets'
C'public'
D'files'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a folder name other than 'static' without configuring Flask properly.
Forgetting to set the static_folder parameter.
2fill in blank
medium

Complete the code to generate a URL for a static file named 'style.css'.

Flask
url_for('static', filename=[1])
Drag options to blanks, or click blank then click option'
A'main.css'
B'style.css'
C'app.js'
D'image.png'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong filename that does not exist in the static folder.
Omitting quotes around the filename.
3fill in blank
hard

Fix the error in the code to enable caching of static files for 1 day.

Flask
app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = [1]
Drag options to blanks, or click blank then click option'
A0
B3600
C86400
D60
Attempts:
3 left
💡 Hint
Common Mistakes
Setting the cache timeout too low or zero disables caching.
Confusing minutes with seconds.
4fill in blank
hard

Fill both blanks to create a route that serves a static file with caching headers.

Flask
from flask import send_from_directory

@app.route('/files/<path:filename>')
def serve_file(filename):
    return send_from_directory([1], filename, cache_timeout=[2])
Drag options to blanks, or click blank then click option'
A'static'
B'templates'
C3600
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong folder name like 'templates'.
Setting cache timeout to zero disables caching.
5fill in blank
hard

Fill all three blanks to create a Flask blueprint for static files with a custom URL prefix and caching.

Flask
from flask import Blueprint

static_bp = Blueprint('static_bp', __name__, static_folder=[1], static_url_path=[2])
static_bp.static_cache_timeout = [3]
Drag options to blanks, or click blank then click option'
A'assets'
B'/assets'
C86400
D'static'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'static' folder name but mismatching URL prefix.
Setting cache timeout to zero or forgetting to set it.