jazzband / prettytable

Display tabular data in a visually appealing ASCII table format
https://pypi.org/project/PrettyTable/
Other
1.36k stars 157 forks source link

Markdown tables with narrow columns don't render #295

Closed hugovk closed 6 months ago

hugovk commented 6 months ago

What did you do?

Create a Markdown table with max zero, one or two characters in a column.

What did you expect to happen?

Table to render.

What actually happened?

Just the code was shown.

What versions are you using?

Please include code that reproduces the issue.

The best reproductions are self-contained scripts with minimal dependencies.

from prettytable import PLAIN_COLUMNS, PrettyTable, MARKDOWN

table = PrettyTable()
table.set_style(MARKDOWN)
table.add_row([1, "Adelaide", 1295, 1158259, 600.5])
table.add_row([2, "Brisbane", 5905, 1857594, 1146.4])
table.add_row([3, "Darwin", 112, 120900, 1714.7])
table.add_row([4, "Hobart", 1357, 205556, 619.5])
table.add_row([5, "Sydney", 2058, 4336374, 1214.8])
table.add_row([6, "Melbourne", 1566, 3806092, 646.9])
table.add_row([7, "Perth", 5386, 1554769, 869.4])

for first_header in ("", "A", "AB", "ABC"):
    table.field_names = [first_header, "City name", "Area", "Population", "Annual Rainfall"]
    print(table)
    print()

3.9.0

Before using GFM from https://github.com/jazzband/prettytable/pull/268, all render:

|   | City name | Area | Population | Annual Rainfall |
|:-:|:---------:|:----:|:----------:|:---------------:|
| 1 |  Adelaide | 1295 |  1158259   |      600.5      |
| 2 |  Brisbane | 5905 |  1857594   |      1146.4     |
| 3 |   Darwin  | 112  |   120900   |      1714.7     |
| 4 |   Hobart  | 1357 |   205556   |      619.5      |
| 5 |   Sydney  | 2058 |  4336374   |      1214.8     |
| 6 | Melbourne | 1566 |  3806092   |      646.9      |
| 7 |   Perth   | 5386 |  1554769   |      869.4      |

| A | City name | Area | Population | Annual Rainfall |
|:-:|:---------:|:----:|:----------:|:---------------:|
| 1 |  Adelaide | 1295 |  1158259   |      600.5      |
| 2 |  Brisbane | 5905 |  1857594   |      1146.4     |
| 3 |   Darwin  | 112  |   120900   |      1714.7     |
| 4 |   Hobart  | 1357 |   205556   |      619.5      |
| 5 |   Sydney  | 2058 |  4336374   |      1214.8     |
| 6 | Melbourne | 1566 |  3806092   |      646.9      |
| 7 |   Perth   | 5386 |  1554769   |      869.4      |

| AB | City name | Area | Population | Annual Rainfall |
|:--:|:---------:|:----:|:----------:|:---------------:|
| 1  |  Adelaide | 1295 |  1158259   |      600.5      |
| 2  |  Brisbane | 5905 |  1857594   |      1146.4     |
| 3  |   Darwin  | 112  |   120900   |      1714.7     |
| 4  |   Hobart  | 1357 |   205556   |      619.5      |
| 5  |   Sydney  | 2058 |  4336374   |      1214.8     |
| 6  | Melbourne | 1566 |  3806092   |      646.9      |
| 7  |   Perth   | 5386 |  1554769   |      869.4      |

| ABC | City name | Area | Population | Annual Rainfall |
|:---:|:---------:|:----:|:----------:|:---------------:|
|  1  |  Adelaide | 1295 |  1158259   |      600.5      |
|  2  |  Brisbane | 5905 |  1857594   |      1146.4     |
|  3  |   Darwin  | 112  |   120900   |      1714.7     |
|  4  |   Hobart  | 1357 |   205556   |      619.5      |
|  5  |   Sydney  | 2058 |  4336374   |      1214.8     |
|  6  | Melbourne | 1566 |  3806092   |      646.9      |
|  7  |   Perth   | 5386 |  1554769   |      869.4      |
City name Area Population Annual Rainfall
1 Adelaide 1295 1158259 600.5
2 Brisbane 5905 1857594 1146.4
3 Darwin 112 120900 1714.7
4 Hobart 1357 205556 619.5
5 Sydney 2058 4336374 1214.8
6 Melbourne 1566 3806092 646.9
7 Perth 5386 1554769 869.4
A City name Area Population Annual Rainfall
1 Adelaide 1295 1158259 600.5
2 Brisbane 5905 1857594 1146.4
3 Darwin 112 120900 1714.7
4 Hobart 1357 205556 619.5
5 Sydney 2058 4336374 1214.8
6 Melbourne 1566 3806092 646.9
7 Perth 5386 1554769 869.4
AB City name Area Population Annual Rainfall
1 Adelaide 1295 1158259 600.5
2 Brisbane 5905 1857594 1146.4
3 Darwin 112 120900 1714.7
4 Hobart 1357 205556 619.5
5 Sydney 2058 4336374 1214.8
6 Melbourne 1566 3806092 646.9
7 Perth 5386 1554769 869.4
ABC City name Area Population Annual Rainfall
1 Adelaide 1295 1158259 600.5
2 Brisbane 5905 1857594 1146.4
3 Darwin 112 120900 1714.7
4 Hobart 1357 205556 619.5
5 Sydney 2058 4336374 1214.8
6 Melbourne 1566 3806092 646.9
7 Perth 5386 1554769 869.4

