Acces to db made easy

- Prev: Filters
- Next: Localization

# Totals

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')

## personalization and colors

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.

## dates

Since date breaks are probably very common a function makes it easy to brake on dates.

## totals... with no totals!

If you only need subtotals and not totals you can prevent totals using option `hide_total` when declaring the column to sum.

## API

class `sqlkit.widgets.table.totals.``Totals`(table, treeview=None)

  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_total`(*field_names, **kw)

    Add a total object each time subtotal changes

    Parameters: | field_names – a list of field_names for which we want a total quiet – boolean. If True silently fail if the field is not a number hide_total – boolean. If True gran total will not be shown (only subtotals)

  `add_break`(field_name, func=None)

    If no `func` is provided, a break_func will be setup to insert a subtotal each time field_name changes.

    Othewise `func` will be set as break_function. Break function must have this signature:

        def break_func(obj, field_name)

    see `func_date_*` is this module for examples

  `add_date_break`(field_name, period)
    Set date break period can be: `day`, `week`, `month`, `quarter`, `year`
  `compute`()
    Go, add subtotals and total

class `sqlkit.widgets.table.totals.``TotalObj`(fields, sub=True)

  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

  `set_value_and_colors`(cell, value, field_name)
    set the color cell and possibly background