The Pyramid Web Application Development Framework¶
Pyramid is a small, fast, down-to-earth Python web application development framework. It is developed as part of the Pylons Project. It is licensed under a BSD-like license.
Here is one of the simplest Pyramid applications you can make:
from paste.httpserver import serve
from pyramid.config import Configurator
from pyramid.response import Response
def hello_world(request):
return Response('Hello %(name)s!' % request.matchdict)
if __name__ == '__main__':
config = Configurator()
config.add_route('hello', '/hello/{name}')
config.add_view(hello_world, route_name='hello')
app = config.make_wsgi_app()
serve(app, host='0.0.0.0')
When saved to helloworld.py
, the above application can be run via:
$ easy_install pyramid
$ python helloworld.py
serving on 0.0.0.0:8080 view at http://127.0.0.1:8080
And when you visit http://localhost:8080/hello/world
in a browser, you
will see the text Hello, world!
.
See Creating Your First Pyramid Application for a full explanation of how this application works. Read the Narrative documentation to understand how Pyramid is designed to scale from simple applications like this to very large web applications.
“What’s New” Documents¶
Narrative documentation¶
Narrative documentation in chapter form explaining how to use Pyramid.
- Pyramid Introduction
- Installing Pyramid
- Creating Your First Pyramid Application
- Application Configuration
- Creating a Pyramid Project
- Scaffolds Included with Pyramid
- Creating the Project
- Installing your Newly Created Project for Development
- Running The Tests For Your Application
- Running The Project Application
- Viewing the Application
- The Project Structure
- The
MyProject
Project
- The
myproject
Package
- Modifying Package Structure
- Using the Interactive Shell
- Using an Alternate WSGI Server
- Startup
- URL Dispatch
- High-Level Operational Overview
- Route Configuration
- Route Matching
- Routing Examples
- Matching the Root URL
- Generating Route URLs
- Static Routes
- Redirecting to Slash-Appended Routes
- Debugging Route Matching
- Using a Route Prefix to Compose Applications
- Custom Route Predicates
- Route Factories
- Using Pyramid Security With URL Dispatch
- Route View Callable Registration and Lookup Details
- References
- Views
- View Callables
- Defining a View Callable as a Function
- Defining a View Callable as a Class
- View Callable Responses
- Using Special Exceptions In View Callables
- Custom Exception Views
- Using a View Callable to Do an HTTP Redirect
- Handling Form Submissions in View Callables (Unicode and Character Set Issues)
- Alternate View Callable Argument/Calling Conventions
- Pylons-1.0-Style “Controller” Dispatch
- Renderers
- Templates
- Using Templates Directly
- System Values Used During Rendering
- Templates Used as Renderers via Configuration
Chameleon
ZPT Templates- Templating with
Chameleon
Text Templates - Side Effects of Rendering a Chameleon Template
- Nicer Exceptions in Chameleon Templates
Chameleon
Template Internationalization- Templating With Mako Templates
- Automatically Reloading Templates
- Available Add-On Template System Bindings
- View Configuration
- Static Assets
- Request and Response Objects
- Sessions
- Using Events
- Environment Variables and
.ini
File Settings- Reloading Templates
- Reloading Assets
- Debugging Authorization
- Debugging Not Found Errors
- Debugging Route Matching
- Preventing HTTP Caching
- Debugging All
- Reloading All
- Default Locale Name
- Including Packages
- Explicit Tween Configuration
- Mako Template Render Settings
- Examples
- Understanding the Distinction Between
reload_templates
andreload_assets
- Adding A Custom Setting
- Logging
- Paste
- Command-Line Pyramid
- Internationalization and Localization
- Creating a Translation String
- Working With
gettext
Translation Files - Using a Localizer
- Obtaining the Locale Name for a Request
- Performing Date Formatting and Currency Formatting
- Chameleon Template Support for Translation Strings
- Mako Pyramid I18N Support
- Localization-Related Deployment Settings
- “Detecting” Available Languages
- Activating Translation
- Locale Negotiators
- Virtual Hosting
- Unit, Integration, and Functional Testing
- Resources
- Defining a Resource Tree
- Location-Aware Resources
- Generating The URL Of A Resource
- Generating the Path To a Resource
- Finding a Resource by Path
- Obtaining the Lineage of a Resource
- Determining if a Resource is In The Lineage of Another Resource
- Finding the Root Resource
- Resources Which Implement Interfaces
- Finding a Resource With a Class or Interface in Lineage
- Pyramid API Functions That Act Against Resources
- Much Ado About Traversal
- Traversal
- Security
- Enabling an Authorization Policy
- Protecting Views with Permissions
- Assigning ACLs to your Resource Objects
- Elements of an ACL
- Special Principal Names
- Special Permissions
- Special ACEs
- ACL Inheritance and Location-Awareness
- Changing the Forbidden View
- Debugging View Authorization Failures
- Debugging Imperative Authorization Failures
- Creating Your Own Authentication Policy
- Creating Your Own Authorization Policy
- Combining Traversal and URL Dispatch
- Using Hooks
- Changing the Not Found View
- Changing the Forbidden View
- Changing the Request Factory
- Using The Before Render Event
- Adding Renderer Globals (Deprecated)
- Using Response Callbacks
- Using Finished Callbacks
- Changing the Traverser
- Changing How
pyramid.request.Request.resource_url()
Generates a URL - Changing How Pyramid Treats View Responses
- Using a View Mapper
- Registering Configuration Decorators
- Registering “Tweens”
- Advanced Configuration
- Extending An Existing Pyramid Application
- Request Processing
- Thread Locals
- Using the Zope Component Architecture in Pyramid
Tutorials¶
Detailed tutorials explaining how to use Pyramid to build various types of applications and how to deploy Pyramid applications to various platforms.
Reference Material¶
Reference material includes documentation for every Pyramid API.
- API Documentation
pyramid.authorization
pyramid.authentication
pyramid.chameleon_text
pyramid.chameleon_zpt
pyramid.config
pyramid.events
pyramid.exceptions
pyramid.httpexceptions
pyramid.i18n
pyramid.interfaces
pyramid.location
pyramid.paster
pyramid.registry
pyramid.renderers
pyramid.request
pyramid.response
pyramid.scripting
pyramid.security
pyramid.session
pyramid.settings
pyramid.static
pyramid.testing
pyramid.threadlocal
pyramid.traversal
pyramid.tweens
pyramid.url
pyramid.view
pyramid.wsgi
Detailed Change History¶
Design Documentation¶
Sample Applications¶
cluegun is a simple pastebin application based on Rocky Burt’s ClueBin. It demonstrates form processing, security, and the use of ZODB within a Pyramid application. Check this application out via:
git clone git://github.com/Pylons/cluegun.git
virginia is a very simple dynamic file rendering application. It is willing to render structured text documents, HTML documents, and images from a filesystem directory. An earlier version of this application runs the repoze.org website. Check this application out via:
git clone git://github.com/Pylons/virginia.git
shootout is an example “idea
competition” application by Carlos de la Guardia and Lukasz Fidosz. It
demonstrates URL dispatch, simple authentication, integration
with SQLAlchemy and pyramid_simpleform
.
Check this application out of version control via:
git clone git://github.com/Pylons/shootout.git
Older Sample Applications (repoze.bfg)¶
Note
These applications are for an older version of Pyramid, which was
named repoze.bfg
. They won’t work unmodified under Pyramid, but
might provide useful clues.
bfgsite is the software which runs the bfg.repoze.org website. It demonstrates integration with Trac, and includes several mini-applications such as a pastebin and tutorial engine. Check a buildout for this application out of Subversion via:
svn co http://svn.repoze.org/buildouts/bfgsite/ bfgsite_buildout
KARL is a moderately-sized application
(roughly 70K lines of Python code) built on top of repoze.bfg
and other Repoze software. It is an open source web system for
collaboration, organizational intranets, and knowledge management, It
provides facilities for wikis, calendars, manuals, searching, tagging,
commenting, and file uploads. See the KARL site for download and installation details.
Support and Development¶
The Pylons Project web site is the main online source of Pyramid support and development information.
To report bugs, use the issue tracker.
If you’ve got questions that aren’t answered by this documentation, contact the Pylons-devel maillist or join the #pyramid IRC channel.
Browse and check out tagged and trunk versions of Pyramid via
the Pyramid GitHub repository.
To check out the trunk via git
, use this command:
git clone git@github.com:Pylons/pyramid.git
To find out how to become a contributor to Pyramid, please see the contributor’s section of the documentation.