diff --git a/README.md b/README.md index 908264b..92e3399 100644 --- a/README.md +++ b/README.md @@ -17,29 +17,41 @@ importing: ```markdownTable(data).getMarkdown()``` or ```markdownTable(data).setParams(...).getMarkdown()``` - +where data is a `list` of `dict` where each dict represents a row. Keys must be uniform for all `dict` elements and are used to span the header. Values populate the cells for each row. ## Parameters -The library supports has the following parameters: -``` -markdownTable(data): - data (list): List of dicts with uniform {key : value} pairs used to generate the header -markdownTable(data).setParams(...): - row_sep (str): Row separation strategy with the following options: - 'always' Separate each row - 'topbottom' Insert row separator above header and below the last row - 'markdown' Single header/body separator formated as valid markdown - 'None' No row separation - padding_width (int): Extra padding to all table cells - padding_weight (str): Padding strategy. The following values are accepted: - 'left' Aligns items to the end of the cell - 'right' Aligns items to the beginning of the cell - 'centerleft' Centers items, where extra padding is allocated to the beginning of the cell - 'centerright' Centers items, where extra padding is allocated to the end of the cell - padding_char (str): Custom single character to fill extra and normal padding with. Default is a blank space. - newline_char (str): Custom character to be used for indicating a newline. Default is '\n' - float_rounding (int): Round down float values to a number decimal places. - Default is 2, but can also be set to 'None' to not round down. - quote(bool): If true (default) surrounds the table with markdown block code quote +Chaining `.setParams(...)` to a `markdownTable(data)` object allows you to pass the following keyword arguments: + +``` +| param | type | values | description | +|-----------------|-----------------|---------------|------------------------------------------------------------| +| row_sep | str | | Row separation strategy using `----` as pattern | +| | | always | Separate each row | +| | | topbottom |Separate the top (header) and bottom (last row) of the table| +| | | markdown | Separate only header from body | +| | | None | No row separators will be inserted | +| padding_width | int | | Allocate padding to all table cells | +| padding_weight | str | | Strategy for allocating padding within table cells | +| | | left | Aligns cell's contents to the end of the cell | +| | | right | Aligns cell's contents to the beginning of the cell | +| | | centerleft | Centers cell's contents with extra padding | +| | | | allocated to the beginning of the cell | +| | | centerright | Centers cell's contents with extra padding | +| | | | allocated to the end of the cell | +| padding_char | str | | Single character used to fill extra and | +| | | | normal padding with. Default is a blank | +| | | | space ` `. | +| newline_char | str | | Custom character appended to each row to | +| | | | force a newline. Default is `\n` | +| float_rounding | int | | Keep values up until `float_rounder` after the | +| | | | decimal dot. Default is `2`, but can also | +| | | | be set to `None` to not round down | +| | | | floats. | +| quote | bool | | If `true` (default) wrap the generated markdown | +| | | | table with block code quote | +| multiline | dict | | Predefine the column width by passing a | +| | | | dict() with keys matching those of the | +| | | | passed data and values -- the desired width | +| | | | for each corresponding column. | ``` ## Using it with a dataframe diff --git a/markdownTable/__gendesc__.py b/markdownTable/__gendesc__.py new file mode 100644 index 0000000..8b1f5ed --- /dev/null +++ b/markdownTable/__gendesc__.py @@ -0,0 +1,24 @@ +from __init__ import markdownTable + +params = [ + {'param': 'row_sep', 'type': 'str', 'values': '', 'description': 'Row separation strategy using `----` as pattern'}, + {'param': '', 'type': '', 'values': 'always', 'description': 'Separate each row'}, + {'param': '', 'type': '', 'values': 'topbottom', 'description': 'Separate the top (header) and bottom (last row) of the table'}, + {'param': '', 'type': '', 'values': 'markdown', 'description': 'Separate only header from body'}, + {'param': '', 'type': '', 'values': 'None', 'description': 'No row separators will be inserted'}, + {'param': 'padding_width', 'type': 'int', 'values': '', 'description': 'Allocate padding to all table cells'}, + {'param': 'padding_weight', 'type': 'str', 'values': '', 'description': 'Strategy for allocating padding within table cells'}, + {'param': '', 'type': '', 'values': 'left', 'description': 'Aligns cell\'s contents to the end of the cell'}, + {'param': '', 'type': '', 'values': 'right', 'description': 'Aligns cell\'s contents to the beginning of the cell'}, + {'param': '', 'type': '', 'values': 'centerleft', 'description': 'Centers cell\'s contents with extra padding allocated to the beginning of the cell'}, + {'param': '', 'type': '', 'values': 'centerright', 'description': 'Centers cell\'s contents with extra padding allocated to the end of the cell'}, + {'param': 'padding_char', 'type': 'str', 'values': '', 'description': 'Single character used to fill extra and normal padding with. Default is a blank space ` `.'}, + {'param': 'newline_char', 'type': 'str', 'values': '', 'description': 'Custom character appended to each row to force a newline. Default is `\\n`'}, + {'param': 'float_rounding', 'type': 'int', 'values': '', 'description': 'Keep values up until `float_rounder` after the decimal dot. Default is `2`, but can also be set to `None` to not round down floats.'}, + {'param': 'quote', 'type': 'bool', 'values': '', 'description': 'If `true` (default) wrap the generated markdown table with block code quote'}, + {'param': 'multiline', 'type': 'dict', 'values': '', 'description': 'Predefine the column width by passing a dict() with keys matching those of the passed data and values -- the desired width for each corresponding column.'}, +] + +widths = {'param': 17, 'type': 17, 'values': 15, 'description': 60} + +print(markdownTable(params).setParams(padding_char=' ', padding_weight='centerleft', padding_width=3, row_sep='markdown', multiline=widths).getMarkdown()) diff --git a/markdownTable/__init__.py b/markdownTable/__init__.py index eaba7fe..7ac8616 100644 --- a/markdownTable/__init__.py +++ b/markdownTable/__init__.py @@ -90,8 +90,8 @@ def validate(self): for key, item in row.items(): multiline_data = row[key].split(" ") multiline_max_width = max(multiline_data, key=len) - if (self.var_padding[key]) < len(multiline_max_width): - raise Exception('Contiguous string exists longer than the allocated column width') + if (self.var_padding[key]) < len(multiline_max_width) + self.padding_width: + raise Exception(f'Contiguous string exists longer than the allocated column width for column "{key}" and padding_width "{self.padding_width}"') def getPadding(self): padding = dict() @@ -183,29 +183,31 @@ def getMultilineRow(self, item): for key in self.data[0].keys(): items = item[key].split(" ") column_list = [] - multiline_row = '' - for ix, sub_item in enumerate(items): - multiline_row += sub_item - if ix+1 < len(items) and len(multiline_row) + len(items[ix+1]) > self.var_padding[key]: - column_list.append(multiline_row) - multiline_row = '' + single_row = [] + buffer = 0 + spacing_between_items = 0 + while len(items) > 0: + print(items) + if len(items[0]) + buffer + spacing_between_items + self.padding_width <= self.var_padding[key]: + buffer += len(items[0]) + single_row.append(items.pop(0)) + spacing_between_items += len(single_row) - 1 else: - multiline_row += ' ' - if ix+1 == len(items): - column_list.append(sub_item) + column_list.append(" ".join(single_row)) + single_row = [] + buffer = 0 + spacing_between_items = 0 + column_list.append(' '.join(single_row)) multiline_items[key] = column_list # get the max vertical length of the multiline row - multiline_rows = 0 - for key, value in multiline_items.items(): - if len(value) > multiline_rows: - multiline_rows = len(value) + multiline_rows = max(map(len, multiline_items.values())) # fill in blank values for key, value in multiline_items.items(): if len(value) < multiline_rows: for i in range(len(value), multiline_rows): - multiline_items[key].append(" ") + multiline_items[key].append(self.padding_char*self.var_padding[key]) rows = '' for ix in range(0, multiline_rows): diff --git a/setup.py b/setup.py index de88a03..59aa32d 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="py-markdown-table", - version="0.3.1", + version="0.3.2", author="hvalev", description="A package used to generate basic markdown tables from a list of dicts", long_description=long_description,