0
0
Flaskframework~20 mins

Serving CSS files in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CSS Serving Mastery in Flask
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
How does Flask serve CSS files by default?

In a Flask app, you want to add a CSS file to style your pages. Where should you place the CSS file so Flask can serve it automatically?

APlace the CSS file inside a folder named <code>static</code> in your project root.
BPlace the CSS file inside the <code>templates</code> folder.
CPlace the CSS file in the same folder as your main Python script.
DPlace the CSS file inside a folder named <code>css</code> anywhere in your project.
Attempts:
2 left
💡 Hint

Flask has a special folder for static files like CSS and images.

📝 Syntax
intermediate
1:30remaining
Correct way to link CSS in Flask template

You have a CSS file named style.css inside the static folder. Which HTML line correctly links this CSS file in a Flask Jinja2 template?

A<link rel="stylesheet" href="{{ static('style.css') }}">
B<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
C<link rel="stylesheet" href="static/style.css">
D<link rel="stylesheet" href="/static/style.css">
Attempts:
2 left
💡 Hint

Use Flask's url_for function to generate URLs for static files.

🔧 Debug
advanced
2:00remaining
Why is the CSS not loading in this Flask app?

Consider this Flask app structure:

project/
 ├── app.py
 ├── static/
 │    └── main.css
 └── templates/
      └── index.html

In index.html, the CSS is linked as:

<link rel="stylesheet" href="/main.css">

Why does the CSS not load when you open the page?

AThe CSS file must be inside the <code>templates</code> folder to be served.
BThe CSS file name is incorrect; it should be <code>style.css</code>.
CThe href should be <code>/static/main.css</code> or use <code>url_for</code> to generate the URL.
DFlask does not serve CSS files by default; you must write a route to serve them.
Attempts:
2 left
💡 Hint

Static files are served from the /static/ URL path by default.

state_output
advanced
1:30remaining
What is the URL generated by url_for for a CSS file?

Given a Flask app with a CSS file theme.css inside the static folder, what URL does this Jinja2 code generate?

{{ url_for('static', filename='theme.css') }}
A/static/theme.css
B/theme.css
C/staticfiles/theme.css
D/assets/theme.css
Attempts:
2 left
💡 Hint

By default, Flask serves static files under the /static/ URL path.

🧠 Conceptual
expert
2:30remaining
How to serve CSS files from a custom static folder in Flask?

You want to serve CSS files from a folder named assets instead of the default static folder in Flask. Which approach correctly configures Flask to serve CSS files from assets?

ARename the <code>assets</code> folder to <code>static</code> because Flask does not allow custom static folders.
BCreate a route to serve CSS files manually from <code>assets</code> using <code>@app.route('/assets/&lt;path:filename&gt;')</code>.
CSet <code>app.static_url_path = '/assets'</code> after app creation and keep CSS files in <code>static</code>.
DInitialize Flask with <code>app = Flask(__name__, static_folder='assets')</code> and place CSS files inside <code>assets</code>.
Attempts:
2 left
💡 Hint

Flask's constructor accepts a static_folder parameter to change the static files folder.