
Chapter Outline
Chapter 1: Introduction to Web Development with Python
In this chapter, we set the stage for building modern web applications with Python. You will learn:
- The client-server model and how web apps communicate.
- The basics of HTTP (requests, responses, headers, status codes).
- Why Python is a strong choice for web development (FastAPI, Django).
- How to set up your first web project environment with Poetry, MyPy, and Pytest.
- How to run a hello world FastAPI server and test it.
This foundation ensures that all upcoming chapters build on a clean, reproducible, and testable development environment.
1.1 The Client-Server Model Refresher
A web app is fundamentally about clients (browsers, mobile apps, scripts) talking to a server (your backend app) over the internet.
The following diagram depicts the interaction between a client and a server, in the context of a Python web application.
Explanation:
- The client sends an HTTP request.
- The server processes it and fetches data from the database.
- The database responds.
- The server formats the response (e.g., JSON) and sends it back to the client.
1.2 Why Python for Web Development?
Python is one of the most popular languages for web apps because:
- FastAPI → modern, async-first, great for APIs.
- Django → batteries-included, great for full-stack apps.
- Large ecosystem → ORMs, testing libraries, data/ML integration.
- Developer productivity → readable syntax, strong typing (via MyPy), excellent testing tools.
1.3 Project Setup with Poetry, MyPy, and Pytest
We’ll use Poetry as our dependency manager, and from the start integrate MyPy for type checking and Pytest for testing.
Step 1: Initialize a new project
bashmkdir fastapi-todocd fastapi-todopoetry init --name fastapi-todo --dependency fastapi --dependency uvicorn
Add dev tools:
bashpoetry add --group dev pytest mypy httpx
Step 2: Basic project structure
bashfastapi-todo/├── pyproject.toml├── fastapi_todo/│ └── main.py└── tests/└── test_main.py
1.4 Hello World in FastAPI
fastapi_todo/main.py1from fastapi import FastAPI23app = FastAPI()456@app.get("/")7def read_root() -> dict[str, str]:8 """Root endpoint returning a welcome message.910 Returns:11 dict[str, str]: JSON response with a message.12 """13 return {"message": "Hello, FastAPI world!"}
Explanation:
FastAPI()creates an app instance.@app.get("/")defines a route (GET /).read_rootreturns a JSON dictionary.- Type hints (
dict[str, str]) allow MyPy to catch typing issues.
Run it with:
bashpoetry run uvicorn fastapi_todo.main:app --reload
Visit: http://127.0.0.1:8000/ → you’ll see {"message": "Hello, FastAPI world!"}.
1.5 Writing Tests with Pytest & HTTPX
tests/test_main.py1from fastapi.testclient import TestClient2from fastapi_todo.main import app34client = TestClient(app)567def test_read_root() -> None:8 """Test that the root endpoint returns the correct JSON response."""9 response = client.get("/")10 assert response.status_code == 20011 assert response.json() == {"message": "Hello, FastAPI world!"}
Explanation:
TestClientsimulates HTTP requests.- We assert status code (
200 OK) and response body. - Run tests:
bashpoetry run pytest
1.6 Static Type Checking with MyPy
Run:
bashpoetry run mypy fastapi_todo
If all type hints are correct, MyPy will report no errors. This helps catch mismatches before runtime.
1.7 Further Reading / References
Check your understanding
Test your knowledge of Introduction to Web Development with Python