📖 Scenario: You are creating a simple Flask web application for a small bookstore. You want to organize your project files properly so it is easy to maintain and expand later.
🎯 Goal: Build a basic Flask project structure with the main application file, a templates folder for HTML files, and a static folder for CSS and images.
📋 What You'll Learn
Create a main Flask application file named app.py
Create a folder named templates inside the project directory
Create a folder named static inside the project directory
Add a simple route in app.py that renders an HTML file from templates
💡 Why This Matters
🌍 Real World
Organizing Flask projects properly helps teams work together and makes it easier to add features or fix bugs later.
💼 Career
Understanding Flask project structure is essential for backend web development roles using Python and Flask.
Progress0 / 4 steps
1
Create the main Flask application file
Create a file named app.py and write the code to import Flask from flask and create an app instance called app.
Flask
Hint
Use from flask import Flask and then app = Flask(__name__) to create the app instance.
2
Create the templates and static folders
Create two folders named templates and static inside your project directory. These folders will hold HTML files and static assets like CSS and images respectively.
Flask
Hint
Use your file explorer or terminal commands to create templates and static folders next to app.py.
3
Add a route to render an HTML template
In app.py, import render_template from flask. Then add a route for / that returns render_template('index.html').
Flask
Hint
Use @app.route('/') decorator and define a function named home that returns render_template('index.html').
4
Add a simple HTML file in templates folder
Create a file named index.html inside the templates folder. Add a basic HTML5 structure with a <h1> heading that says Welcome to the Bookstore.
Flask
Hint
Use a basic HTML5 template with <h1> inside the body that says Welcome to the Bookstore.
Practice
(1/5)
1. Which folder is conventionally used to store HTML files in a Flask project?
easy
A. templates
B. static
C. app
D. config
Solution
Step 1: Understand Flask folder roles
Flask uses a folder named templates to hold HTML files for rendering views.
Step 2: Identify correct folder for HTML
Since HTML files are templates, they belong in the templates folder, not static or others.
Final Answer:
templates -> Option A
Quick Check:
HTML files go in templates [OK]
Hint: HTML files live in templates folder by Flask convention [OK]
Common Mistakes:
Confusing static folder for HTML files
Putting HTML files directly in app folder
Using config folder for templates
2. Which file is used to initialize a Flask app package inside the app folder?
easy
A. run.py
B. config.py
C. __init__.py
D. app.py
Solution
Step 1: Recognize Python package initialization
Python packages use __init__.py to initialize modules and setup code.
Step 2: Apply to Flask app folder
In Flask, the app folder is a package, so __init__.py initializes the Flask app instance.
Final Answer:
__init__.py -> Option C
Quick Check:
__init__.py initializes app package [OK]
Hint: Use __init__.py to initialize app package in Flask [OK]
Which file is typically used to start the Flask application?
medium
A. config.py
B. app/__init__.py
C. app/routes.py
D. run.py
Solution
Step 1: Identify the purpose of run.py
In Flask projects, run.py is commonly used as the entry point to start the app server.
Step 2: Differentiate from other files
config.py holds settings, __init__.py initializes app, and routes.py defines routes, but none start the server directly.
Final Answer:
run.py -> Option D
Quick Check:
run.py starts Flask app [OK]
Hint: run.py usually starts the Flask app server [OK]
Common Mistakes:
Trying to run config.py to start app
Running __init__.py directly
Running routes.py as main file
4. You have this run.py file:
from app import app
if __name__ == '__main__':
app.run(debug=true)
But running python run.py gives an ImportError: cannot import name 'app'. What is the likely cause?
medium
A. run.py is missing the Flask import statement
B. The 'app' variable is not defined in app/__init__.py
C. The templates folder is missing
D. Debug mode cannot be set in run.py
Solution
Step 1: Understand the import error
The error means app is not found inside the app package's __init__.py.
Step 2: Check app/__init__.py for app variable
Usually, app = Flask(__name__) is defined in __init__.py. If missing, import fails.
Final Answer:
The 'app' variable is not defined in app/__init__.py -> Option B
Quick Check:
app must be defined in __init__.py [OK]
Hint: Ensure app variable is defined in __init__.py before importing [OK]
Common Mistakes:
Assuming missing Flask import causes this error
Thinking templates folder affects imports
Believing debug mode setting causes import error
5. You want to organize a Flask project with multiple blueprints for different features. Which structure best supports this while following Flask conventions?