natcap.invest.reporting.table_generator

A helper module for generating html tables that are represented as Strings

natcap.invest.reporting.table_generator.add_checkbox_column(col_list, row_list, checkbox_pos=1)

Insert a new column into the list of column dictionaries so that it is the second column dictionary found in the list. Also add the checkbox column header to the list of row dictionaries and subsequent checkbox value

Parameters
  • col_list

    a list of dictionaries that defines the column structure for the table (required). The order of the columns from left to right is depicted by the index of the column dictionary in the list. Each dictionary in the list has the following keys and values:

    ’name’ - a string for the column name ‘total’ - a boolean for whether the column should be totaled

  • row_list – a list of dictionaries that represent the rows. Each dictionary’s keys should match the column names found in ‘col_list’ (required) Example: [ {col_name_1: value, col_name_2: value, …}, {col_name_1: value, col_name_2: value, …}, …]

  • - an integer for the position of the checkbox (checkbox_pos) – column. Defaulted at 1 (optional)

Returns

a tuple of the updated column and rows list of dictionaries

in that order

natcap.invest.reporting.table_generator.add_totals_row(col_headers, total_list, total_name, checkbox_total, tdata_tuples)

Construct a totals row as an html string. Creates one row element with data where the row gets a class name and the data get a class name if the corresponding column is a totalable column

col_headers - a list of the column headers in order (required)

total_list - a list of booleans that corresponds to ‘col_headers’ and

indicates whether a column should be totaled (required)

total_name - a string for the name of the total row, ex: ‘Total’, ‘Sum’

(required)

checkbox_total - a boolean value that distinguishes whether a checkbox

total row is being added or a regular total row. Checkbox total row is True. This will determine the row class name and row data class name (required)

tdata_tuples - a list of tuples where the first index in the tuple is a

boolean which indicates if a table data element has a attribute class. The second index is the String value of that class or None (required)

return - a string representing the html contents of a row which should

later be used in a ‘tfoot’ element

natcap.invest.reporting.table_generator.generate_table(table_dict, attributes=None)

Takes in a dictionary representation of a table and generates a string of the the table in the form of hmtl

Parameters
  • table_dict (dict) –

    a dictionary with the following arguments:

    ’cols’: a list of dictionaries that defines the column

    structure for the table (required). The order of the columns from left to right is depicted by the index of the column dictionary in the list. Each dictionary in the list has the following keys and values:

    ’name’ (string): the column name (required) ‘total’ (boolean): whether to total the column ‘attr’ (dict, optional): tag attributes. Ex: ‘attr’: {‘class’: ‘offsets’} ‘td_class’ (string, optional): a String to assign as a class name to the table data tags under the column. Each table data tag under the column will have a class attribute assigned to ‘td_class’ value.

    ’rows’: a list of dictionaries that represent the rows. Each

    dictionaries keys should match the column names found in ‘cols’ (possibly empty list) (required) Example:[ {col_name_1: value, col_name_2: value, …}, {col_name_1: value, col_name_2: value, …}, …]

    ’checkbox’: a boolean value for whether there should be a

    checkbox column. If True a ‘selected total’ row will be added to the bottom of the table that will show the total of the columns selected (optional)

    ’checkbox_pos’: an integer value for in which column

    position the the checkbox column should appear (optional)

    ’total’: a boolean value for whether there should be a constant

    total row at the bottom of the table that sums the column values (optional)

  • attributes (dict, optional) – a dictionary of html table attributes. The attribute name is the key which gets set to the value of the key. Example: {‘class’: ‘sorttable’, ‘id’: ‘parcel_table’}

Returns

string representing an html table

natcap.invest.reporting.table_generator.get_dictionary_values_ordered(dict_list, key_name)

Generate a list, with values from ‘key_name’ found in each dictionary in the list of dictionaries ‘dict_list’. The order of the values in the returned list match the order they are retrieved from ‘dict_list’

dict_list - a list of dictionaries where each dictionary has the same

keys. Each dictionary should have at least one key:value pair with the key being ‘key_name’ (required)

key_name - a String or Int for the key name of interest in the

dictionaries (required)

return - a list of values from ‘key_name’ in ascending order based

on ‘dict_list’ keys

natcap.invest.reporting.table_generator.get_row_data(row_list, col_headers)

Construct the rows in a 2D List from the list of dictionaries, using col_headers to properly order the row data.

Parameters
  • row_list – a list of dictionaries that represent the rows. Each dictionaries keys should match the column names found in ‘col_headers’. The rows will be ordered the same as they are found in the dictionary list (required) Example: [ {‘col_name_1’:’9/13’, ‘col_name_3’:’high’, ‘col_name_2’:’chips’}, {‘col_name_1’:’3/13’, ‘col_name_2’:’low’, ‘col_name_3’:’peanuts’}, {‘col_name_1’:’5/12’, ‘col_name_2’:’med’, ‘col_name_3’:’mints’}]

  • col_headers

    a list of the names of the column headers in order

    example : [col_name_1, col_name_2, col_name_3…]

Returns

a 2D list with each inner list representing a row