0
0
Flaskframework~10 mins

Static file organization in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Static file organization
Start Flask App
Request for Static File
Flask looks in /static folder
Find file?
No404 Error
Yes
Serve file to browser
Browser displays static content
This flow shows how Flask serves static files by looking in the /static folder when a request for a static file comes in.
Execution Sample
Flask
from flask import Flask
app = Flask(__name__)

# Static files served from 'static' folder by default

@app.route('/')
def home():
    return '<img src="/static/logo.png">'
This Flask app serves an image from the static folder when the home page is visited.
Execution Table
StepActionFile RequestedFlask Folder CheckedFile Found?Response
1User visits '/'N/AN/AN/AReturn HTML with <img src='/static/logo.png'>
2Browser requests '/static/logo.png'/static/logo.png/staticYesServe logo.png file content
3Browser displays imageN/AN/AN/AImage shown on page
4User requests '/static/missing.css'/static/missing.css/staticNoReturn 404 Not Found
💡 Execution stops when file is served or 404 error is returned if file not found.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4
Requested FileNoneN/A/static/logo.png/static/missing.css
File FoundFalseN/ATrueFalse
ResponseNoneHTML with img tagFile content served404 Not Found
Key Moments - 2 Insights
Why does Flask look in the /static folder for static files?
Flask is configured by default to serve static files from the /static folder, so when a URL starts with /static, Flask checks this folder for the requested file (see execution_table rows 2 and 4).
What happens if the requested static file does not exist?
Flask returns a 404 Not Found error because it cannot find the file in the /static folder, as shown in execution_table row 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what response does Flask send when '/static/logo.png' is requested?
AServe logo.png file content
BReturn 404 Not Found
CReturn HTML with img tag
DRedirect to home page
💡 Hint
Check execution_table row 2 under 'Response'
At which step does Flask return a 404 error for a missing static file?
AStep 1
BStep 4
CStep 2
DStep 3
💡 Hint
Look at execution_table row 4 for the 'Response' column
If the static folder was renamed, how would the execution table change?
AFlask would find files in the new folder automatically
BFlask would serve files from both folders
CFlask would still look in /static and return 404 for all static files
DFlask would crash immediately
💡 Hint
Flask defaults to /static folder unless configured otherwise, see concept_flow
Concept Snapshot
Flask serves static files from the /static folder by default.
When a URL starts with /static, Flask looks inside this folder.
If the file exists, Flask sends it to the browser.
If not, Flask returns a 404 error.
Keep static files organized inside /static for easy access.
Full Transcript
This lesson shows how Flask handles static files. When a user visits a page with static content like images or CSS, the browser requests those files from URLs starting with /static. Flask then looks inside the /static folder in your project to find the requested file. If the file is found, Flask sends it back to the browser to display. If the file is missing, Flask returns a 404 error. This default behavior helps keep your static files organized and easy to serve.