Challenge - 5 Problems
Flask Static Files Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when serving a JavaScript file with Flask static folder?
Given this Flask app setup, what will the browser receive when requesting
/static/app.js?Flask
from flask import Flask app = Flask(__name__) # Assume 'static/app.js' contains: console.log('Hello from JS'); if __name__ == '__main__': app.run()
Attempts:
2 left
💡 Hint
Flask automatically serves files from the 'static' folder at the '/static' URL path.
✗ Incorrect
Flask by default serves files placed in the 'static' folder when requested via '/static/filename'. So requesting '/static/app.js' returns the JavaScript file content.
📝 Syntax
intermediate2:00remaining
Which Flask route correctly serves a JavaScript file from a custom folder?
You want to serve JavaScript files from a folder named 'js' inside your project root. Which route code correctly serves the file
filename?Attempts:
2 left
💡 Hint
Use Flask's send_from_directory to serve files from a folder.
✗ Incorrect
send_from_directory safely serves files from a folder. Option D uses it correctly with the route parameter as a path to allow subfolders.
🔧 Debug
advanced2:00remaining
Why does this Flask app fail to serve JavaScript files from 'static' folder?
Consider this Flask app code snippet:
from flask import Flask
app = Flask(__name__, static_folder='assets')
# JavaScript file is located at 'assets/app.js'
if __name__ == '__main__':
app.run()
When requesting '/static/app.js', the browser gets a 404 error. Why?
Attempts:
2 left
💡 Hint
Changing static_folder changes the URL path prefix for static files.
✗ Incorrect
When you set static_folder='assets', Flask serves files from 'assets' folder at the URL path '/assets', not '/static'. So requesting '/static/app.js' returns 404.
❓ state_output
advanced2:00remaining
What is the Content-Type header when Flask serves a JavaScript file?
If Flask serves a file named 'script.js' from the static folder, what is the Content-Type header sent to the browser?
Attempts:
2 left
💡 Hint
Browsers expect JavaScript files to have a specific MIME type.
✗ Incorrect
Flask uses mimetypes to set Content-Type headers. For '.js' files, it sets 'application/javascript' so browsers treat it as JavaScript.
🧠 Conceptual
expert3:00remaining
How to serve versioned JavaScript files to avoid browser caching in Flask?
You want to serve JavaScript files with a version query parameter like
/static/app.js?v=123 to force browsers to reload updated files. Which approach is best in Flask?Attempts:
2 left
💡 Hint
Flask's url_for can add query parameters to static file URLs.
✗ Incorrect
Using url_for with extra query parameters adds versioning info to URLs, which browsers treat as new resources, forcing reloads without renaming files or disabling caching globally.