# pylons/pyramid architecture

> Architecture diagram of the GitHub repository pylons/pyramid, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/pylons/pyramid
- Repository: https://github.com/pylons/pyramid

## Overview

##### Project type and purpose
- Type: Open‑source Python web framework (library)
- Purpose: Provide a minimal, predictable, and customizable WSGI request‑handling pipeline, routing/traversal, view lookup, rendering, security, sessions, and static asset serving.

##### File tree at a high level
- src/pyramid: the entire framework implementation in modules such as router, request, response, view, config, events, security, session, static, tweens, scripts, etc.
- tests: comprehensive unit/integration test suites exercising each subcomponent.
- scripts (e.g., pserve, pshell): CLI entry points to bootstrap a Pyramid app.
- configuration files (pyproject.toml, setup.py, setup.cfg, .flake8, CI workflows) define packaging, linting, and testing.

##### README for usage pattern
- A minimal WSGI “hello world” example that shows how Configurator builds a WSGI app, adds routes and views, then hands it off to a WSGI server.
- Identifies Pyramid’s entry point: Configurator → make_wsgi_app() → WSGI pipeline.

##### Main components
a. External actors/services
– HTTP client (browser, curl)
– WSGI server (e.g., Waitress, Gunicorn)
– Optional backend services: database, cache, third‑party APIs (for tutorials/quick tours)
b. Pyramid core pipeline
1. WSGI server passes environ → Pyramid WSGI app
2. Pyramid’s WSGI app invokes a chain of “tweens” (middleware) registered via config tweens
3. Request object creation (src/pyramid/request.py), attached to a registry (src/pyramid/registry.py)
4. URL dispatch or traversal router (src/pyramid/router.py) selects a “context” and a view callable
5. Security checks (authentication, authorization) using policies configured in src/pyramid/security.py
6. View callable (src/pyramid/view.py) executes application code and returns a view result (Response or dict)
7. Renderer subsystem (src/pyramid/renderers.py) transforms view return value into a Response body (templates, JSON, etc.)
8. Response object creation (src/pyramid/response.py)
9. Post‑processing tweens, events (src/pyramid/events.py), and finally the WSGI response is sent back to the WSGI server
c. Static assets
– StaticURLInfo, static view config (src/pyramid/static.py)
d. Lifecycle & extensibility
– Configurator and config actions (src/pyramid/config/*) for scanning packages, registering routes, views, renderers, subscribers, etc.
– ZCA adapters (src/pyramid/config/adapters.py)
– Event subscribers/hooks (src/pyramid/config/events.py)
– Scripting tools (src/pyramid/scripts/*.py) for pshell, pserve, proutes, etc.

##### Architectural patterns & design principles
- Inversion of Control via Configurator and a central “registry”
- WSGI middleware pipeline (tweens) for cross-cutting concerns
- Pluggable URL dispatch vs. traversal dispatch
- View derivers and renderers for abstracting output formats
- Use of Zope Component Architecture adapters for extensibility

## Diagram (Mermaid)

```mermaid
graph TB
    %% External Actors
    subgraph "External Services"
        HC["HTTP Client"]:::external
        WS["WSGI Server (e.g., Waitress/Gunicorn)"]:::external
    end

    HC -->|Sends request| WS

    %% Pyramid Entry Point
    subgraph "Pyramid WSGI App" 
        WA["make_wsgi_app()"]:::core
    end

    click WA "https://github.com/pylons/pyramid/blob/main/src/pyramid/wsgi.py"

    WS -->|Invoke app| WA

    %% Core Pipeline
    subgraph "Core Pipeline"
        TW["Tweens (middleware pipeline)"]:::core
        RF["Request Factory"]:::core
        RO["Registry / Configurator"]:::core
        UD["URL Dispatch Router"]:::core
        TR["Traversal Router"]:::core
        SC["Security Policy"]:::core
        VL["View Lookup & Execution"]:::core
        RN["Renderer Subsystem"]:::core
        RP["Response Object"]:::core
        EV["Events & Subscribers"]:::core
        ST["Static Asset Serving"]:::core
        SS["Sessions"]:::core
    end

    WA --> TW
    TW --> RF
    RF --> RO
    RO --> UD
    RO --> TR
    UD --> SC
    TR --> SC
    SC --> VL
    VL --> RN
    RN --> RP
    RP --> EV
    EV -->|Final Response| WA
    ST -.-> RO
    SS -.-> RO

    %% Extension Points
    subgraph "Extension & Configuration" 
        CFG["Configurator / Registry"]:::extension
        TZ["Tweens Config"]:::extension
        SV["Views Config"]:::extension
        RE["Renderers Config"]:::extension
        SE["Security Config"]:::extension
        AE["Assets Config"]:::extension
        AD["ZCA Adapters"]:::extension
    end

    CFG --> TZ
    CFG --> SV
    CFG --> RE
    CFG --> SE
    CFG --> AE
    CFG --> AD

    CFG --> RO
    TZ --> TW
    SV --> VL
    RE --> RN
    SE --> SC
    AE --> ST
    AD --> RO

    click CFG "https://github.com/pylons/pyramid/blob/main/src/pyramid/config/__init__.py"
    click RO "https://github.com/pylons/pyramid/blob/main/src/pyramid/registry.py"
    click TZ "https://github.com/pylons/pyramid/blob/main/src/pyramid/config/tweens.py"
    click SV "https://github.com/pylons/pyramid/blob/main/src/pyramid/config/views.py"
    click RE "https://github.com/pylons/pyramid/blob/main/src/pyramid/config/rendering.py"
    click SE "https://github.com/pylons/pyramid/blob/main/src/pyramid/config/security.py"
    click AE "https://github.com/pylons/pyramid/blob/main/src/pyramid/config/assets.py"
    click AD "https://github.com/pylons/pyramid/blob/main/src/pyramid/config/adapters.py"

    %% Specific Components
    click RF "https://github.com/pylons/pyramid/blob/main/src/pyramid/request.py"
    click UD "https://github.com/pylons/pyramid/blob/main/src/pyramid/router.py"
    click TR "https://github.com/pylons/pyramid/blob/main/src/pyramid/traversal.py"
    click SC "https://github.com/pylons/pyramid/blob/main/src/pyramid/security.py"
    click VL "https://github.com/pylons/pyramid/blob/main/src/pyramid/view.py"
    click RN "https://github.com/pylons/pyramid/blob/main/src/pyramid/renderers.py"
    click RP "https://github.com/pylons/pyramid/blob/main/src/pyramid/response.py"
    click EV "https://github.com/pylons/pyramid/blob/main/src/pyramid/events.py"
    click ST "https://github.com/pylons/pyramid/blob/main/src/pyramid/static.py"
    click SS "https://github.com/pylons/pyramid/blob/main/src/pyramid/session.py"
    click TW "https://github.com/pylons/pyramid/blob/main/src/pyramid/tweens.py"

    %% CLI Tools
    subgraph "CLI Scripts" 
        PSERVE["pserve"]:::cli
        PSHELL["pshell"]:::cli
        PROUTES["proutes"]:::cli
        PREQUEST["prequest"]:::cli
        PTWEENS["ptweens"]:::cli
        PVIEWS["pviews"]:::cli
        PDIST["pdistreport"]:::cli
    end

    PSERVE --> WA
    PSHELL --> WA
    PROUTES --> WA
    PREQUEST --> WA
    PTWEENS --> WA
    PVIEWS --> WA
    PDIST --> WA

    click PSERVE "https://github.com/pylons/pyramid/blob/main/src/pyramid/scripts/pserve.py"
    click PSHELL "https://github.com/pylons/pyramid/blob/main/src/pyramid/scripts/pshell.py"
    click PROUTES "https://github.com/pylons/pyramid/blob/main/src/pyramid/scripts/proutes.py"
    click PREQUEST "https://github.com/pylons/pyramid/blob/main/src/pyramid/scripts/prequest.py"
    click PTWEENS "https://github.com/pylons/pyramid/blob/main/src/pyramid/scripts/ptweens.py"
    click PVIEWS "https://github.com/pylons/pyramid/blob/main/src/pyramid/scripts/pviews.py"
    click PDIST "https://github.com/pylons/pyramid/blob/main/src/pyramid/scripts/pdistreport.py"

    %% Styles
    classDef external fill:#dddddd,stroke:#888,stroke-width:1px
    classDef core fill:#bbdefb,stroke:#0b5394,stroke-width:1px
    classDef extension fill:#c8e6c9,stroke:#38761d,stroke-width:1px
    classDef cli fill:#ffecb3,stroke:#b37400,stroke-width:1px
```
