📖 Scenario: You are building a simple Flask web app that needs to show a logo image and a CSS style to make the page look nice. These files are called static files because they do not change dynamically. Serving static files correctly is important so your users see the images and styles properly.
🎯 Goal: Create a Flask app that serves a static image and a CSS file. You will set up the static folder, configure the app to use it, and link the static files in your HTML template.
📋 What You'll Learn
Create a Flask app with a static folder named static
Add an image file named logo.png inside the static folder
Add a CSS file named style.css inside the static folder
Serve the static files correctly in the Flask app
Link the static files in the HTML template using Flask's url_for function
💡 Why This Matters
🌍 Real World
Web apps often need to serve images, styles, and scripts to users. Knowing how to serve static files ensures your app looks good and works well.
💼 Career
Web developers must understand static file serving to build professional websites and apps that load resources efficiently and correctly.
Progress0 / 4 steps
1
Set up the Flask app and static folder
Create a Flask app in a file named app.py with the line app = Flask(__name__). Also, create a folder named static in the same directory. Inside the static folder, place an image file named logo.png.
Flask
Hint
Remember, Flask automatically looks for a folder named static for static files.
2
Add a CSS file in the static folder
Inside the static folder, create a CSS file named style.css with some basic style, for example, body { background-color: lightblue; }.
Flask
Hint
The CSS file should be inside the static folder, not in the main app folder.
3
Create a route and HTML template linking static files
In app.py, create a route for / that returns an HTML string. Use Flask's url_for('static', filename='logo.png') to link the image and url_for('static', filename='style.css') to link the CSS file inside the HTML.
Flask
Hint
Use url_for('static', filename='...') inside the HTML string to link static files.
4
Run the Flask app with debug mode
Add the code to run the Flask app with app.run(debug=True) inside the if __name__ == '__main__': block at the end of app.py.
Flask
Hint
This code lets you run the Flask app and see changes immediately because debug mode is on.
Practice
(1/5)
1. Why does Flask serve static files automatically from the static folder?
easy
A. To store user data securely
B. To easily provide images, CSS, and JavaScript files needed by web pages
C. To run Python code faster
D. To handle database connections
Solution
Step 1: Understand what static files are
Static files include images, CSS, and JavaScript that make web pages look and behave well.
Step 2: Recognize Flask's role in serving these files
Flask automatically serves files from the static folder so the browser can load them easily.
Final Answer:
To easily provide images, CSS, and JavaScript files needed by web pages -> Option B
Quick Check:
Static files = images, CSS, JS served automatically [OK]
2. Which of the following is the correct way to link a CSS file named style.css located in Flask's static folder inside an HTML file?
easy
A.
B.
C.
D.
Solution
Step 1: Recall Flask static file URL pattern
Flask serves static files under the URL path starting with /static/.
Step 2: Match the correct href attribute
The correct link to style.css in the static folder is /static/style.css.
Final Answer:
<link rel="stylesheet" href="/static/style.css"> -> Option C
Quick Check:
Static files URL = /static/filename [OK]
Hint: Static files always use /static/ URL prefix [OK]
Common Mistakes:
Using /templates/ instead of /static/
Omitting the leading slash in href
Linking directly without /static/ prefix
3. Given this Flask route and folder structure, what will happen when accessing http://localhost:5000/static/logo.png?
Folder structure:
- static/
- logo.png
Code:
4. You wrote this HTML inside a Flask template to load a JavaScript file: <script src="static/app.js"></script> But the script does not load. What is the likely problem?
medium
A. Missing leading slash in the src URL, should be '/static/app.js' instead
B. The JavaScript file must be inside the templates folder
C. Flask does not support JavaScript files
D. The script tag should be self-closing like
Solution
Step 1: Check the URL path for static files
Flask serves static files under the URL path starting with a leading slash, e.g., /static/app.js.
Step 2: Identify the missing slash issue
Using static/app.js without the leading slash makes the browser look for a relative path, causing the file not to load.
Final Answer:
Missing leading slash in the src URL, should be '/static/app.js' instead -> Option A
Quick Check:
Static URLs need leading slash [OK]
Hint: Always start static URLs with /static/ [OK]
Common Mistakes:
Putting JS files in templates folder
Thinking Flask blocks JavaScript
Using incorrect script tag syntax
5. You want to serve a custom static folder named assets instead of the default static folder in Flask. Which code change correctly sets this up?
hard
A. app = Flask(__name__, static_folder='assets')
B. app = Flask(__name__)
app.static_folder = 'assets'
C. app = Flask(__name__)
app.route('/assets/')
D. app = Flask(__name__, static_url_path='/assets')