Complete the code to serve static files from the 'static' folder in Flask.
app = Flask(__name__, static_folder=[1])Flask serves static files by default from the folder named static. Setting static_folder='static' tells Flask to use that folder.
Complete the code to generate a URL for a static file named 'style.css'.
url_for('static', filename=[1])
The url_for function with 'static' endpoint generates the URL for static files. The filename must match the actual file, here 'style.css'.
Fix the error in the code to enable caching of static files for 1 day.
app = Flask(__name__) app.config['SEND_FILE_MAX_AGE_DEFAULT'] = [1]
The SEND_FILE_MAX_AGE_DEFAULT config sets the cache timeout in seconds. 86400 seconds equals 1 day.
Fill both blanks to create a route that serves a static file with caching headers.
from flask import send_from_directory @app.route('/files/<path:filename>') def serve_file(filename): return send_from_directory([1], filename, cache_timeout=[2])
The send_from_directory function serves files from the 'static' folder. Setting cache_timeout=3600 caches files for 1 hour.
Fill all three blanks to create a Flask blueprint for static files with a custom URL prefix and caching.
from flask import Blueprint static_bp = Blueprint('static_bp', __name__, static_folder=[1], static_url_path=[2]) static_bp.static_cache_timeout = [3]
This blueprint serves static files from the 'assets' folder with URL prefix '/assets'. The cache timeout is set to 86400 seconds (1 day).