📖 Scenario: You are building a simple Flask web app that serves static files like images and CSS from a folder named static.This is common in websites to keep files like pictures and styles separate from the main code.
🎯 Goal: Configure a Flask app to serve static files from the static folder and create a route that returns a simple HTML page referencing a static image.
📋 What You'll Learn
Create a Flask app with a static folder named static
Set a configuration variable to specify the static folder path
Create a route / that returns HTML referencing a static image
Ensure the static image is accessible via the URL /static/sample.png
💡 Why This Matters
🌍 Real World
Websites often need to serve images, styles, and scripts separately from the main code. Configuring a static folder in Flask helps organize these files and makes them accessible to users.
💼 Career
Knowing how to configure static files in Flask is essential for backend web developers to build maintainable and user-friendly web applications.
Progress0 / 4 steps
1
Create the Flask app and static folder
Create a Flask app instance named app and ensure there is a folder named static in your project directory.
Flask
Hint
Use Flask(__name__) to create the app instance.
2
Set the static folder configuration
Add a configuration variable app.static_folder and set it to the string 'static' to specify the folder for static files.
Flask
Hint
Assign the string 'static' to app.static_folder.
3
Create a route that returns HTML referencing a static image
Create a route / using @app.route('/') and define a function index() that returns a string of HTML. The HTML should include an <img> tag with src="/static/sample.png".
Flask
Hint
Use @app.route('/') and return a string with an <img> tag pointing to /static/sample.png.
4
Run the Flask app with debug mode
Add the code to run the Flask app with debug=True inside the if __name__ == '__main__': block.
Flask
Hint
Use the standard Python block if __name__ == '__main__': and call app.run(debug=True).
Practice
(1/5)
1. What is the default folder name Flask uses to serve static files like images and CSS?
easy
A. assets
B. static
C. public
D. files
Solution
Step 1: Recall Flask default static folder
Flask by default looks for a folder named static in your project to serve static files.
Step 2: Confirm other options are not defaults
Folders like assets, public, or files are common in other frameworks but not Flask's default.
Final Answer:
static -> Option B
Quick Check:
Default static folder = static [OK]
Hint: Remember Flask uses 'static' folder by default for static files [OK]
Common Mistakes:
Confusing 'assets' or 'public' as default static folder
Thinking Flask serves static files from root directory
Assuming no default static folder exists
2. Which of the following is the correct way to change the static folder name to 'assets' when creating a Flask app?
easy
A. app = Flask(__name__, static='assets')
B. app = Flask(static_folder='assets')
C. app = Flask(__name__, static_folder='assets')
D. app = Flask(__name__, static_url_path='assets')