pyramid.renderers

get_renderer(renderer_name, package=None, registry=None)[ソース]

Return the renderer object for the renderer renderer_name.

You may supply a relative asset spec as renderer_name. If the package argument is supplied, a relative renderer name will be converted to an absolute asset specification by combining the package package with the relative asset specification renderer_name. If package is None (the default), the package name of the caller of this function will be used as the package.

You may directly supply an application registry using the registry argument, and it will be used to look up the renderer. Otherwise, the current thread-local registry (obtained via get_current_registry()) will be used.

render(renderer_name, value, request=None, package=None)[ソース]

Using the renderer renderer_name (a template or a static renderer), render the value (or set of values) present in value. Return the result of the renderer's __call__ method (usually a string or Unicode).

If the renderer_name refers to a file on disk, such as when the renderer is a template, it's usually best to supply the name as an asset specification (e.g. packagename:path/to/template.pt).

You may supply a relative asset spec as renderer_name. If the package argument is supplied, a relative renderer path will be converted to an absolute asset specification by combining the package package with the relative asset specification renderer_name. If package is None (the default), the package name of the caller of this function will be used as the package.

The value provided will be supplied as the input to the renderer. Usually, for template renderings, this should be a dictionary. For other renderers, this will need to be whatever sort of value the renderer expects.

The 'system' values supplied to the renderer will include a basic set of top-level system names, such as request, context, renderer_name, and view. See System Values Used During Rendering for the full list. If renderer globals have been specified, these will also be used to augment the value.

Supply a request parameter in order to provide the renderer with the most correct 'system' values (request and context in particular).

render_to_response(renderer_name, value, request=None, package=None, response=None)[ソース]

Using the renderer renderer_name (a template or a static renderer), render the value (or set of values) using the result of the renderer's __call__ method (usually a string or Unicode) as the response body.

If the renderer name refers to a file on disk (such as when the renderer is a template), it's usually best to supply the name as a asset specification.

You may supply a relative asset spec as renderer_name. If the package argument is supplied, a relative renderer name will be converted to an absolute asset specification by combining the package package with the relative asset specification renderer_name. If you do not supply a package (or package is None) the package name of the caller of this function will be used as the package.

The value provided will be supplied as the input to the renderer. Usually, for template renderings, this should be a dictionary. For other renderers, this will need to be whatever sort of value the renderer expects.

The 'system' values supplied to the renderer will include a basic set of top-level system names, such as request, context, renderer_name, and view. See System Values Used During Rendering for the full list. If renderer globals have been specified, these will also be used to argument the value.

Supply a request parameter in order to provide the renderer with the most correct 'system' values (request and context in particular). Keep in mind that any changes made to request.response prior to calling this function will not be reflected in the resulting response object. A new response object will be created for each call unless one is passed as the response argument.

バージョン 1.6 で変更: In previous versions, any changes made to request.response outside of this function call would affect the returned response. This is no longer the case. If you wish to send in a pre-initialized response then you may pass one in the response argument.

class JSON(serializer=<function dumps>, adapters=(), **kw)[ソース]

Renderer that returns a JSON-encoded string.

Configure a custom JSON renderer using the add_renderer() API at application startup time:

from pyramid.config import Configurator

config = Configurator()
config.add_renderer('myjson', JSON(indent=4))

Once this renderer is registered as above, you can use myjson as the renderer= parameter to @view_config or add_view():

from pyramid.view import view_config

@view_config(renderer='myjson')
def myview(request):
    return {'greeting':'Hello world'}

Custom objects can be serialized using the renderer by either implementing the __json__ magic method, or by registering adapters with the renderer. See Serializing Custom Objects for more information.

注釈

The default serializer uses json.JSONEncoder. A different serializer can be specified via the serializer argument. Custom serializers should accept the object, a callback default, and any extra kw keyword arguments passed during renderer construction. This feature isn't widely used but it can be used to replace the stock JSON serializer with, say, simplejson. If all you want to do, however, is serialize custom objects, you should use the method explained in Serializing Custom Objects instead of replacing the serializer.

バージョン 1.4 で追加: Prior to this version, there was no public API for supplying options to the underlying serializer without defining a custom renderer.

add_adapter(type_or_iface, adapter)[ソース]

When an object of the type (or interface) type_or_iface fails to automatically encode using the serializer, the renderer will use the adapter adapter to convert it into a JSON-serializable object. The adapter must accept two arguments: the object and the currently active request.

class Foo:
    x = 5

def foo_adapter(obj, request):
    return obj.x

renderer = JSON(indent=4)
renderer.add_adapter(Foo, foo_adapter)

When you've done this, the JSON renderer will be able to serialize instances of the Foo class when they're encountered in your view results.

class JSONP(param_name='callback', **kw)[ソース]

JSONP renderer factory helper which implements a hybrid json/jsonp renderer. JSONP is useful for making cross-domain AJAX requests.

Configure a JSONP renderer using the pyramid.config.Configurator.add_renderer() API at application startup time:

from pyramid.config import Configurator

config = Configurator()
config.add_renderer('jsonp', JSONP(param_name='callback'))

The class' constructor also accepts arbitrary keyword arguments. All keyword arguments except param_name are passed to the json.dumps function as its keyword arguments.

from pyramid.config import Configurator

config = Configurator()
config.add_renderer('jsonp', JSONP(param_name='callback', indent=4))

バージョン 1.4 で変更: The ability of this class to accept a **kw in its constructor.

The arguments passed to this class' constructor mean the same thing as the arguments passed to pyramid.renderers.JSON (including serializer and adapters).

Once this renderer is registered via add_renderer() as above, you can use jsonp as the renderer= parameter to @view_config or pyramid.config.Configurator.add_view`():

from pyramid.view import view_config

@view_config(renderer='jsonp')
def myview(request):
    return {'greeting':'Hello world'}

When a view is called that uses the JSONP renderer:

  • If there is a parameter in the request's HTTP query string that matches the param_name of the registered JSONP renderer (by default, callback), the renderer will return a JSONP response.

  • If there is no callback parameter in the request's query string, the renderer will return a 'plain' JSON response.

バージョン 1.1 で追加.

参考

See also JSONP Renderer.

add_adapter(type_or_iface, adapter)

When an object of the type (or interface) type_or_iface fails to automatically encode using the serializer, the renderer will use the adapter adapter to convert it into a JSON-serializable object. The adapter must accept two arguments: the object and the currently active request.

class Foo:
    x = 5

def foo_adapter(obj, request):
    return obj.x

renderer = JSON(indent=4)
renderer.add_adapter(Foo, foo_adapter)

When you've done this, the JSON renderer will be able to serialize instances of the Foo class when they're encountered in your view results.

null_renderer

An object that can be used in advanced integration cases as input to the view configuration renderer= argument. When the null renderer is used as a view renderer argument, Pyramid avoids converting the view callable result into a Response object. This is useful if you want to reuse the view configuration and lookup machinery outside the context of its use by the Pyramid router.