Flask Setup: Building Your First Web Kitchen 🍳
Imagine you want to open a tiny restaurant. Before cooking anything, you need:
- A kitchen (your computer setup)
- A clean workspace (virtual environment)
- Organized shelves (project structure)
- A recipe book ready (Flask app instance)
- A test stove (development server)
- A helper who tells you if something burns (debug mode)
Let’s build your Flask kitchen step by step!
1. Installing Flask
What Is Flask?
Flask is like a tiny cookbook for building websites. It’s small, simple, and lets you cook (code) however you like!
How to Install It
Open your terminal (the black screen where you type commands) and type:
pip install flask
What just happened?
pipis like a delivery service- It went to the internet
- Found Flask
- Downloaded it to your computer
Check If It Worked
pip show flask
You should see Flask’s details. If you do—great! Your cookbook arrived!
2. Python Virtual Environments
The Problem
Imagine you have two recipes:
- Recipe A needs 1 cup sugar
- Recipe B needs 2 cups sugar
If you mix them up, disaster! Same with Python projects.
The Solution: Virtual Environments
A virtual environment is like a separate kitchen for each project. Each kitchen has its own ingredients (packages).
graph TD A[Your Computer] --> B[Kitchen 1: Blog Project] A --> C[Kitchen 2: Game Project] A --> D[Kitchen 3: Flask Project] B --> E[Flask 2.0] C --> F[Flask 1.0] D --> G[Flask 3.0]
Create Your Own Kitchen
Step 1: Create the kitchen
python -m venv myenv
Step 2: Walk into the kitchen
On Windows:
myenv\Scripts\activate
On Mac/Linux:
source myenv/bin/activate
Step 3: You’ll see (myenv) before your prompt
(myenv) C:\Users\You>
This means you’re inside your special kitchen!
Why This Matters
| Without Virtual Env | With Virtual Env |
|---|---|
| Projects share packages | Each project has its own |
| Updates break old projects | Projects stay safe |
| Messy | Clean and organized |
3. Flask Project Structure
The Simple Version
For a tiny Flask app, you only need one file:
my_project/
└── app.py
That’s it! Just one file named app.py.
The Bigger Version
When your app grows, organize it like this:
my_project/
├── app.py (main recipe)
├── templates/ (HTML pages)
│ └── home.html
├── static/ (images, CSS)
│ ├── style.css
│ └── logo.png
└── requirements.txt (ingredient list)
graph TD A[my_project folder] --> B[app.py] A --> C[templates folder] A --> D[static folder] A --> E[requirements.txt] C --> F[HTML files] D --> G[CSS, Images, JS]
What Each Part Does
| Folder/File | Purpose | Like… |
|---|---|---|
app.py |
Your main code | The head chef |
templates/ |
HTML pages | Menu designs |
static/ |
Images, CSS | Decorations |
requirements.txt |
Package list | Shopping list |
4. Creating Flask App Instance
What’s an “Instance”?
When you say “I want to build a website,” Flask says “Okay, let me create one for you!”
That creation is called an instance—your actual app.
The Magic Code
Create a file called app.py and write:
from flask import Flask
app = Flask(__name__)
Line by line:
-
from flask import Flask- “Hey, bring me the Flask cookbook!”
-
app = Flask(__name__)- “Create my restaurant and name it!”
__name__tells Flask “this file is the boss”
Add Your First Page
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return 'Hello, World!'
What’s happening:
@app.route('/')= “When someone visits the main page…”def home():= “…run this function”return 'Hello, World!'= “…and show this message”
graph LR A[User visits /] --> B[@app.route catches it] B --> C[home function runs] C --> D[Hello World appears!]
5. Running the Development Server
What’s a Development Server?
It’s like a practice kitchen that only you can see. You test your recipes before opening the real restaurant.
Start Your Server
Add this to the bottom of app.py:
if __name__ == '__main__':
app.run()
Now run your file:
python app.py
What You’ll See
* Running on http://127.0.0.1:5000
Open your browser and go to: http://127.0.0.1:5000
You’ll see: Hello, World!
🎉 Your first website is alive!
Understanding the Address
| Part | Meaning |
|---|---|
127.0.0.1 |
Your own computer |
5000 |
Door number (port) |
It’s like saying: “My computer, room 5000”
Stop the Server
Press Ctrl + C in the terminal.
6. Debug Mode
The Problem
You make a mistake in your code. The website just shows “Error.”
Not helpful! What went wrong? Where?
The Solution: Debug Mode
Debug mode is like having a helpful assistant who:
- Shows exactly what went wrong
- Points to the exact line
- Auto-restarts when you change code
Turn On Debug Mode
Method 1: In your code
if __name__ == '__main__':
app.run(debug=True)
Method 2: Using environment variable
export FLASK_DEBUG=1
flask run
What Debug Mode Does
graph TD A[Debug Mode ON] --> B[Shows detailed errors] A --> C[Auto-reloads on changes] A --> D[Interactive debugger] B --> E[Find bugs fast!] C --> F[No manual restart!] D --> G[Test code in browser!]
Example Error Without Debug
Internal Server Error
Example Error With Debug
NameError: name 'hello' is not defined
File "app.py", line 7, in home
return hello
Now you know exactly where to look!
⚠️ Important Warning
NEVER use debug mode in production!
Debug mode shows your secret code to everyone. Only use it while building.
| Environment | Debug Mode |
|---|---|
| Development (your computer) | âś… ON |
| Production (real website) | ❌ OFF |
Complete Example: Your First Flask App
Here’s everything together:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return 'Welcome to my Flask app!'
@app.route('/about')
def about():
return 'This is the about page!'
if __name__ == '__main__':
app.run(debug=True)
Try It!
- Save as
app.py - Run:
python app.py - Visit:
http://127.0.0.1:5000 - Visit:
http://127.0.0.1:5000/about
Quick Summary
| Step | Command/Code | What It Does |
|---|---|---|
| Install Flask | pip install flask |
Downloads Flask |
| Create venv | python -m venv myenv |
Makes clean workspace |
| Activate venv | source myenv/bin/activate |
Enters the workspace |
| Create app | app = Flask(__name__) |
Makes your app |
| Add route | @app.route('/') |
Creates a page |
| Run server | app.run() |
Starts testing |
| Debug mode | app.run(debug=True) |
Shows helpful errors |
You Did It! 🎉
You just learned:
- âś… How to install Flask
- âś… Why virtual environments matter
- âś… How to organize your project
- âś… How to create a Flask app
- âś… How to run the development server
- âś… How to use debug mode
Your Flask kitchen is ready. Time to cook something amazing!
Remember: Every expert was once a beginner. You’re doing great! 🚀