Porting an Existing WSGI Application to Pyramid

Pyramid is cool, but already-working code is cooler. You may not have the time, money or energy to port an existing Pylons, Django, Zope, or other WSGI-based application to Pyramid wholesale. In such cases, it can be useful to incrementally port an existing application to Pyramid.

The broad-brush way to do this is:

  • Set up an exception view that will be called whenever a NotFound exception is raised by Pyramid.

  • In this exception view, delegate to your already-written WSGI application.

Here's an example:

 1from pyramid.wsgi import wsgiapp2
 2from pyramid.exceptions import NotFound
 3
 4if __name__ == '__main__':
 5    # during Pyramid configuration (usually in your Pyramid project's
 6    # __init__.py), get a hold of an instance of your existing WSGI
 7    # application.
 8    original_app = MyWSGIApplication()
 9
10    # using the pyramid.wsgi.wsgiapp2 wrapper function, wrap the
11    # application into something that can be used as a Pyramid view.
12    notfound_view = wsgiapp2(original_app)
13
14    # in your configuration, use the wsgiapp2-wrapped application as
15    # a NotFound exception view
16    config = Configurator()
17
18    # ... your other Pyramid configuration ...
19    config.add_view(notfound_view, context=NotFound)
20    # .. the remainder of your configuration ...

When Pyramid cannot resolve a URL to a view, it will raise a NotFound exception. The add_view statement in the example above configures Pyramid to use your original WSGI application as the NotFound view. This means that whenever Pyramid cannot resolve a URL, your original application will be called.

Incrementally, you can begin moving features from your existing WSGI application to Pyramid; if Pyramid can resolve a request to a view, the Pyramid "version" of the application logic will be used. If it cannot, the original WSGI application version of the logic will be used. Over time, you can move all of the logic into Pyramid without needing to do it all at once.