Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add table format "colon_grid" #341

Merged
merged 5 commits into from
Sep 26, 2024
Merged

Add table format "colon_grid" #341

merged 5 commits into from
Sep 26, 2024

Conversation

dn813
Copy link

@dn813 dn813 commented Sep 26, 2024

To close #332, this PR adds a new table format "colon_grid" which is the same as the "grid" format but adds colons to the line below the header to indicate a column's alignment. This requires forcing left alignment on the data in the text output of tabulate.

With this format, tabulate can create Pandoc-compatible tables with alignments.

Existing grid format:

+------------+------------+------------+
| Column 1   | Column 2   | Column 3   |
+============+============+============+
| 1          | a          | 1.23       |
+------------+------------+------------+
| 2          | b          | 12.3       |
+------------+------------+------------+
| 3          | c          | 123        |
+------------+------------+------------+

Grid format using existing alignment functionality:

+------------+------------+------------+
|   Column 1 | Column 2   |   Column 3 |
+============+============+============+
|          1 | a          |       1.23 |
+------------+------------+------------+
|          2 | b          |      12.3  |
+------------+------------+------------+
|          3 | c          |     123    |
+------------+------------+------------+

Broken Pandoc rendering:

345424732-71a74103-4560-4f52-bd3d-163171344a3d

New colon_grid format:

+------------+------------+------------+
| Column 1   | Column 2   | Column 3   |
+===========:+:===========+===========:+
| 1          | a          | 1.23       |
+------------+------------+------------+
| 2          | b          | 12.3       |
+------------+------------+------------+
| 3          | c          | 123        |
+------------+------------+------------+

Pandoc rendering of a colon_grid:

345426116-064c2912-23cc-471d-ae7b-fa895a97ccfe

@astanin
Copy link
Owner

astanin commented Sep 26, 2024

There is a extra empty line in the colon_grid doctests (tabulate/__init__.py:1865 and 1877).

Also the second doctest fails even when these empty lines are removed (there is no ':' in the first column).

_______________________________ [doctest] tabulate.tabulate _______________________________
1864     ...                ["strings", "numbers"], "colon_grid"))
1865     +-----------+-----------+
1866     | strings   | numbers   |
1867     +:==========+:==========+
1868     | spam      | 41.9999   |
1869     +-----------+-----------+
1870     | eggs      | 451       |
1871     +-----------+-----------+
1872
1873     >>> print(tabulate([["spam", 41.9999], ["eggs", "451.0"]],
Differences (unified diff with -expected +actual):
    @@ -1,5 +1,5 @@
     +-----------+-----------+
     | strings   | numbers   |
    -+==========:+:==========+
    ++===========+:==========+
     | spam      | 41.9999   |
     +-----------+-----------+

...\python-tabulate\tabulate\__init__.py:1873: DocTestFailure

@dn813
Copy link
Author

dn813 commented Sep 26, 2024

Thanks for pointing out the failing doctests, that's something I should have checked! Should be working now.

@astanin astanin merged commit 3b66d2d into astanin:master Sep 26, 2024
4 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add new Pandoc grid table format
2 participants