dbReport Package

This module will will generate HTML reports for the views in a sqlite database.

class dbreport.dbreport.Report(layout_path=None, **kwargs)[source]

Bases: object

The Report will handle querying the database and generating the reports

Parameters
  • layout_path (str | None) – path to the layout file, or None when using kwargs

  • kwargs – Any keyword argument defined in the layout configuration

Raises

ValueError – When both layout_path and kwargs defined

property categories

Categories for reports

The categories is a dictionary defining the menu and the items in the menus on each report. This is useful for categorizing the reports together.

Each key is the menu name, how it should appear in the report. The values for the key is a list of view names, (note it should be the view name, not the alias or title) to be under that heading. The name that will appear in the rendered report is the title for that report, if one is given.

A view name can be under multiple menus.

If a view name does not appear under any menus, it will be automatically included in a Misc menu item. Unless it is listed in ignore.

Raises
  • ValueError – when an item is not in views

  • TypeError – when setting value that is not a dict

  • TypeError – when key is not of type str

  • Typeerror – when value is not of type list

property ignore

List of views in database that should not be included in reports

When setting the ignore property, it must be an iterable (list or tuple) of view names.

When a view is listed here, it will not be included in any menu, including the Misc category.

Defaults to an empty list

Raises

ValueError – When any item listed is not in views

parse(data)[source]

The parse function may be called to intercept data before rendering

Parameters

data (dict) – data as queried from database. The keys are view names from database, and values are a list of queried results.

Returns

data as it should be rendered by report.

The keys are view names as used in layout file, and values are a list of results to be included in reports.

Return type

dict

Raises

NotImplementedError – When the default parse function is used. This must be overloaded by a custom parse function before use.

This function is useful to filter, format, add hyperlinks, or otherwise manipulate raw data queried from database before it gets rendered in report.

To be sure the parse function is called, create a class that inherits from the base Report class and overload the parse function in the custom class. Then render the reports with render(parse=True). If you try to parse data without overloading the default parse function, it will raise a NotImplementedError.

Notes

The data format for both data parameter and the returned value are defined below.

  • keys: view names where the data was queried from; and the

    filename of the report if using the write method.

  • values: list of tuples, each tuple is a row of data
    • (list): list of rows, with each row defined as a tuple

    • (tuple): each element in values is a tuple.

      The elements of the tuple are the values from the database, and/or the values that will be shown in the report.

Note

The elements of each row must be a single item (str, bool, int, etc) or a tuple in the form (value, href), where href is where the generated hyperlink for that value is directed to.

property paths
render(views=None, parse=False)[source]

Renders html for each view in views

Parameters
  • views (list | None) – list of view names to render, defaults to None, all views

  • parse (bool) – whether the parse function is called on query results. Defaults to False (don’t parse)

Returns

Rendered html of reports

Return type

dict

Changed in version 0.3.3a1: returns results; parse default was True

property views

List of views to be rendered

This will include all the views defined in the database, without the views specified by the ignore_views key.

Returns

obj:list: list of views to be rendered.

write(report_dir=None, **kwargs)[source]

Write rendered reports to files

Parameters
  • report_dir (str | None) –

    path where reports are written to defaults to None,

    which will use the path in the layout.

  • kwargs (dict) – all other keyword arguments are passed directly to the render function.

Returns

No return value

Return type

None

Raises

NotADirectoryError – When report path does not exist

New in version 0.3.3a1.