Renderers

Quick use

Similar to json.dumps(), the extended encoding capabilities can be used like this:

import flask_arrest.json as extjson

extjson.dumps('foo')

Rendering and content negotiation

To transform data into a representation, a Renderer is required. Flask-arrest distinguishes between two renderers: Content renderers and exception renderers.

Content-renderers are usually invoked by serialize_response() and turn arbitrary data into a content-type they can handle. If not content-renderer is supplied in the function call, the content_renderer attribute will be used.

If an exception occurs, it is rendered by a different renderer: exception_renderer. While the interface is the same, it is usually not directly invoked by view code; instead any instance of HTTPException inside a RestBlueprint is passed to it automatically instead.

Rendering API reference

class flask_arrest.renderers.Renderer

Basic Renderer interface.

A Renderer can be asked to render an an object into a response.

render_response(data, content_type, status=200)

Render data.

Parameters:
  • data – The data to be rendered.
  • content_type – A string describing the desired output content-type.
  • status – The status code for the response.
Returns:

A Response instance.

class flask_arrest.renderers.PluggableRenderer(*args, **kwargs)

Support rendering content by registering rendering functions for each content type.

Any renderer will be called with arguments matching data, content_type, where data is the object to be rendered and content_type the desired content-type as a string. The return value is passed as arguments to make_response().

register_renderer(content_type, func)

Set renderer for content_type to func.

renders(content_type)

A function decorator. Decorating a function with this is equivalent to calling register_renderer(content_type, this_function).

flask_arrest.renderers.content_renderer

The default content rendererer, includes preset renderers for application/json and text/plain. JSON data is handled by a simple json.dumps, while text-rendering is performed by pprint.pformat(). See the source code for details.

flask_arrest.renderers.exception_renderer

The default exception renderer, renders exception as application/json, application/problem+json (the Problem Details for HTTP APIs-format), text/plain and text/html.