Pyramid Views

Authors: Chris McDonough
Agendaless Consulting
Date: 4/29/2011 (Pylons Minicon, SF)

View Callables

Hi, here's a request. Could I have a response? BTW, I'm kinda particular.
Wow! I see all the conditions you put in there. Let me dig around a little bit to find you the best answer. OK, here's my best response, hope you like it!

WTView?

omg-wtf.jpg

View Callables

A Pyramid view callable is:

A View Callable as Function

Below, aview is a simple view callable implemented as a function:

from pyramid.response import Response

def aview(request):
    return Response('OK')

A View Callable as Instance

Below, aview is a simple view callable implemented as an instance:

from pyramid.response import Response

class AViewFactory(object):
    def __call__(self, request):
        return Response('OK')

aview = AViewFactory()

A View Callable as Class

Below, aview is a simple view callable implemented as a class:

from pyramid.response import Response

class aview(object):
    def __init__(self, request):
        self.request = request

    def __call__(self):
        return Response('OK')

View Class (Multiple Methods)

Any method of a view class can return a response:

class aview(object):
    def __init__(self, request):
        self.request = request
    def ok(self):
        return Response('OK')
    def notok(self):
        return Response('Not OK')

View Declared!=Registered

View Registration (Imperative)

from pyramid.config import Configurator
from pyramid.response import Response

def aview(request):
    return Response('OK')

if __name__ == '__main__':
   config = Configurator()
   config.add_route('home', '/')
   config.add_view(aview, route_name='home')

View Class (Multiple Methods)

class aview(object):
    def __init__(self, request):
        self.request = request
    def ok_view(self):return Response('OK')
    def notok_view(self):return Response('NotOK')
if __name__ == '__main__': # ...
    config.add_route('ok', '/ok')
    config.add_route('notok', '/notok')
    config.add_view(aview,route_name='ok',
                    attr='ok_view')
    config.add_view(aview,route_name='notok',
                    attr='notok_view')

View Class (Multiple Methods)

Declarative View Registration

Register View Declaratively

from pyramid.view import view_config
from pyramid.response import Response

@view_config(route_name='home')
def aview(request):
    return Response('OK')

if __name__ == '__main__':
    # ...
    config.add_route('home', '/')
    config.scan('__main__')

Register Class Declaratively

@view_config(route_name='home')
class aview(object):
    def __init__(self, request):
        self.request = request
    def __call__(self):
        return Response('OK')

if __name__ == '__main__':
    # ...
    config.add_route('home', '/')
    config.scan('__main__')

Register Method Declaratively

class aview(object):
    def __init__(self, request):
        self.request = request
    @view_config(route_name='ok')
    def ok(self): return Response('OK')
    @view_config(route_name='notok')
    def notok(self): return Response('Not OK')
if __name__ == '__main__':
    # ...
    config.add_route('ok', '/ok')
    config.add_route('notok', '/not_ok')
    config.scan('__main__')

Impact of view_config

View Configuration

View Lookup/Execution

Pyramid "router" orchestrates view lookup and execution.

asimo.jpg

Router Responsibilities

Blackbox.jpg

Router Responsibilities

The router (among other things) is responsible for these steps:

Router Timeline

Factory_1.png

Router Timeline (2)

routes.png

Router Timeline (3)

checkmark.png

Router Timeline (4)

access.jpg

Router Timeline (5)

Hi, here's a request and a context. Could I have a view callable?
Yep, here's one that matches all the conditions of the request and the context.

Router Timeline (6)

multiview2.png

Router Timeline (7)

multiview-grid.png

Router Timeline (7)

from pyramid.response import Response
def aview(request):
    return Response('OK')
def derived_view(request):
    # ... do security checking using request and
    # request.context ..
    return aview(request)

Router Timeline (8)

Denouement

Question Time