- class mdfy.elements.table.TableData(header: list[str], row_labels: list[str], values: Iterable[list[Any] | Tuple])[source]¶
Bases:
objectData model for markdown table content.
- 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:
MdElementConverter for dict or list to markdown table.
- Parameters:
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 |