Backend Step 1: Project Setup
In this step you'll set up your Python development environment and install the tools needed for the backend.
What You'll Learn
- Creating a Python virtual environment
- Installing packages with pip
- Understanding the project structure
- Designing your API before writing code
Design First
Before you write a single line of code, sketch your API on paper or in a text file. This is called design-first development — you plan what your API will do before you build it.
For this chat app, your API will need:
| Endpoint | Method | What it does |
|---|---|---|
GET /api/status |
GET | Health check — returns {"status":"ok"} |
GET /api/messages |
GET | Get all chat messages |
POST /api/chat |
POST | Send a message, get AI reply |
DELETE /api/messages/<id> |
DELETE | Delete a specific message |
GET /api/health |
GET | Detailed health info (database, version, metrics) |
GET /api/metrics |
GET | Request count and performance data |
Why design first? It gives you a roadmap. You know exactly what to build, in what order. You can spot missing pieces before you start coding.
Step 1: Create the Project Structure
Create the following folder structure:
aiproject/
├── backend/
│ ├── app/
│ │ ├── __init__.py
│ │ ├── models.py
│ │ ├── routes.py
│ │ ├── config.py
│ │ ├── errors.py
│ │ ├── logging_config.py
│ │ ├── metrics.py
│ │ ├── requirements.txt
│ │ └── .env.example
│ ├── tests/
│ │ ├── __init__.py
│ │ ├── conftest.py
│ │ ├── test_status.py
│ │ ├── test_chat.py
│ │ ├── test_messages.py
│ │ └── test_health.py
│ ├── Dockerfile
│ ├── Dockerfile.prod
│ ├── docker-compose.yml
│ └── wsgi.py
├── advanced/
├── concepts/
├── deployment/
├── ...
Run:
mkdir -p aiproject/backend/app aiproject/backend/tests
cd aiproject/backend
Step 2: Choose Your Setup Method
You have two ways to run the backend: virtual environment (standard Python) or Docker (containerized). Both produce the same result — pick the one you're more comfortable with.
Option A: Virtual Environment (Standard)
This is the classic Python approach. You need Python 3.10+ installed on your machine.
Step 2A: Create a Virtual Environment
python3 -m venv venv
Activate it:
# macOS / Linux
source venv/bin/activate
# Windows (Command Prompt)
venv\Scripts\activate
# Windows (PowerShell)
venv\Scripts\Activate.ps1
Your terminal prompt should now show (venv) at the beginning.
Step 3A: Create requirements.txt
Create backend/app/requirements.txt:
flask
flask-sqlalchemy
flask-cors
python-dotenv
openai
Step 4A: Install Dependencies
pip install -r app/requirements.txt
Verify they installed:
pip list
You should see flask, flask-sqlalchemy, flask-cors, python-dotenv, and openai in the list.
Option B: Docker (Containerized)
Docker runs your app in an isolated container — no need to install Python or manage virtual environments.
Step 2B: Install Docker
- macOS: Download Docker Desktop for Mac
- Windows: Download Docker Desktop for Windows
- Linux: Run your package manager — e.g.
sudo apt install docker.io docker-compose-v2
Verify Docker is working:
docker --version
docker compose version
Step 3B: Build and Run with Docker
cd aiproject/backend
docker compose build
docker compose up
The first build downloads the Python image and installs dependencies — this takes a minute. Subsequent starts are instant.
You'll see:
app-1 | * Running on http://127.0.0.1:5000
The container uses the .env file from backend/app/.env automatically (you'll create this in Step 5 below). Any code changes you make are reflected immediately thanks to the live-reload volume mount.
To stop: press Ctrl+C.
Tip: SQLite data persists in a Docker volume. To reset the database: docker compose down -v
Proceed with Either Option
The remaining steps work identically for both venv and Docker. The only difference is how you start the server:
| Action | Venv | Docker |
|---|---|---|
| Install dependencies | pip install -r app/requirements.txt |
docker compose build |
| Start server | source venv/bin/activate && python app/__init__.py |
docker compose up |
| Stop server | Ctrl+C |
Ctrl+C |
Step 5: Create .env.example
Create backend/app/.env.example:
OPENAI_API_KEY=sk-your-key-here
This is a template. When you get your actual API key, you'll copy this to .env and replace the value.
Step 6: Create init.py (empty for now)
touch app/__init__.py
touch app/models.py
touch app/routes.py
What Just Happened?
- Virtual environment isolates your project's packages from the rest of your system
- requirements.txt lists your dependencies so anyone can install them with one command
- .env.example is a template for required environment variables (never commit the real
.env)
What Is Ruff?
Ruff is a linter — a tool that checks your code for errors, unused imports, and style issues before you run it.
Instead of finding bugs at runtime (when your app crashes), ruff finds them at edit time:
ruff check app/
# → app/routes.py:5:1: F401 `os` imported but unused
One command catches many common mistakes. You'll use it starting in Step 2.
Install it now:
pip install ruff
Verification
Run this to confirm everything is installed:
python3 -c "import flask; import flask_sqlalchemy; import flask_cors; print('All imports work!')"
Expected output: All imports work!
Option B (Docker)
The build succeeded if docker compose build completed without errors. Run a quick check:
docker compose run app python -c "import flask; import flask_sqlalchemy; import flask_cors; print('All imports work!')"
Expected output: All imports work!