Numeric columns of a Table have the option to show totals and subtotals. Subtotals are determined by brake fields/functions. This may be done interactively from the column menu or from code. The following example is very stupid but... you get the feeling!:
t = SqlTable('movie', dbproxy=db, order_by='director_id')
t.totals.add_break('director_id')
t.totals.add_total('year')
Totals are generated by a class sqlkit.widgets.table.totals.Totals that can be inherited and modified to set different total behavior.
The colors are defined in a class in the same module TotalObj whose method set_value_and_colors can be used to personalize colors and markup of the cell.
Since date breaks are probably very common a function makes it easy to brake on dates.
If you only need subtotals and not totals you can prevent totals using option hide_total when declaring the column to sum.
computed: |
|
---|
An object whose ‘compute’ method adds TotalObjets to show partial totals/grand totals to a table. A TotalObject is inserted each time total_by changes. If total_by is a callable, it’s evaluated to detect if a partial total must be inserted
Add a total object each time subtotal changes
Parameters: |
|
---|
If no func is provided, a break_func will be setup to insert a subtotal each time field_value changes.
Othewise func will be set as break_function. Break function must have this signature:
def break_func(obj, field_name, path)
see func_date_* is this module for examples
sum values and store result in totals and subtotals
Parameters: |
|
---|
To customize the behaviour of total you can just customize this method, suppose you want to make it sum flagged objects in table t:
class BoolTotals(totals.Totals):
def sum(self, obj, model, path, iter):
if obj.flag:
for field_name in self.totals.keys():
self.totals[field_name] += getattr(obj, field_name, 0) or 0
self.subtotals[field_name] += getattr(obj, field_name, 0) or 0
t. = SqlTable(...)
t.totals = BoolTotal(t)
A simple class that represents the object holding the totals and a way to represent it
personalization
You can change the look of totals inheriting from TotalObj and placing the new class as class attribute of Totals