0
0
Flaskframework~20 mins

Serving images in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Image Serving Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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')
AThe image file 'photo.jpg' is sent with correct headers so the browser displays it.
BThe raw bytes of the image are printed as text in the browser.
CA 404 error because 'send_from_directory' cannot serve files from 'static'.
DA redirect to the home page '/' occurs instead of sending the image.
Attempts:
2 left
💡 Hint
Flask's send_from_directory helps serve files from folders like 'static'.
📝 Syntax
intermediate
2: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?
Areturn send_from_directory('static/images', 'logo.png')
Breturn send_from_directory('images', 'logo.png')
Creturn send_file('images/logo.png')
Dreturn send_file('logo.png', directory='images')
Attempts:
2 left
💡 Hint
send_from_directory takes folder and filename separately.
🔧 Debug
advanced
2: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')
AFlask requires images to be in the 'images' folder, not 'static'.
Bsend_file cannot serve jpg files, only png files.
CThe file path is relative and Flask cannot find 'static/picture.jpg' because the working directory is different.
DThe route function is missing a return statement.
Attempts:
2 left
💡 Hint
Relative paths depend on where the app runs from.
state_output
advanced
2: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')
Aimage/jpeg
Btext/html
Capplication/octet-stream
Dimage/png
Attempts:
2 left
💡 Hint
send_file guesses the MIME type from the file extension.
🧠 Conceptual
expert
2: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.
AFlask's static folder is optimized for caching and efficient serving by the web server, reducing load on Flask app.
BCustom routes cannot serve image files at all due to Flask limitations.
CServing images via custom routes always causes security vulnerabilities.
DFlask automatically compresses images only when served from the static folder.
Attempts:
2 left
💡 Hint
Think about performance and web server features.