natcap.invest.reporting.html

Utilities for creating simple HTML report files.

class natcap.invest.reporting.html.Element(tag, content='', end_tag=True, **attrs)

Bases: object

Represents a generic HTML element.

Any Element object can be passed to HTMLDocument.add()

Example

doc = html.HTMLDocument(…) details_elem = doc.add(html.Element(‘details’)) details_elem.add(html.Element(‘img’, src=’my_pic.png’, end_tag=False))

add(elem)

Add a child element (which is returned for convenience).

html()

Returns an HTML string for this element (and its children).

class natcap.invest.reporting.html.HTMLDocument(uri, title, header)

Bases: object

Utility class for creating simple HTML files.

Example usage:

# Create the document object. doc = html.HTMLDocument(‘myfile.html’, ‘My Page’, ‘A Page About Me’)

# Add some text. doc.write_header(‘My Early Life’) doc.write_paragraph(‘I lived in a small barn.’)

# Add a table. table = doc.add(html.Table()) table.add_row([‘Age’, ‘Weight’], is_header=True) table.add_row([‘1 year’, ‘20 pounds’]) table.add_row([‘2 years’, ‘40 pounds’])

# Add an arbitrary HTML element. # Note that the HTML ‘img’ element doesn’t have an end tag. doc.add(html.Element(‘img’, src=’images/my_pic.png’, end_tag=False))

# Create the file. doc.flush()

add(elem)

Add an arbitrary element to the body of the document.

elem - any object that has a method html() to output HTML markup

Return the added element for convenience.

flush()

Create a file with the contents of this document.

insert_table_of_contents(max_header_level=2)

Insert an auto-generated table of contents.

The table of contents is based on the headers in the document.

write_header(text, level=2)

Convenience method to write a header.

write_paragraph(text)

Convenience method to write a paragraph.

class natcap.invest.reporting.html.Table(**attr)

Bases: object

Represents and renders HTML tables.

add_row(cells, is_header=False, cell_attr=None, do_formatting=True)

Writes a table row with the given cell data.

cell_attr - attributes for each cell. If provided, it must be the

same length as cells. Each entry should be a dictionary mapping attribute key to value.

add_two_level_header(outer_headers, inner_headers, row_id_header)

Adds a two level header to the table.

In this header, each outer header appears on the top row, and each inner header appears once beneath each outer header.

For example, the following code:

table.add_two_level_header(

outer_headers=[‘Weight’, ‘Value’], inner_headers=[‘Mean, Standard deviation’], row_id_header=’Farm ID’)

produces the following header:

‘ Weight Value ‘ ‘Farm ID Mean Standard Deviation Mean Standard deviation’

html()

Return the HTML string for the table.

natcap.invest.reporting.html.cell_format(data)

Formats the data to put in a table cell.