Package: table2md
table2mdPrint tabular data in markdown formatInstallationTested on Pythons above 3.6+.pip3 install table2mdUsagefromtable2mdimportMarkdownTable# All following examples print the following table onto stdout:# | constant | value |# |----------|-------|# | e        | 2.71  |# | pi       | 3.14  |# | sqrt2    | 1.41  |# With dictionariesMarkdownTable.from_dicts([{"constant":"e","value":2.71},{"constant":"pi","value":3.14},{"constant":"sqrt2","value":1.41},]).print()# With 2D iterablesMarkdownTable.from_2d_iterable([["constant","value"],["pi",6.28],["e",2.71],["sqrt2",1.41],]).print()# With named tuplesfromtypingimportNamedTupleclassConstant(NamedTuple):constant:strvalue:floatMarkdownTable.from_namedtuples([Constant("e",2.71),Constant("pi",3.14),Constant("sqrt2",1.41),]).print()MarkdownTable automatically coalesces all values into strings (withstr(value));
with the exception of theMarkdownTable.from_dicts, where keys are assumed to be strings.To use custom formatting, either coalesce values into strings on your own,
use your own classes with custom__str__methods,
or use the specialMarkdownTable.from_serializableconstructor.It takes an iterable of objects with_fieldsproperty and aserializemethod. Example:fromtable2mdimportMarkdownTablefromtypingimportNamedTuplefromdatetimeimportdatetime# Named tuples already provide the _fields property,# so we only need to implement the serialize method.classTemperatureReadout(NamedTuple):tstamp:datetimevalue:floatdefserialize(self)->tuple[str,str]:returnself.tstamp.strftime("%Y-%m-%d%H:%M"),f"{self.value:.1f}"MarkdownTable.from_serializable([TemperatureReadout(datetime(2021,11,1,10,0,0),10.411),TemperatureReadout(datetime(2021,11,1,12,0,0),12.782),TemperatureReadout(datetime(2021,11,1,14,0,0),11.214),]).print()# Output:# |      tstamp      | value |# |------------------|-------|# | 2021-11-01 10:00 | 10.4  |# | 2021-11-01 12:00 | 12.8  |# | 2021-11-01 14:00 | 11.2  |DocumentationMarkdownTableA class to represent tabular dataMarkdownTable(data: List[List[Str]]):Initializes a table from a 2D list of strings.
1st row is always the header row.
See utility MarkdownTable.from_* helper classmethods.
Provided lists are not copied!markdown_table.__str__() -> str:Serializes the contained tabular data to a markdown table.
First row is assumed to be the header row.markdown_table.validate() -> None:Ensures the contained table is good-to-print -
that is it has a header; and all rows with data have the same number
of cells as the header.Raises a sub-class of InvalidData in case invalid state is detected.markdown_table.print(end: str = "", file: SupportsWrite[str] = sys.stdout, flush: bool = False) -> None:Validates the table, and then prints it.
'end', 'file' and 'flush' arguments are passed
through to the builtin print function.Pleas note that the serialized table already has a newline at the end,
soend="\n"is not necessary.markdown_table.display() -> None:Validates the table, then tries to pretty-print
using IPython.display.display_markdown.If that is not available, the same as table.print()MarkdownTable.from_2d_iterable(iters: Iterable[Iterable[Any]]) -> MarkdownTable:Initializes the table from a 2D iterable.
Every cell is saved by callingstr(cell).
If provided with a 2D list, those lists are copied
(this is different to the behavior from the constructor).MarkdownTable.from_dicts(dicts: Iterable[Mapping[str, Any]]) -> MarkdownTable::Initializes the table from an iterable of dictionaries.
Every value is saved by callingstr(cell).Only keys from the first dictionary are used; that is any extra key
in other dictionaries are ignored.
However, if a following dict has a missing key, KeyError is thrown.MarkdownTable.from_namedtuples(named_tuples: Iterable[_NamedTupleLike]) -> MarkdownTable:Initializes the table from an iterable of NamedTuples.Well, in reality those don't have to be NamedTuples per se;
as longs at the objects have a_fieldsproperty
and one can iterate over those objects this function works fine.First object's_fieldsproperty is the header row.If objects aren't of the same type, ensure all of them have the same amount of fields;
otherwise an invalid table is created.MarkdownTable.from_serializable(objects: Iterable[_Serializable]) -> MarkdownTable:This is an extension of from_namedtuples; but instead of iterating over
those objects directly, obj.serialize() is used to get the string representations
of the cells.So, as long as objects have the_fieldsproperty and
aserialize()method; this method works fine.First object's_fieldsproperty is the header row.Ensure all object's serialize() method yield the same amount of cells,
otherwise an invalid table is created.ExceptionsInvalidData:
Base exception fort use in table validation. SubclassesValueError.NoData:
Exception used when a table has absolutely no data; not even a header row.
SubclassesInvalidData.MisalignedRows:
Exception used when a table has rows with more/less cells than the header.
SubclassesInvalidData.Protocols/InterfacesSupportsWrite[str]:
Anything with a write method accepting a string argument.Implemented by the _typeshed module._NamedTupleLike:
Anything with a_fieldsproperty, which is an iterable of strings
representing field names; that is itself iterable._Serializable:
Anything with a_fieldsproperty, which is an iterable of strings
representing field names; and with aserialize() -> Iterable[str]method
which returns all the held field values as strings.