Skip to content

Table Builder

HTMLTable

Helper utility to create HTML (and plain text) tables.

This class is iterable

Iterating over this class will return an iterable, where the first element is the list of headers, and then the rest are the individual rows. Changing values in this iteration will NOT update the original entries.

Example:

table = HTMLTable(caption="My Table")
table.add_columns("number", "value")
table.add_rows("0", "hello world")
table.add_rows("1", "foo bar")

await bot.room_send(
    "!example:example.example",
    {
        "msgtype": "m.notice",
        "body": table.render("text"),
        "format": "org.matrix.custom.html",
        "formatted_body": table.render("html"),
    }
)

add_column

add_column(column_name: str) -> Self

Add a column (header name) to the table.

add_columns

add_columns(*column_names: str) -> Self

Add multiple columns (header names) to the table.

add_row

add_row(*values: str) -> Self

Add a row to the table.

If the number of values is not equal to the number of columns, blank values will be added to pad the row.

add_rows

add_rows(*rows: List[str]) -> Self

Add multiple rows to the table.

render_html

render_html() -> str

Render the table as an HTML string.

render_text

render_text() -> str

Render the table as a simple textual table

Example:

number | value
-------+------
0      | a
1      | b
2      | c
3      | d
4      | e

render

render(
    output_format: Literal["html", "text"] = "html",
) -> str

Render the table in the specified format.

Parameters:

Name Type Description Default
output_format Literal['html', 'text']

The output format. Either "html" or "text".

'html'

remove_row

remove_row(index: int) -> Self

Remove a row from the table.

insert_row

insert_row(index: int, *values: str) -> Self

Insert a row at a specific index.

Note that the index will become the values. For example, inserting at index 0 will insert the row at the top, and inserting at index 2 will make this row 3.