Catalog / Pyramid Web Framework Cheat Sheet
Pyramid Web Framework Cheat Sheet
A quick reference guide to the Pyramid web framework for Python, covering configuration, views, routing, and more.
Core Concepts & Configuration
Basic Setup
Installation:
|
Project Structure (minimal):
|
Configuration:
|
Configuration Options
|
Include a configuration snippet from another module. |
|
Scan a package for configuration directives (e.g., views). |
|
Adds a property to the request object. |
|
Serve static files from a directory. |
Routes & Views
Routes
Routes map URLs to view callables. Example:
|
Route parameters are available in the Example:
|
You can use
|
Views
Views are callables that handle requests. Example:
|
Views can return a
|
View lookups are based on:
|
View Predicates
|
Matches a specific HTTP method (e.g., |
|
Matches the |
|
Matches if the request is an AJAX request ( |
|
Use custom predicate function |
Templates & Static Assets
Template Rendering
Pyramid supports various templating languages, including:
Configure a renderer in the
|
Chameleon:
|
Jinja2:
|
Static Assets
Serving static assets (CSS, JavaScript, images) using
Accessing static assets in templates:
|
Make sure to create the static directory. |
Request & Response Objects
Request Object
The
|
Example:
|
Response Object
The
Example:
|
You can also set cookies on the response object:
|
Sessions
Pyramid provides session support. To enable it, configure a session factory (e.g.,
Accessing the session:
|