Pybald Core

templates - template/view handling

class pybald.core.templates.TemplateEngine(template_path=None, cache_path=None, helpers=None)

The basic template engine, looks up templates and renders them. Uses the mako template system

__call__(template=None, data={}, format='html')

Renders the template.

  • template – The name of the template in the filesystem to retrieve for rendering.
  • data – A dictionary that represents the context to render inside the template. Keys in this dictionary will be available to the template.
  • format – A string specifying the format type to return (e.g. ‘html’, ‘json’, ‘xml’)

Calls _get_template to retrieve the template and then renders it.

_get_template(template, format='html')

Retrieves the proper template from the Mako template system.

  • template – The name of the template in the filesystem to retrieve for rendering.
  • format – A string specifying the format for the template (html, json, xml, etc…), overrides the format specified in the data dictionary.

The _get_template method of the template engine constructs a template name based on the template_id and the format and retrieves it from the Mako template system.

form_render(template_name=None, format='form', **kargs)

Render the form for a specific model using formalchemy.

  • template_name – The name of the template to search for and render for a form
  • kargs – the data to render in the context of the form template

action() - decorator to turn methods into actions


Decorates methods that are WSGI apps to turn them into pybald-style actions.

Parameters:method – A method to turn into a pybald-style action.

This decorator is usually used to take the method of a controller instance and add some syntactic sugar around it to allow the method to use WebOb Request and Response objects. It will work with any method that implements the WSGI spec.

It allows actions to work with WebOb request / response objects and handles default behaviors, such as displaying the view when nothing is returned, or setting up a plain text Response if a string is returned. It also assigns instance variables from the pybald.extension environ variables that can be set from other parts of the WSGI pipeline.

This decorator is optional but recommended for making working with requests and responses easier.

Controller - simple base class that pybald controllers can optionally subclass

class pybald.core.controllers.Controller(*pargs, **kargs)

Base Controller that provides a registry mount

The registry keeps track of all countrollers defined in the project. The Controller class also has some minor convenience methods attached.


Raise the 404 http_client_error exception.

_redirect_to(*pargs, **kargs)

Redirect the controller


Raise an http_client_error exception using a specific code

router - pybald core, url dispatch, method munging, WSGI app

class pybald.core.router.Router(application=None, routes=None, controllers=None)

Create a Router object, the core of the pybald framework.

  • application – WSGI application/middleware that is to be wrapped by the router in the web app pipeline.
  • routes – A routing function that takes a mapper (for parsing and matching urls).
  • controllers – A registry of all loaded controllers. This is required to do routing lookups. This is also a security precaution since only registered controllers can be matched against.
__call__(environ, start_response)

A Router instance is a WSGI app. It accepts the standard WSGI call signature of environ, start_response.

The Router has a few jobs. First it uses the Routes package to compare the requested path to available url patterns that have been loaded and passed to the Router upon init.

Router is the most framework-like component of Pybald. In addition to dispatching urls to controllers, it also allows ‘method override’ behavior allowing other HTTP methods to be invoked such as put and delete from web clients that don’t support them natively.

  • environ – WSGI CGI-like request environment
  • start_response – WSGI callback for starting the response and setting HTTP response headers

Method that returns the callable code mapped to this current request.

This method can be overriden to change the behavior of mapping.


Walks the controller registry and builds the lookup table for controller classes to match against.

Does some text munging to change the camel-case class names into underscore-separated url like names. (HomeController to home)

Parameters:controllers – A controller registry, a list of all controllers that will be used with this application.

All controller candidates are loaded into a hash to look up the matched “controller” urlvar against.

The _controller suffix is removed from the module name for the url route mapping table (so controller=”home” matches home_controller).

This method is called only once at the start of a pybald application.