📖 Scenario: You are building a simple web app using Flask that shows a picture on a webpage. The picture file is stored in a folder inside your project.
🎯 Goal: Create a Flask app that serves an image file from a folder called static/images and displays it on the homepage.
📋 What You'll Learn
Create a Flask app instance
Set up a route for the homepage at /
Use the url_for function to link to the image in the template
Store the image file in static/images folder
Render an HTML template that shows the image
💡 Why This Matters
🌍 Real World
Serving images is a common need in web apps for user profiles, product pictures, or blog posts.
💼 Career
Knowing how to serve static files like images is essential for backend web developers working with Flask or similar frameworks.
Progress0 / 4 steps
1
Create the Flask app and homepage route
Write code to import Flask, create an app instance called app, and define a route for / that returns the text 'Hello, Flask!'.
Flask
Hint
Start by importing Flask and creating the app instance. Then use @app.route('/') to make the homepage.
2
Add a configuration variable for the image filename
Create a variable called image_filename and set it to the string 'cat.jpg'. This will be the image file you want to serve.
Flask
Hint
Just create a variable named image_filename and assign the exact string 'cat.jpg'.
3
Use url_for to link the image in the homepage route
Import url_for from flask. Change the home function to return an HTML string that includes an <img> tag. Use url_for('static', filename='images/' + image_filename) as the src attribute value.
Flask
Hint
Use an f-string to build the HTML string. The src attribute should call url_for('static', filename='images/' + image_filename).
4
Add the static folder and place the image file
Create a folder named static/images in your project directory. Place the image file named cat.jpg inside static/images. This allows Flask to serve the image file when requested.
Flask
Hint
Flask automatically serves files from the static folder. Make sure your image is inside static/images/cat.jpg.
Practice
(1/5)
1. What is the main purpose of using send_from_directory in a Flask app when serving images?
easy
A. To send image files from a specific folder to the user's browser
B. To create new image files dynamically
C. To convert images to text format
D. To upload images from the user to the server
Solution
Step 1: Understand the role of send_from_directory
This function is designed to send files stored in a folder on the server to the client browser.
Step 2: Identify its use with images
When serving images, it helps Flask deliver image files from a folder to display in the browser.
Final Answer:
To send image files from a specific folder to the user's browser -> Option A
Quick Check:
send_from_directory serves files = D [OK]
Hint: Remember: send_from_directory sends files from folders [OK]
Common Mistakes:
Thinking it creates or modifies images
Confusing it with file upload functions
Assuming it converts image formats
2. Which of the following is the correct way to import send_from_directory in a Flask app?
easy
A. from flask import send_file
B. from flask import send_from_directory
C. import send_from_directory from flask
D. from flask.static import send_from_directory
Solution
Step 1: Recall Flask import syntax
Flask functions are imported using from flask import function_name.
Step 2: Match the function name
The correct function name is send_from_directory, so the import is from flask import send_from_directory.
Final Answer:
from flask import send_from_directory -> Option B
Quick Check:
Correct import syntax = B [OK]
Hint: Use 'from flask import function' to import Flask helpers [OK]
Common Mistakes:
Using 'import send_from_directory from flask' (wrong syntax)
Importing send_file instead of send_from_directory
Importing from flask.static which doesn't contain it
3. Given this Flask route code, what will happen when accessing /image/cat.png if the file exists in the 'static/images' folder?
D. The folder name 'image' is likely incorrect; should be 'images'
Solution
Step 1: Check folder name consistency
The route suggests images are in 'images' folder, but code uses 'image' (singular), likely a typo causing file not found.
Step 2: Verify other parts
Imports and route syntax are correct; function name is valid in Python.
Final Answer:
The folder name 'image' is likely incorrect; should be 'images' -> Option D
Quick Check:
Folder name mismatch causes errors = C [OK]
Hint: Check folder names carefully for typos [OK]
Common Mistakes:
Assuming function names must be special
Thinking route syntax is wrong
Ignoring folder name typos
5. You want to serve user-uploaded images stored in a folder outside your Flask app directory called /var/www/uploads. Which is the best way to serve these images securely?
hard
A. Directly link to the folder path in HTML without a Flask route
B. Copy images into the app's static folder and serve from there
C. Use send_from_directory('/var/www/uploads', filename) in a route with filename validation