pages.context

Page actions AI-ready formats and sharing
Open LLM text
Share with AI
Ask Claude Ask ChatGPT Ask Gemini Ask Copilot

Context cascade for filesystem-based page routes.

Runs_context.pyproviders from root to deepest, merging results. Child context overrides parent — like Bengal's cascade_snapshot but for live server requests instead of static site…

Context cascade for filesystem-based page routes.

Runs_context.pyproviders from root to deepest, merging results. Child context overrides parent — like Bengal's cascade_snapshot but for live server requests instead of static site builds.

pages.context

Name Type Default Description
type
qualified_name
element_type
description
source_file
line_number
is_autodoc
autodoc_element
_autodoc_template
_autodoc_url_path
_autodoc_page_type
title
doc_content_hash

Symbols on this page

build_cascade_context
function async
async def build_cascade_context(providers: tuple[ContextProvider, ...], path_params: dict[str, str], service_providers: dict[type, Any] | None = None, *, request: Request | None = None) -> dict[str, Any]

Run context providers from root to leaf, merging results.

Each provider's output is merged into the accumulated context. Later providers (deeper in the filesystem) override earlier ones.

Providers may raiseHTTPError subclasses (e.g. NotFound) to abort the cascade early. The exception propagates to the caller (page_wrapper), which is wrapped by handle_request — chirp's standard error pipeline renders the appropriate error page automatically. This eliminates the need for downstream handlers to guard against missing resources::

# In _context.py:
from chirp import NotFound

def context(doc_id: str) -> dict:
    doc = store.get(doc_id)
    if doc is None:
        raise NotFound(f"Document {doc_id} not found")
    return {"doc": doc}

Parameters

Name Type Default Description
providers tuple[ContextProvider, ...] Context providers ordered from root (depth=0) to leaf.
path_params dict[str, str] Extracted path parameters from the URL match.
service_providers dict[type, Any] | None None Type-keyed factories from ``app.provide()``. Provider params with matching annotations are resolved from these.
request Request | None None
_call_provider
function
def _call_provider(func: Any, path_params: dict[str, str], accumulated_ctx: dict[str, Any], service_providers: dict[type, Any], *, request: Request | None = None) -> Any

Call a context provider, injecting path params, parent context, and services.

The provider function's signature determines which arguments it receives:

  • request (by name or Requestannotation) comes from the active request.

  • Path params (e.g.name from /skill/\{name}) come from the URL.

  • Other params come from the accumulated context of parent providers.

  • Params with type annotations matchingapp.provide()are resolved from service providers::

    def context() -> dict:

    ...  # receives nothing
    

    def context(request: Request) -> dict:

    ...  # current_path from request.path
    

    def context(doc_id: str, store: DocumentStore) -> dict:

    ...  # doc_id from path, store from app.provide()
    

Parameters

Name Type Default Description
func Any
path_params dict[str, str]
accumulated_ctx dict[str, Any]
service_providers dict[type, Any]
request Request | None None

View source · /home/runner/work/chirp/chirp/site/../src/chirp/pages/context.py:1