Skip to content

Commit

Permalink
Merge pull request #96 from cmason3/v1.20.1
Browse files Browse the repository at this point in the history
V1.20.1
  • Loading branch information
cmason3 authored May 15, 2024
2 parents 3ba9240 + 23ffc38 commit 0cbe49d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## CHANGELOG

### [1.20.1] - May 15, 2024
- Added suport for different table styles for `jinjafx.tabulate()`

### [1.20.0] - May 8, 2024
- Added support for `jinjafx.tabulate()` to produce GitHub Markdown style tables from `data.csv`

Expand Down Expand Up @@ -525,6 +528,7 @@ Updated `to_yaml` and `to_nice_yaml` to use `SafeDumper`
- Initial release


[1.20.1]: https://github.com/cmason3/jinjafx/compare/v1.20.0...v1.20.1
[1.20.0]: https://github.com/cmason3/jinjafx/compare/v1.19.3...v1.20.0
[1.19.3]: https://github.com/cmason3/jinjafx/compare/v1.19.2...v1.19.3
[1.19.2]: https://github.com/cmason3/jinjafx/compare/v1.19.1...v1.19.2
Expand Down
37 changes: 35 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>, colons</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>, style</b>: Optional[String]<b>="default")</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,40 @@ This will produce the following table:
| 6 | 4 |
```

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`.
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. The `style` argument can be specified to change the style of table - it currently supports 3 different styles:

#### default

This is the default and is based on GitHub Markdown, except we don't include the alignment colons:

```
| A | B | C |
| - | - | - |
| 1 | 2 | 3 |
| 4 | 5 | 6 |
```

#### github

This is the same as the default style, except we explicitly include the alignment colons:

```
| A | B | C |
|:- |:- |:- |
| 1 | 2 | 3 |
| 4 | 5 | 6 |
```

#### simple

This style will produce a table which looks like this:

```
A B C
- - -
1 2 3
4 5 6
```

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

Expand Down
21 changes: 16 additions & 5 deletions jinjafx.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from cryptography.hazmat.primitives.ciphers.modes import CTR
from cryptography.exceptions import InvalidSignature

__version__ = '1.20.0'
__version__ = '1.20.1'

__all__ = ['JinjaFx', 'Vault']

Expand Down Expand Up @@ -1126,12 +1126,16 @@ def __jfx_fields(self, field=None, ffilter={}):
return field_values


def __jfx_tabulate(self, datarows=None, *, cols=None, colons=False):
def __jfx_tabulate(self, datarows=None, *, cols=None, style='default'):
colwidth = []
colmap = []
offset = 0
o = ''

style = style.lower()
if style not in ('default', 'github', 'simple'):
raise JinjaFx.TemplateError(f'invalid style "{style}" passed to jinjafx.tabulate()')

if datarows is None:
datarows = self.__g_datarows
offset = 1
Expand Down Expand Up @@ -1164,11 +1168,18 @@ def __jfx_tabulate(self, datarows=None, *, cols=None, colons=False):
if t == 2:
colalign[c] = [">", " ", ":"]

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"
if style == 'simple':
o = " ".join([f"{datarows[0][c]:{colalign[c][0]}{colwidth[c]}}" for c in colmap]) + "\n"
o += " ".join([f"{'-' * colwidth[c]}" for c in colmap]) + "\n"
else:
o = "| " + " | ".join([f"{datarows[0][c]:{colalign[c][0]}{colwidth[c]}}" for c in colmap]) + " |\n"
o += "|" + "|".join([f"{colalign[c][1] if style == 'github' else ' '}{'-' * colwidth[c]}{colalign[c][2] if style == 'github' else ' '}" for c in colmap]) + "|\n"

for r in range(1, len(datarows)):
o += "| " + " | ".join([f"{datarows[r][c + offset]:{colalign[c][0]}{colwidth[c]}}" for c in colmap]) + " |\n"
if style == "simple":
o += " ".join([f"{datarows[r][c + offset]:{colalign[c][0]}{colwidth[c]}}" for c in colmap]) + "\n"
else:
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 0cbe49d

Please sign in to comment.