Introduction to Web Development with Python

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.

sequenceDiagram participant C as Client (Browser/App) participant S as Server (FastAPI/Django) participant DB as Database C->>S: HTTP Request (GET /todos) S->>DB: Query todos DB-->>S: Results S-->>C: HTTP Response (200 OK + JSON)

Explanation:

  1. The client sends an HTTP request.
  2. The server processes it and fetches data from the database.
  3. The database responds.
  4. 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

bash
mkdir fastapi-todo
cd fastapi-todo
poetry init --name fastapi-todo --dependency fastapi --dependency uvicorn

Add dev tools:

bash
poetry add --group dev pytest mypy httpx

Step 2: Basic project structure

bash
fastapi-todo/
├── pyproject.toml
├── fastapi_todo/
│ └── main.py
└── tests/
└── test_main.py

1.4 Hello World in FastAPI

fastapi_todo/main.py
1from fastapi import FastAPI
2
3app = FastAPI()
4
5
6@app.get("/")
7def read_root() -> dict[str, str]:
8 """Root endpoint returning a welcome message.
9
10 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_root returns a JSON dictionary.
  • Type hints (dict[str, str]) allow MyPy to catch typing issues.

Run it with:

bash
poetry 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.py
1from fastapi.testclient import TestClient
2from fastapi_todo.main import app
3
4client = TestClient(app)
5
6
7def test_read_root() -> None:
8 """Test that the root endpoint returns the correct JSON response."""
9 response = client.get("/")
10 assert response.status_code == 200
11 assert response.json() == {"message": "Hello, FastAPI world!"}

Explanation:

  • TestClient simulates HTTP requests.
  • We assert status code (200 OK) and response body.
  • Run tests:
bash
poetry run pytest

1.6 Static Type Checking with MyPy

Run:

bash
poetry 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

Feedback