Claude Code for Flask: Lightweight Web Framework in Python — Claude Skills 360 Blog
Blog / AI / Claude Code for Flask: Lightweight Web Framework in Python
AI

Claude Code for Flask: Lightweight Web Framework in Python

Published: June 17, 2028
Read time: 5 min read
By: Claude Skills 360

Flask is a lightweight WSGI web framework for Python. pip install flask. App: from flask import Flask; app = Flask(__name__). Route: @app.route("/users", methods=["GET"]). JSON response: from flask import jsonify; return jsonify({"key":"val"}), 200. Request body: from flask import request; data = request.get_json(). Form: request.form["field"]. Query: request.args.get("q",""). Path param: @app.route("/users/<int:user_id>"). Blueprint: from flask import Blueprint; bp = Blueprint("users", __name__, url_prefix="/users"); app.register_blueprint(bp). before_request: @app.before_request def auth(): .... after_request: @app.after_request def add_headers(resp): resp.headers["X-Custom"]="v"; return resp. abort: from flask import abort; abort(404). HTTPException: @app.errorhandler(404) def not_found(e): return jsonify({"error":"not found"}), 404. g: from flask import g; g.user = user. send_file: from flask import send_file; return send_file("path/to/file.pdf"). url_for: from flask import url_for; url_for("users.get_user", user_id=1). Factory: def create_app(config=None) -> Flask: app = Flask(__name__); app.config.from_object(config); ...return app. Session: from flask import session; session["user_id"] = uid; session.clear(). Run: flask --app app run --debug. Test: with app.test_client() as c: r = c.get("/users"); assert r.status_code == 200. Claude Code generates Flask REST APIs, Blueprint-structured apps, middleware, and test suites.

CLAUDE.md for Flask

## Flask Stack
- Version: Flask >= 3.0 | pip install flask
- Factory: create_app(config) pattern | app.config.from_object/from_envvar
- Route: @bp.route("/path", methods=["GET","POST"]) | return jsonify({}), status
- Request: request.get_json() | request.args.get("key") | request.form["field"]
- Error: abort(400) | @app.errorhandler(404) def handler(e): return jsonify(...), 404
- Test: app.test_client() | pytest with fixture returning test_client | assert r.json

Flask REST API Pipeline

# app/flask_app.py — Flask app factory, Blueprints, middleware, error handling, testing
from __future__ import annotations

import logging
import os
import time
from dataclasses import asdict, dataclass
from functools import wraps
from typing import Any, Callable

from flask import (
    Blueprint,
    Flask,
    abort,
    current_app,
    g,
    jsonify,
    request,
    send_file,
)
from flask import Response
from werkzeug.exceptions import HTTPException

log = logging.getLogger(__name__)


# ─────────────────────────────────────────────────────────────────────────────
# 1. Response helpers
# ─────────────────────────────────────────────────────────────────────────────

def ok(data: Any = None, message: str = "ok", status: int = 200) -> Response:
    """
    Standard 200 JSON response.

    Example:
        return ok({"users": users})
        return ok(message="created", status=201)
    """
    body: dict = {"status": "ok"}
    if message != "ok":
        body["message"] = message
    if data is not None:
        body["data"] = data
    return jsonify(body), status  # type: ignore[return-value]


def created(data: Any = None) -> Response:
    """201 Created response."""
    return ok(data=data, status=201)


def no_content() -> Response:
    """204 No Content response."""
    return Response(status=204)


def error(message: str, status: int = 400, **extra) -> Response:
    """
    Standard error JSON response.

    Example:
        return error("Invalid email address", 422)
        return error("Not found", 404, resource="user", id=42)
    """
    body = {"status": "error", "message": message, **extra}
    return jsonify(body), status  # type: ignore[return-value]


