diff --git a/README.md b/README.md index 016becd..09a541f 100644 --- a/README.md +++ b/README.md @@ -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. -- jinjafx.tabulate(datarows: Optional[List[List[String]]], *, cols: Optional[List[String]], include_alignment: Optional[Boolean]=False) -> String +- jinjafx.tabulate(datarows: Optional[List[List[String]]], *, cols: Optional[List[String]], colons: Optional[Boolean]=False) -> String 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: @@ -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`. - jinjafx.expand(value: String) -> List[String] diff --git a/jinjafx.py b/jinjafx.py index aeb8978..0a2bd1d 100755 --- a/jinjafx.py +++ b/jinjafx.py @@ -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 @@ -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()