🔄 Quick Recap (Day 25)

  • You packaged a small project, built wheels/sdists, and learned how to install and (optionally) publish them.

🎯 What You’ll Learn Today

  1. What a web framework is and when to use one.

  2. The core idea behind Flask, Django, and FastAPI.

  3. How to spin up a tiny “Hello” app in each framework.

  4. How to run and test endpoints locally.

📖 Why Frameworks?

A framework gives you ready‑made building blocks for common tasks (routing, handling requests, rendering responses) so you focus on your app’s logic instead of reinventing the wheel.

  • Flask: Minimal, flexible—great for prototypes and small services.

  • Django: “Batteries‑included”—ORM, auth, admin, templates; great for full web apps.

  • FastAPI: Type‑hint‑first, fast, excellent for JSON APIs.

Tip: Use your Day 16 virtual environments so each project stays isolated.

🧪 Flask in 5 Minutes (Micro‑Framework)

When to reach for it: You want a small site or API with total flexibility and only the pieces you choose.

Install

pip install flask

Minimal app (app.py)

from flask import Flask
app = Flask(__name__)

@app.get("/")
def home():
    return "Hello, Flask!"

# Optional extra route
@app.get("/hello/<name>")
def hello(name):
    return f"Hi, {name}!"

Run

flask --app app run --reload

Visit: http://127.0.0.1:5000/

Expected output (browser):

Hello, Flask!

Try: http://127.0.0.1:5000/hello/YourNameHi, YourName!

🏗️ Django in 10 Minutes (Full‑Stack)

When to reach for it: You’re building a full web app with database, users, admin, templates—Django ships with all of this.

Install

pip install Django

Start a project

django-admin startproject mysite
cd mysite
python manage.py startapp core

Add a tiny view

Create core/views.py:

from django.http import HttpResponse

def hello(request):
    return HttpResponse("Hello, Django!")

Wire the URL

Create core/urls.py:

from django.urls import path
from .views import hello

urlpatterns = [
    path("", hello),
]

Edit mysite/urls.py to include the app’s URLs:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path("admin/", admin.site.urls),
    path("", include("core.urls")),
]

Finally, add "core" to INSTALLED_APPS in mysite/settings.py.

Run

python manage.py runserver

Visit: http://127.0.0.1:8000/

Expected output (browser):

Hello, Django!

⚡ FastAPI in 5 Minutes (Modern APIs)

When to reach for it: You’re building JSON APIs quickly, love type hints, and want automatic interactive docs.

Install

pip install fastapi uvicorn

Minimal app (main.py)

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"message": "Hello, FastAPI!"}

@app.get("/items/{item_id}")
def read_item(item_id: int, q: str | None = None):
    return {"item_id": item_id, "q": q}

Run

uvicorn main:app --reload

Visit: http://127.0.0.1:8000/

Expected output (JSON):

{"message": "Hello, FastAPI!"}

Bonus: Interactive docs at http://127.0.0.1:8000/docs.

🧭 Which One Should I Use?

  • Quick prototype / microservice? Flask or FastAPI.

  • Full website with auth, admin, ORM, templates? Django.

  • Pure API, clean types, great docs out‑of‑the‑box? FastAPI.

There’s no single “best”—choose what fits the project’s scope and your team’s skills.

🧙‍♂️ Take the Wand and Try Yourself

Goal: Spin up a tiny endpoint in each framework and verify it works.

  1. Flask: Create app.py as above → run flask --app app run --reload → open / and /hello/YourName.

  2. Django: Create project mysite and app core, add a hello view, wire URLs, run runserver, open /.

  3. FastAPI: Create main.py, run uvicorn main:app --reload, open / and /items/42?q=test.

Expected results (browser or curl):

Flask   → Hello, Flask!
Django  → Hello, Django!
FastAPI → {"message": "Hello, FastAPI!"}
/items/42?q=test → {"item_id": 42, "q": "test"}

Troubleshooting:

  • If a command isn’t found, confirm your virtual environment is active.

  • Port already in use? Stop prior servers or change the port (see each framework’s CLI help).

Up next: Day 27: Automation & Scripting — schedule scripts, run commands, and automate routine tasks.

Keep Reading