Static file serving lets your web app show images, styles, and scripts to users. Without it, your pages would look plain and not work well.
Why static file serving matters in Flask
from flask import Flask app = Flask(__name__, static_folder='static') @app.route('/') def home(): return '<img src="/static/logo.png" alt="Logo">' if __name__ == '__main__': app.run()
Flask automatically serves files from the static folder.
You link to static files using the /static/filename URL path.
static to serve files.app = Flask(__name__)
# Default static folder is 'static'static_folder.app = Flask(__name__, static_folder='assets')/static/ path in your HTML to link static files.<link rel="stylesheet" href="/static/style.css">
This Flask app serves an HTML page that uses a CSS file and an image from the static folder. Flask automatically handles sending these files when the browser requests them.
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return ''' <html lang="en"> <head> <link rel="stylesheet" href="/static/style.css"> </head> <body> <h1>Welcome!</h1> <img src="/static/logo.png" alt="Logo"> </body> </html> ''' if __name__ == '__main__': app.run(debug=True)
Put all your static files like images, CSS, and JavaScript inside the static folder.
Flask serves static files automatically without extra code.
For production, use a web server like Nginx to serve static files faster.
Static file serving is essential to make web pages look good and work well.
Flask makes it easy by serving files from the static folder automatically.
Use the /static/ URL path to link your static files in HTML.
Practice
static folder?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 thestaticfolder so the browser can load them easily.Final Answer:
To easily provide images, CSS, and JavaScript files needed by web pages -> Option BQuick Check:
Static files = images, CSS, JS served automatically [OK]
- Confusing static files with database data
- Thinking static files run Python code
- Believing static files store user input
style.css located in Flask's static folder inside an HTML file?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 tostyle.cssin the static folder is/static/style.css.Final Answer:
<link rel="stylesheet" href="/static/style.css"> -> Option CQuick Check:
Static files URL = /static/filename [OK]
- Using /templates/ instead of /static/
- Omitting the leading slash in href
- Linking directly without /static/ prefix
http://localhost:5000/static/logo.png?
Folder structure:
- static/
- logo.png
Code:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return 'Home Page'Solution
Step 1: Understand Flask's static file serving default
Flask automatically serves files from thestaticfolder at the URL path/static/without needing explicit routes.Step 2: Analyze the request URL
Requesting/static/logo.pngmatches the static folder filelogo.png, so Flask serves it directly.Final Answer:
The image file logo.png will be served correctly -> Option DQuick Check:
Static folder files served automatically [OK]
- Thinking static files need explicit routes
- Confusing static URL with home route
- Expecting 404 due to missing route
<script src="static/app.js"></script>But the script does not load. What is the likely problem?
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
Usingstatic/app.jswithout 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 AQuick Check:
Static URLs need leading slash [OK]
- Putting JS files in templates folder
- Thinking Flask blocks JavaScript
- Using incorrect script tag syntax
assets instead of the default static folder in Flask. Which code change correctly sets this up?Solution
Step 1: Understand Flask static folder customization
Flask allows changing the static folder by passing thestatic_folderparameter when creating the app.Step 2: Identify correct syntax to set static folder
Usingapp = Flask(__name__, static_folder='assets')tells Flask to serve static files from theassetsfolder instead ofstatic.Final Answer:
app = Flask(__name__, static_folder='assets') -> Option AQuick Check:
Set static_folder param to change static folder [OK]
- Trying to set app.static_folder after app creation
- Confusing static_url_path with static_folder
- Adding route manually for static files
Ready to prove it?
Pass the test and this pattern moves unknown
