Challenge - 5 Problems
Image Serving Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Flask route serving an image?
Consider this Flask route that serves an image file from the 'static' folder. What will the browser receive when accessing '/image'?
Flask
from flask import Flask, send_from_directory app = Flask(__name__) @app.route('/image') def image(): return send_from_directory('static', 'photo.jpg')
Attempts:
2 left
💡 Hint
Flask's send_from_directory helps serve files from folders like 'static'.
✗ Incorrect
send_from_directory sends the file with proper content-type headers, so the browser shows the image.
📝 Syntax
intermediate2:00remaining
Which option correctly serves an image file in Flask?
You want to serve 'logo.png' from the 'images' folder inside your project. Which Flask route code is correct?
Attempts:
2 left
💡 Hint
send_from_directory takes folder and filename separately.
✗ Incorrect
send_from_directory('images', 'logo.png') correctly sends the file from 'images' folder. send_from_directory provides security against path traversal, unlike send_file with directory.
🔧 Debug
advanced2:00remaining
Why does this Flask code raise a FileNotFoundError when serving an image?
Look at this Flask route. It raises FileNotFoundError when accessed. Why?
Flask
from flask import Flask, send_file app = Flask(__name__) @app.route('/pic') def pic(): return send_file('static/picture.jpg')
Attempts:
2 left
💡 Hint
Relative paths depend on where the app runs from.
✗ Incorrect
send_file needs the correct absolute or relative path. If the working directory differs, the relative path 'static/picture.jpg' may not be found.
❓ state_output
advanced2:00remaining
What is the Content-Type header when serving a PNG image with send_file?
Given this Flask route serving 'icon.png' with send_file, what Content-Type header will the response have?
Flask
from flask import Flask, send_file app = Flask(__name__) @app.route('/icon') def icon(): return send_file('static/icon.png')
Attempts:
2 left
💡 Hint
send_file guesses the MIME type from the file extension.
✗ Incorrect
send_file sets Content-Type based on the file extension. For .png files, it sets 'image/png'.
🧠 Conceptual
expert2:00remaining
Why is it better to serve static images via Flask's 'static' folder than via a custom route?
Choose the best reason why using Flask's built-in static folder to serve images is preferred over writing custom routes to serve image files.
Attempts:
2 left
💡 Hint
Think about performance and web server features.
✗ Incorrect
The static folder is designed for static content and can be served directly by the web server with caching and compression, improving performance and reducing Flask app load.