krait.mvc module

This module interfaces Krait’s MVC support with your Python backend. MVC support is implemented by having a Python script act as a route target, that creates and specifies a controller object that holds the properties used in the finished page. This page’s code is provided by a template file, that the controller requests. Templates are files with Pyml syntax embedded inside that accesses properties on the controller. The rest of the code is usually HTML, although it can be JSON, JavaScript, or anything else.

Usage

See the tutorial (TODO) for a more detailed explanation.

  1. Create a template (a view):
    Create a file with the .html or .pyml extension in a hidden directory in your site root (typically .view) with your template code.
  2. Create a controller:
    Create a subclass of CtrlBase and override its CtrlBase.get_view method to return the relative path of a template. To add properties to the controller, simply set them in its __init__ method.
  3. Reference properties on the controller in templates:
    In template files the ctrl variable refers to the active controller. Use Pyml syntax to access its members and use them on the page.
  4. Set a controller to handle a specific URL
    Create a Python script with the appropriate location and name to be reached by the URL. Import krait.mvc, then call set_init_ctrl, passing as an argument an object of your controller type. Krait will then call its overridden CtrlBase.get_view method and render the template.

Reference

class krait.mvc.CtrlBase[source]

Bases: object

The base of a MVC controller. Abstract, implementations have to override the get_view method.

get_view()[source]

Get the view that renders the response.

Returns:a relative path
Return type:str
class krait.mvc.SimpleCtrl(view, members)[source]

Bases: krait.mvc.CtrlBase

A simple controller wrapper. Best not to use, controllers should set their views themselves.

Parameters:
  • view (str) – The view that this controller renders with.
  • members (dict) – The attributes of the controller. These will be set dynamically as instance attributes.
view

str – The view that this controller renders with.

Apart from the view, other attributes exist, specified in the members dictionary.

get_view()[source]

Get the selected view.

Returns:view
Return type:str
krait.mvc.init_ctrl = None

CtrlBase – The controller to be used as a master controller. Do not use directly, use set_init_ctrl. Set from route targets that want to invoke a controller (and render a response using MVC).

krait.mvc.set_init_ctrl(ctrl)[source]

Invoke a controller after the route target has finished executing.

Parameters:ctrl (CtrlBase) – The controller object to be used.
krait.mvc.ctrl_stack = []

list of CtrlBase – The stack of controllers, used with nested controllers. Semi-deprecated. Do not use directly, use push_ctrl and pop_ctrl.

krait.mvc.curr_ctrl = None

CtrlBase – The current controller, used in controller stacking. Semi-deprecated. Do not use directly, use push_ctrl and pop_ctrl.

krait.mvc.push_ctrl(new_ctrl)[source]

Save the current controller and set a new one.

Parameters:new_ctrl (CtrlBase) – The new controller.
Returns:The new controller.
Return type:CtrlBase
krait.mvc.pop_ctrl()[source]

Discard the current controller and set the one before it.

Returns:The old, now current, controller.
Return type:CtrlBase