0
0
Flaskframework~5 mins

Static file organization in Flask - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the static folder in a Flask project?
The static folder holds files like images, CSS, and JavaScript that the browser can load directly. It keeps these files separate from Python code for better organization.
Click to reveal answer
beginner
How do you reference a static file in a Flask HTML template?
Use the url_for('static', filename='path/to/file') function. For example, <img src="{{ url_for('static', filename='images/logo.png') }}"> loads an image from the static folder.
Click to reveal answer
beginner
Why should static files be organized into subfolders like css, js, and images inside the static folder?
Organizing static files into subfolders helps keep the project tidy and makes it easier to find and manage files, just like sorting your documents into labeled folders at home.
Click to reveal answer
beginner
What happens if you place a CSS file outside the static folder in Flask?
Flask will not serve the CSS file automatically, so the browser won’t load it. Static files must be inside the static folder to be accessible via URLs.
Click to reveal answer
intermediate
How can you customize the static folder location in a Flask app?
You can set the static_folder parameter when creating the Flask app, like app = Flask(__name__, static_folder='assets'). This changes where Flask looks for static files.
Click to reveal answer
Where should you put JavaScript files in a Flask project?
AInside the <code>static/js</code> folder
BInside the <code>templates</code> folder
CIn the root project folder
DInside the <code>instance</code> folder
How do you link a CSS file named style.css located in static/css in a Flask template?
A&lt;link href="/css/style.css"&gt;
B&lt;link href="{{ url_for('static', filename='css/style.css') }}"&gt;
C&lt;link href="static/css/style.css"&gt;
D&lt;link href="{{ url_for('templates', filename='css/style.css') }}"&gt;
What is the default name of the folder Flask uses to serve static files?
Aassets
Bpublic
Cstatic
Dmedia
If you want to change the static folder to assets, how do you do it?
Aapp = Flask(__name__, static_folder='assets')
Bapp.static_folder = 'assets'
CSet STATIC_FOLDER = 'assets' in config
DRename the <code>static</code> folder to <code>assets</code> without code changes
Why is it important to keep static files separate from Python code in Flask?
ATo make the app run faster
BBecause Python cannot read static files
CTo prevent static files from running as code
DTo improve code readability and browser access
Explain how to organize and reference static files in a Flask project.
Think about how you keep your desk tidy with folders and labels.
You got /3 concepts.
    Describe what happens if a static file is placed outside the static folder in Flask.
    Imagine trying to find a book that is not on the shelf where you expect it.
    You got /3 concepts.