0
0
Flaskframework~20 mins

Static folder configuration in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Static Folder Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the URL to access a static file?
Given a Flask app with the default static folder named 'static', what URL path will serve the file located at static/images/logo.png?
Flask
from flask import Flask
app = Flask(__name__)

# File located at static/images/logo.png

@app.route('/')
def home():
    return 'Home Page'

if __name__ == '__main__':
    app.run()
A/static/images/logo.png
B/images/logo.png
C/static/logo.png
D/assets/images/logo.png
Attempts:
2 left
💡 Hint
Flask serves static files from the folder named 'static' by default under the '/static' URL prefix.
📝 Syntax
intermediate
2:00remaining
How to change the static folder name in Flask?
Which code snippet correctly changes the static folder from the default 'static' to 'assets' in a Flask app?
Aapp = Flask(__name__, static_folder='/assets')
Bapp = Flask(__name__, static_folder='assets')
Capp = Flask(__name__, static_url_path='assets')
Dapp = Flask(__name__, static_path='assets')
Attempts:
2 left
💡 Hint
The parameter to set the folder name is 'static_folder'.
🔧 Debug
advanced
2:00remaining
Why does Flask not serve static files from a custom folder?
A developer sets app = Flask(__name__, static_folder='public') but accessing /public/style.css returns 404. What is the cause?
AThe static_url_path must be set to '/public' to match the folder name.
BThe URL path to static files is still '/static' by default, but the folder is 'public', so the URL should be '/public/style.css'.
CFlask requires the static folder to be named 'static' and cannot be changed.
DThe static_folder parameter must be an absolute path, not a relative one.
Attempts:
2 left
💡 Hint
Changing the folder does not change the URL path unless you also set static_url_path.
state_output
advanced
2:00remaining
What is the value of static_url_path after app creation?
Given app = Flask(__name__, static_folder='assets', static_url_path='/content'), what is the value of app.static_url_path?
Flask
from flask import Flask
app = Flask(__name__, static_folder='assets', static_url_path='/content')
print(app.static_url_path)
A/assets
B/
C/static
D/content
Attempts:
2 left
💡 Hint
static_url_path sets the URL prefix for static files.
🧠 Conceptual
expert
2:00remaining
Why use a custom static_url_path in Flask?
Which reason best explains why a developer might set a custom static_url_path different from the default '/static'?
ABecause Flask requires a custom static_url_path for production environments.
BTo make Flask serve static files faster by changing the URL path.
CTo avoid URL conflicts with other routes or external services using '/static'.
DTo automatically compress static files when served.
Attempts:
2 left
💡 Hint
Think about URL naming conflicts in web apps.