class mdfy.elements.table.TableData(header: list[str], row_labels: list[str], values: Iterable[list[Any] | Tuple])[source]

Bases: object

Data model for markdown table content.

header

Column headers of the table

Type:

list[str]

row_labels

Row labels (used when table is transposed)

Type:

list[str]

values

2D array of table values

Type:

list[list[Any]]

header: list[str]
row_labels: list[str]
values: Iterable[list[Any] | Tuple]
classmethod from_dict_list(data: list[Dict[str, Any]], header: list[str] | None = None, row_labels: list[str] | None = None) TableData[source]

Create TableData from a list of dictionaries.

Parameters:
  • data (list[dict[str, Any]]) – List of dictionaries to convert to table data

  • header (Optional[list[str]], optional) – Custom header labels. Defaults to None.

  • row_labels (Optional[list[str]], optional) – Custom row labels. Defaults to None.

Returns:

Converted table data

Return type:

TableData

transpose() TableData[source]

Create a transposed version of the table data.

Returns:

Transposed table data

Return type:

TableData

class mdfy.elements.table.MdTable(data: Dict[str, Any] | list[Dict[str, Any]], header: list[str] | None = None, row_labels: list[str] | None = None, transpose: bool = False, precision: None | int = None)[source]

Bases: MdElement

Converter for dict or list to markdown table.

Parameters:
  • data (dict or list) – The data to convert.

  • header (list[str], optional) – Custom header labels. If not provided, dictionary keys will be used.

  • row_labels (list[str], optional) – Custom row labels. If not provided, no row labels will be shown.

  • transpose (bool, optional) – If True, transpose the table. Defaults to False.

  • precision (Optional[int]) – Number of decimal places for floats. If None, values are not formatted.

Examples

>>> data = {
...     "Name": "John Doe",
...     "Age": 30,
...     "Occupation": "Software Engineer",
... }
>>> table = MdTable(data)
>>> print(table)
| Name | Age | Occupation |
| --- | --- | --- |
| John Doe | 30 | Software Engineer |
>>> # Custom headers
>>> table = MdTable(data, header=["Full Name", "Years", "Job"])
>>> print(table)
| Full Name | Years | Job |
| --- | --- | --- |
| John Doe | 30 | Software Engineer |
>>> # With row labels
>>> data = [
...     {"Name": "John Doe", "Age": 30},
...     {"Name": "Jane Doe", "Age": 25}
... ]
>>> table = MdTable(data, row_labels=["Person 1", "Person 2"])
>>> print(table)
| | Name | Age |
| --- | --- | --- |
| Person 1 | John Doe | 30 |
| Person 2 | Jane Doe | 25 |
>>> # Transposed table
>>> print(MdTable(data, transpose=True))
| | | |
| --- | --- | --- |
| Name | John Doe | Jane Doe |
| Age | 30 | 25 |