Skip to content

Commit

Permalink
bugfix for multiline; updated README
Browse files Browse the repository at this point in the history
  • Loading branch information
hvalev committed Aug 17, 2022
1 parent 071617d commit 1e68915
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 39 deletions.
56 changes: 34 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Any,int> | | 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
Expand Down
24 changes: 24 additions & 0 deletions markdownTable/__gendesc__.py
Original file line number Diff line number Diff line change
@@ -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<Any,int>', '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())
34 changes: 18 additions & 16 deletions markdownTable/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 1e68915

Please sign in to comment.