Complete the code to serve static files in a Flask app.
from flask import Flask app = Flask(__name__, static_folder=[1]) @app.route('/') def home(): return 'Welcome!'
The static_folder parameter tells Flask where to find static files like CSS and images. The default folder is static.
Complete the code to link a CSS file from the static folder in an HTML template.
<link rel="stylesheet" href="{{ url_for('static', filename=[1]) }}">
The url_for('static', filename='style.css') generates the URL for the CSS file inside the static folder.
Fix the error in the Flask route to serve a static image file.
@app.route('/image') def image(): return app.send_static_file([1])
The send_static_file method expects the filename as a string relative to the static folder, so quotes are needed.
Fill both blanks to create a dictionary comprehension that maps filenames to their sizes for files in the static folder.
sizes = {file: os.path.getsize(os.path.join(app.static_folder, [1])) for file in os.listdir(app.static_folder) if file.endswith([2])}The comprehension uses file to join the path and filters files ending with '.png'.
Fill all three blanks to create a Flask route that serves a static JavaScript file with caching headers.
@app.route('/js/<filename>') def serve_js(filename): response = app.send_static_file([1]) response.headers['Cache-Control'] = [2] response.headers['Content-Type'] = [3] return response
The route sends the requested file by filename, sets caching to one hour, and sets the content type to JavaScript.