0
0
Flaskframework~20 mins

Static file optimization in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Static File Optimization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
How does Flask serve static files by default?

Consider a Flask app with a folder named static containing CSS and JS files. What happens when a browser requests /static/style.css?

AFlask cannot serve static files; a separate web server is needed.
BFlask automatically serves the file from the <code>static</code> folder without extra code.
CFlask serves static files only if <code>send_file()</code> is called in a route.
DFlask requires a route to be defined to serve static files manually.
Attempts:
2 left
💡 Hint

Think about Flask's default behavior for the static folder.

📝 Syntax
intermediate
1:30remaining
Which Flask code snippet correctly sets cache timeout for static files?

You want to set a cache timeout of 1 hour (3600 seconds) for static files in Flask. Which code snippet achieves this?

Aapp = Flask(__name__, static_folder='static', static_url_path='/static', static_cache_timeout=3600)
B
app = Flask(__name__)
app.config['STATIC_CACHE_TIMEOUT'] = 3600
C
app = Flask(__name__)
app.static_cache_timeout = 3600
D
app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 3600
Attempts:
2 left
💡 Hint

Look for the Flask config key that controls static file cache timeout.

state_output
advanced
1:30remaining
What is the output of this Flask static file URL generation?

Given this Flask code snippet, what is the output of url_for('static', filename='js/app.js')?

Flask
from flask import Flask, url_for
app = Flask(__name__)

with app.test_request_context():
    print(url_for('static', filename='js/app.js'))
A/static/js/app.js
B/js/app.js
C/static/app.js
D/app.js
Attempts:
2 left
💡 Hint

Remember the default static URL path in Flask.

🔧 Debug
advanced
2:00remaining
Why does this Flask app not serve updated static files immediately?

A developer updates a CSS file in the static folder but the browser still shows the old style. What is the most likely cause?

AThe static folder is misnamed; Flask cannot find the updated file.
BFlask does not reload static files automatically; the server must restart.
CBrowser cache is serving the old file; cache headers need adjustment or cache busting.
DThe CSS file is not linked correctly in the HTML template.
Attempts:
2 left
💡 Hint

Think about how browsers handle static files and caching.

🧠 Conceptual
expert
2:30remaining
What is the best practice to optimize static file delivery in Flask for production?

Which approach is recommended to optimize static file delivery in a Flask production environment?

AUse a dedicated web server like Nginx or a CDN to serve static files, offloading Flask.
BDisable caching entirely to ensure users always get the latest static files.
CEmbed static files inside Flask routes and serve them dynamically on each request.
DServe static files directly from Flask using its built-in server with cache headers set.
Attempts:
2 left
💡 Hint

Consider performance and scalability for production environments.