Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
cmason3 committed May 8, 2024
1 parent 9274fb3 commit 2408063
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ This variable will contain the total number of rows within the data.

This function is used to access all the row and column data that JinjaFx is currently traversing through. The first row (0) will contain the header row with subsequent rows containing the row data - it is accessed using `jinjafx.data(row, col)`. If you wish to access the columns via their case-sensitive name then you can also use `jinjafx.data(row, 'FIELD')`. The `row` argument is mandatory, but if you omit the `col` argument then it will return the whole row as a list.

- <code><b>jinjafx.tabulate(datarows</b>: Optional[List[List[String]]]<b>, *, cols</b>: Optional[List[String]]<b>, include_alignment</b>: Optional[Boolean]<b>=False)</b> -> String</code>
- <code><b>jinjafx.tabulate(datarows</b>: Optional[List[List[String]]]<b>, *, cols</b>: Optional[List[String]]<b>, colons</b>: Optional[Boolean]<b>=False)</b> -> String</code>

This function will produce a GitHub Markdown styled table using either the provided `datarows` variable, or (if omitted) using the data from `data.csv`, e.g:

Expand Down Expand Up @@ -611,7 +611,7 @@ This will produce the following table:
| 6 | 4 |
```

This function only supports left alignment of columns at present, but if you wish to include the `:` as used by GitHub Markdown to force left alignment, you can set `include_alignment` to `True`.
All columns are left aligned, except if the column value is of type `int` or `float` (`data.csv` uses `:int` and `:float` syntax to force this) and then they are right aligned. At this point you can't change this, but if you wish to include the `:` as used by GitHub Markdown to force the alignment, you can set `colons` to `True`.

- <code><b>jinjafx.expand(value</b>: String<b>)</b> -> List[String]</code>

Expand Down
21 changes: 14 additions & 7 deletions jinjafx.py
Original file line number Diff line number Diff line change
Expand Up @@ -1126,8 +1126,7 @@ def __jfx_fields(self, field=None, ffilter={}):
return field_values


def __jfx_tabulate(self, datarows=None, *, cols=None, include_alignment=False):
alignment = [":", " "] if include_alignment else [" ", " "]
def __jfx_tabulate(self, datarows=None, *, cols=None, colons=False):
colwidth = []
colmap = []
offset = 0
Expand All @@ -1149,19 +1148,27 @@ def __jfx_tabulate(self, datarows=None, *, cols=None, include_alignment=False):
except Exception:
raise JinjaFx.TemplateError(f'invalid column "{c}" passed to jinjafx.tabulate()')

colalign = [["<", ":", " "]] * len(datarows[0])
coltype = [0] * len(datarows[0])

for c in range(len(datarows[0])):
colwidth.append(len(datarows[0][c]))

for r in range(1, len(datarows)):
for c in range(offset, len(datarows[r])):
if colwidth[c - offset] < len(datarows[r][c]):
colwidth[c - offset] = len(datarows[r][c])
coltype[c - offset] |= (1<<(isinstance(datarows[r][c], (int, float))))
if colwidth[c - offset] < len(str(datarows[r][c])):
colwidth[c - offset] = len(str(datarows[r][c]))

for c, t in enumerate(coltype):
if t == 2:
colalign[c] = [">", " ", ":"]

o = "| " + " | ".join([f"{datarows[0][c]:{colwidth[c]}}" for c in colmap]) + " |\n"
o += f"|{alignment[0]}" + f"{alignment[1]}|{alignment[0]}".join([f"{'-' * colwidth[c]}" for c in colmap]) + f"{alignment[1]}|\n"
o = "| " + " | ".join([f"{datarows[0][c]:{colalign[c][0]}{colwidth[c]}}" for c in colmap]) + " |\n"
o += "|" + "|".join([f"{colalign[c][1] if colons else ' '}{'-' * colwidth[c]}{colalign[c][2] if colons else ' '}" for c in colmap]) + "|\n"

for r in range(1, len(datarows)):
o += "| " + " | ".join([f"{datarows[r][c + offset]:{colwidth[c]}}" for c in colmap]) + " |\n"
o += "| " + " | ".join([f"{datarows[r][c + offset]:{colalign[c][0]}{colwidth[c]}}" for c in colmap]) + " |\n"

return o.strip()

Expand Down

0 comments on commit 2408063

Please sign in to comment.