def paginated(items: list, total: int, page: int, per_page: int) -> Response:
    """
    Paginated list response.

    Example:
        return paginated(users, total=250, page=2, per_page=20)
    """
    return ok({
        "items":    items,
        "total":    total,
        "page":     page,
        "per_page": per_page,
        "pages":    -(-total // per_page),  # ceiling div
    })


# ─────────────────────────────────────────────────────────────────────────────
# 2. Request helpers
# ─────────────────────────────────────────────────────────────────────────────

def get_json_body(required: list[str] | None = None) -> dict:
    """
    Parse JSON body; abort 400 if not JSON or required fields missing.

    Example:
        body = get_json_body(required=["email", "password"])
        user = create_user(body["email"], body["password"])
    """
    data = request.get_json(silent=True)
    if data is None:
        abort(400, description="Request body must be valid JSON")
    if required:
        missing = [k for k in required if k not in data]
        if missing:
            abort(400, description=f"Missing required fields: {', '.join(missing)}")
    return data


def get_pagination() -> tuple[int, int]:
    """
    Parse ?page= and ?per_page= query params.
    Returns (page, per_page) with sensible defaults and max cap.

    Example:
        page, per_page = get_pagination()
        offset = (page - 1) * per_page
    """
    try:
        page     = max(1, int(request.args.get("page", 1)))
        per_page = min(100, max(1, int(request.args.get("per_page", 20))))
    except ValueError:
        abort(400, description="page and per_page must be integers")
    return page, per_page


def get_int_param(name: str, default: int | None = None, min_val: int | None = None) -> int | None:
    """
    Parse an integer query parameter, abort 400 on invalid value.

    Example:
        limit = get_int_param("limit", default=50, min_val=1)
    """
    raw = request.args.get(name)
    if raw is None:
        return default
    try:
        val = int(raw)
    except ValueError:
        abort(400, description=f"Parameter '{name}' must be an integer")
    if min_val is not None and val < min_val:
        abort(400, description=f"Parameter '{name}' must be >= {min_val}")
    return val


# ─────────────────────────────────────────────────────────────────────────────
# 3. Decorators / middleware
# ─────────────────────────────────────────────────────────────────────────────

def require_json(f: Callable) -> Callable:
    """
    Decorator: abort 415 if Content-Type is not application/json.

    Example:
        @bp.route("/users", methods=["POST"])
        @require_json
        def create_user(): ...
    """
    @wraps(f)
    def wrapper(*args, **kwargs):
        if not request.is_json:
            abort(415, description="Content-Type must be application/json")
        return f(*args, **kwargs)
    return wrapper


def require_auth(f: Callable) -> Callable:
    """
    Decorator: extract Bearer token from Authorization header into g.token.
    Abort 401 if missing.

    Example:
        @bp.route("/profile")
        @require_auth
        def profile():
            token = g.token
    """
    @wraps(f)
    def wrapper(*args, **kwargs):
        auth_header = request.headers.get("Authorization", "")
        if not auth_header.startswith("Bearer "):
            abort(401, description="Authorization header required (Bearer token)")
        g.token = auth_header[7:]
        return f(*args, **kwargs)
    return wrapper


def log_requests(app: Flask) -> None:
    """
    Register before/after request hooks to log timing.

    Example:
        log_requests(app)
    """
    @app.before_request
    def _start_timer():
        g.request_start = time.perf_counter()

    @app.after_request
    def _log_response(response: Response) -> Response:
        elapsed_ms = (time.perf_counter() - getattr(g, "request_start", time.perf_counter())) * 1000
        log.info(
            "%s %s%d (%.1fms)",
            request.method, request.path, response.status_code, elapsed_ms,
        )
        return response


# ─────────────────────────────────────────────────────────────────────────────
# 4. Error handlers
# ─────────────────────────────────────────────────────────────────────────────

def register_error_handlers(app: Flask) -> None:
    """
    Register JSON error handlers for common HTTP errors.

    Example:
        register_error_handlers(app)
    """
    @app.errorhandler(HTTPException)
    def handle_http_error(exc: HTTPException) -> Response:
        return jsonify({
            "status": "error",
            "code":   exc.code,
            "error":  exc.name,
            "message": exc.description,
        }), exc.code  # type: ignore[return-value]

    @app.errorhandler(Exception)
    def handle_unexpected(exc: Exception) -> Response:
        log.exception("Unhandled exception: %s", exc)
        return jsonify({
            "status":  "error",
            "code":    500,
            "error":   "Internal Server Error",
            "message": "An unexpected error occurred",
        }), 500  # type: ignore[return-value]


# ─────────────────────────────────────────────────────────────────────────────
# 5. Example Blueprint: Users resource
# ─────────────────────────────────────────────────────────────────────────────

users_bp = Blueprint("users", __name__, url_prefix="/users")

# In-memory store for demo
_USERS: dict[int, dict] = {
    1: {"id": 1, "name": "Alice", "email": "[email protected]", "active": True},
    2: {"id": 2, "name": "Bob",   "email": "[email protected]",   "active": True},
}
_NEXT_ID = 3


@users_bp.route("/", methods=["GET"])
def list_users():
    """GET /users — list all users with pagination."""
    page, per_page = get_pagination()
    all_users = list(_USERS.values())
    start  = (page - 1) * per_page
    subset = all_users[start : start + per_page]
    return paginated(subset, total=len(all_users), page=page, per_page=per_page)


@users_bp.route("/<int:user_id>", methods=["GET"])
def get_user(user_id: int):
    """GET /users/<id> — fetch a single user."""
    user = _USERS.get(user_id)
    if not user:
        abort(404, description=f"User {user_id} not found")
    return ok(user)


@users_bp.route("/", methods=["POST"])
@require_json
def create_user():
    """POST /users — create a user."""
    global _NEXT_ID
    body = get_json_body(required=["name", "email"])
    user = {"id": _NEXT_ID, "name": body["name"], "email": body["email"], "active": True}
    _USERS[_NEXT_ID] = user
    _NEXT_ID += 1
    return created(user)


@users_bp.route("/<int:user_id>", methods=["PATCH"])
@require_json
def update_user(user_id: int):
    """PATCH /users/<id> — partial update."""
    user = _USERS.get(user_id)
    if not user:
        abort(404, description=f"User {user_id} not found")
    body = get_json_body()
    for key in ("name", "email", "active"):
        if key in body:
            user[key] = body[key]
    return ok(user)


@users_bp.route("/<int:user_id>", methods=["DELETE"])
def delete_user(user_id: int):
    """DELETE /users/<id>."""
    if user_id not in _USERS:
        abort(404, description=f"User {user_id} not found")
    del _USERS[user_id]
    return no_content()


# ─────────────────────────────────────────────────────────────────────────────
# 6. App factory
# ─────────────────────────────────────────────────────────────────────────────

def create_app(config: dict | None = None) -> Flask:
    """
    Flask app factory.

    Example:
        app = create_app()
        app = create_app({"TESTING": True, "DEBUG": True})
    """
    app = Flask(__name__)
    app.config.update({
        "SECRET_KEY": os.getenv("SECRET_KEY", "dev-secret-change-in-prod"),
        "DEBUG":      os.getenv("FLASK_DEBUG", "false").lower() == "true",
        "JSON_SORT_KEYS": False,
    })
    if config:
        app.config.update(config)

    log_requests(app)
    register_error_handlers(app)
    app.register_blueprint(users_bp)

    @app.route("/health")
    def health():
        return jsonify({"status": "ok", "service": "flask-api"})

    return app


# ─────────────────────────────────────────────────────────────────────────────
# Demo / test run
# ─────────────────────────────────────────────────────────────────────────────

if __name__ == "__main__":
    app = create_app({"DEBUG": True})

    print("=== Flask in-process test client demo ===")
    with app.test_client() as client:
        print("\n--- GET /health ---")
        r = client.get("/health")
        print(f"  {r.status_code}: {r.json}")

        print("\n--- GET /users/ (paginated) ---")
        r = client.get("/users/?page=1&per_page=5")
        print(f"  {r.status_code}: total={r.json['data']['total']}, items={len(r.json['data']['items'])}")

        print("\n--- GET /users/1 ---")
        r = client.get("/users/1")
        print(f"  {r.status_code}: {r.json['data']}")

        print("\n--- POST /users/ ---")
        r = client.post("/users/", json={"name": "Carol", "email": "[email protected]"})
        print(f"  {r.status_code}: {r.json['data']}")
        new_id = r.json["data"]["id"]

        print("\n--- PATCH /users/<new_id> ---")
        r = client.patch(f"/users/{new_id}", json={"name": "Caroline"})
        print(f"  {r.status_code}: name={r.json['data']['name']!r}")

        print("\n--- DELETE /users/<new_id> ---")
        r = client.delete(f"/users/{new_id}")
        print(f"  {r.status_code} No Content: {r.status_code == 204}")

        print("\n--- 404 error handler ---")
        r = client.get("/users/9999")
        print(f"  {r.status_code}: {r.json['message']!r}")

        print("\n--- 400 missing fields ---")
        r = client.post("/users/", json={"name": "NoEmail"})
        print(f"  {r.status_code}: {r.json['message']!r}")

    print("\nTo run the dev server:")
    print("  flask --app app.flask_app:create_app run --debug")

For the FastAPI alternative — FastAPI uses Python type annotations and Pydantic to auto-generate OpenAPI docs, async request handlers, and automatic request/response validation; Flask is synchronous by default, requires manual validation, but has a much larger extension ecosystem (Flask-SQLAlchemy, Flask-Login, Flask-Admin) and simpler mental model — use FastAPI for new APIs where you want automatic OpenAPI docs, async endpoints, and tight Pydantic integration, Flask for projects leveraging its mature extension ecosystem or when the team prefers its simpler explicit-over-magic design. For the Django alternative — Django is a batteries-included framework with ORM, admin panel, authentication, migrations, forms, and a convention-heavy project structure; Flask is micro-framework with no ORM or admin, requiring explicit library choices for each concern but offering more architectural freedom — use Django for content-heavy or data-admin applications where the built-in ORM and admin panel save significant time, Flask for APIs or applications where you don’t need Django’s built-ins and want full control over your stack choices. The Claude Skills 360 bundle includes Flask skill sets covering ok()/created()/no_content()/error()/paginated() response helpers, get_json_body()/get_pagination()/get_int_param() request helpers, require_json/require_auth decorators, log_requests()/register_error_handlers() middleware, users_bp Blueprint with CRUD routes, and create_app() factory pattern. Start with the free tier to try Flask REST API and web application code generation.

Keep Reading

AI

Claude Code for email.contentmanager: Python Email Content Accessors

Read and write EmailMessage body content with Python's email.contentmanager module and Claude Code — email contentmanager ContentManager for the class that maps content types to get and set handler functions allowing EmailMessage to support get_content and set_content with type-specific behaviour, email contentmanager raw_data_manager for the ContentManager instance that handles raw bytes and str payloads without any conversion, email contentmanager content_manager for the standard ContentManager instance used by email.policy.default that intelligently handles text plain text html multipart and binary content types, email contentmanager get_content_text for the handler that returns the decoded text payload of a text-star message part as a str, email contentmanager get_content_binary for the handler that returns the raw decoded bytes payload of a non-text message part, email contentmanager get_data_manager for the get-handler lookup used by EmailMessage get_content to find the right reader function for the content type, email contentmanager set_content text for the handler that creates and sets a text part correctly choosing charset and transfer encoding, email contentmanager set_content bytes for the handler that creates and sets a binary part with base64 encoding and optional filename Content-Disposition, email contentmanager EmailMessage get_content for the method that reads the message body using the registered content manager handlers, email contentmanager EmailMessage set_content for the method that sets the message body and MIME headers in one call, email contentmanager EmailMessage make_alternative make_mixed make_related for the methods that convert a simple message into a multipart container, email contentmanager EmailMessage add_attachment for the method that attaches a file or bytes to a multipart message, and email contentmanager integration with email.message and email.policy and email.mime and io for building high-level email readers attachment extractors text body accessors HTML readers and policy-aware MIME construction pipelines.

5 min read Feb 12, 2029
AI

Claude Code for email.charset: Python Email Charset Encoding

Control header and body encoding for international email with Python's email.charset module and Claude Code — email charset Charset for the class that wraps a character set name with the encoding rules for header encoding and body encoding describing how to encode text for that charset in email messages, email charset Charset header_encoding for the attribute specifying whether headers using this charset should use QP quoted-printable encoding BASE64 encoding or no encoding, email charset Charset body_encoding for the attribute specifying the Content-Transfer-Encoding to use for message bodies in this charset such as QP or BASE64, email charset Charset output_codec for the attribute giving the Python codec name used to encode the string to bytes for the wire format, email charset Charset input_codec for the attribute giving the Python codec name used to decode incoming bytes to str, email charset Charset get_output_charset for returning the output charset name, email charset Charset header_encode for encoding a header string using the charset's header_encoding method, email charset Charset body_encode for encoding body content using the charset's body_encoding, email charset Charset convert for converting a string from the input_codec to the output_codec, email charset add_charset for registering a new charset with custom encoding rules in the global charset registry, email charset add_alias for adding an alias name that maps to an existing registered charset, email charset add_codec for registering a codec name mapping for use by the charset machinery, and email charset integration with email.message and email.mime and email.policy and email.encoders for building international email senders non-ASCII header encoders Content-Transfer-Encoding selectors charset-aware message constructors and MIME encoding pipelines.

5 min read Feb 11, 2029
AI

Claude Code for email.utils: Python Email Address and Header Utilities

Parse and format RFC 2822 email addresses and dates with Python's email.utils module and Claude Code — email utils parseaddr for splitting a display-name plus angle-bracket address string into a realname and email address tuple, email utils formataddr for combining a realname and address string into a properly quoted RFC 2822 address with angle brackets, email utils getaddresses for parsing a list of raw address header strings each potentially containing multiple comma-separated addresses into a list of realname address tuples, email utils parsedate for parsing an RFC 2822 date string into a nine-tuple compatible with time.mktime, email utils parsedate_tz for parsing an RFC 2822 date string into a ten-tuple that includes the UTC offset timezone in seconds, email utils parsedate_to_datetime for parsing an RFC 2822 date string into an aware datetime object with timezone, email utils formatdate for formatting a POSIX timestamp or the current time as an RFC 2822 date string with optional usegmt and localtime flags, email utils format_datetime for formatting a datetime object as an RFC 2822 date string, email utils make_msgid for generating a globally unique Message-ID string with optional idstring and domain components, email utils decode_rfc2231 for decoding an RFC 2231 encoded parameter value into a tuple of charset language and value, email utils encode_rfc2231 for encoding a string as an RFC 2231 encoded parameter value, email utils collapse_rfc2231_value for collapsing a decoded RFC 2231 tuple to a Unicode string, and email utils integration with email.message and email.headerregistry and datetime and time for building address parsers date formatters message-id generators header extractors and RFC-compliant email construction utilities.

5 min read Feb 10, 2029

Put these ideas into practice

Claude Skills 360 gives you production-ready skills for everything in this article — and 2,350+ more. Start free or go all-in.

Back to Blog

Get 360 skills free