3.10 and main

Only the one with "ABC" in the first column renders.

|   | City name | Area | Population | Annual Rainfall |
| : | :-------: | :--: | :--------: | :-------------: |
| 1 |  Adelaide | 1295 |  1158259   |      600.5      |
| 2 |  Brisbane | 5905 |  1857594   |      1146.4     |
| 3 |   Darwin  | 112  |   120900   |      1714.7     |
| 4 |   Hobart  | 1357 |   205556   |      619.5      |
| 5 |   Sydney  | 2058 |  4336374   |      1214.8     |
| 6 | Melbourne | 1566 |  3806092   |      646.9      |
| 7 |   Perth   | 5386 |  1554769   |      869.4      |

| A | City name | Area | Population | Annual Rainfall |
| : | :-------: | :--: | :--------: | :-------------: |
| 1 |  Adelaide | 1295 |  1158259   |      600.5      |
| 2 |  Brisbane | 5905 |  1857594   |      1146.4     |
| 3 |   Darwin  | 112  |   120900   |      1714.7     |
| 4 |   Hobart  | 1357 |   205556   |      619.5      |
| 5 |   Sydney  | 2058 |  4336374   |      1214.8     |
| 6 | Melbourne | 1566 |  3806092   |      646.9      |
| 7 |   Perth   | 5386 |  1554769   |      869.4      |

| AB | City name | Area | Population | Annual Rainfall |
| :: | :-------: | :--: | :--------: | :-------------: |
| 1  |  Adelaide | 1295 |  1158259   |      600.5      |
| 2  |  Brisbane | 5905 |  1857594   |      1146.4     |
| 3  |   Darwin  | 112  |   120900   |      1714.7     |
| 4  |   Hobart  | 1357 |   205556   |      619.5      |
| 5  |   Sydney  | 2058 |  4336374   |      1214.8     |
| 6  | Melbourne | 1566 |  3806092   |      646.9      |
| 7  |   Perth   | 5386 |  1554769   |      869.4      |

| ABC | City name | Area | Population | Annual Rainfall |
| :-: | :-------: | :--: | :--------: | :-------------: |
|  1  |  Adelaide | 1295 |  1158259   |      600.5      |
|  2  |  Brisbane | 5905 |  1857594   |      1146.4     |
|  3  |   Darwin  | 112  |   120900   |      1714.7     |
|  4  |   Hobart  | 1357 |   205556   |      619.5      |
|  5  |   Sydney  | 2058 |  4336374   |      1214.8     |
|  6  | Melbourne | 1566 |  3806092   |      646.9      |
|  7  |   Perth   | 5386 |  1554769   |      869.4      |
City name Area Population Annual Rainfall
1 Adelaide 1295 1158259 600.5
2 Brisbane 5905 1857594 1146.4
3 Darwin 112 120900 1714.7
4 Hobart 1357 205556 619.5
5 Sydney 2058 4336374 1214.8
6 Melbourne 1566 3806092 646.9
7 Perth 5386 1554769 869.4
A City name Area Population Annual Rainfall
1 Adelaide 1295 1158259 600.5
2 Brisbane 5905 1857594 1146.4
3 Darwin 112 120900 1714.7
4 Hobart 1357 205556 619.5
5 Sydney 2058 4336374 1214.8
6 Melbourne 1566 3806092 646.9
7 Perth 5386 1554769 869.4
AB City name Area Population Annual Rainfall
1 Adelaide 1295 1158259 600.5
2 Brisbane 5905 1857594 1146.4
3 Darwin 112 120900 1714.7
4 Hobart 1357 205556 619.5
5 Sydney 2058 4336374 1214.8
6 Melbourne 1566 3806092 646.9
7 Perth 5386 1554769 869.4
ABC City name Area Population Annual Rainfall
1 Adelaide 1295 1158259 600.5
2 Brisbane 5905 1857594 1146.4
3 Darwin 112 120900 1714.7
4 Hobart 1357 205556 619.5
5 Sydney 2058 4336374 1214.8
6 Melbourne 1566 3806092 646.9
7 Perth 5386 1554769 869.4

The difference is that before, there was at least one hyphen in the row under the first column. But after there is not. We should make sure there is at least one hyphen.