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 support for alignments in Markdown tables #261

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,16 +213,16 @@ eggs 451
bacon 0
```

`github` follows the conventions of GitHub flavored Markdown. It
corresponds to the `pipe` format without alignment colons:
`github` follows the conventions of GitHub flavored Markdown. This
format uses colons to indicate column alignment:

```pycon
>>> print(tabulate(table, headers, tablefmt="github"))
| item | qty |
|--------|-------|
| spam | 42 |
| eggs | 451 |
| bacon | 0 |
| item | qty |
| :------ | ----: |
| spam | 42 |
| eggs | 451 |
| bacon | 0 |
```

`grid` is like tables formatted by Emacs'
Expand Down
40 changes: 24 additions & 16 deletions tabulate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,27 +110,29 @@ def _is_separating_line(row):
return is_sl


def _pipe_segment_with_colons(align, colwidth):
def _segment_with_colons(align, colwidth, sep="-"):
"""Return a segment of a horizontal line with optional colons which
indicate column's alignment (as in `pipe` output format)."""
w = colwidth
if align in ["right", "decimal"]:
return ("-" * (w - 1)) + ":"
return (sep * (w - 1)) + ":"
elif align == "center":
return ":" + ("-" * (w - 2)) + ":"
return ":" + (sep * (w - 2)) + ":"
elif align == "left":
return ":" + ("-" * (w - 1))
return ":" + (sep * (w - 1))
else:
return "-" * w
return sep * w


def _pipe_line_with_colons(colwidths, colaligns):
def _line_with_colons(colwidths, colaligns, begin="|", hline="|", sep="-", end="|"):
"""Return a horizontal line with optional colons to indicate column's
alignment (as in `pipe` output format)."""
alignment (as in `pipe` and `github` output format)."""
if not colaligns: # e.g. printing an empty data frame (github issue #15)
colaligns = [""] * len(colwidths)
segments = [_pipe_segment_with_colons(a, w) for a, w in zip(colaligns, colwidths)]
return "|" + "|".join(segments) + "|"
segments = [
_segment_with_colons(a, w, sep=sep) for a, w in zip(colaligns, colwidths)
]
return begin + hline.join(segments) + end


def _mediawiki_row_with_attrs(separator, cell_values, colwidths, colaligns):
Expand Down Expand Up @@ -470,18 +472,24 @@ def escape_empty(val):
with_header_hide=None,
),
"github": TableFormat(
lineabove=Line("|", "-", "|", "|"),
linebelowheader=Line("|", "-", "|", "|"),
lineabove=partial(
_line_with_colons, begin="| ", hline=" | ", sep="-", end=" |"
),
linebelowheader=partial(
_line_with_colons, begin="| ", hline=" | ", sep="-", end=" |"
),
linebetweenrows=None,
linebelow=None,
headerrow=DataRow("|", "|", "|"),
datarow=DataRow("|", "|", "|"),
padding=1,
headerrow=DataRow("| ", " | ", " |"),
datarow=DataRow("| ", " | ", " |"),
padding=0,
with_header_hide=["lineabove"],
),
"pipe": TableFormat(
lineabove=_pipe_line_with_colons,
linebelowheader=_pipe_line_with_colons,
lineabove=partial(_line_with_colons, begin="|", hline="|", sep="-", end="|"),
linebelowheader=partial(
_line_with_colons, begin="|", hline="|", sep="-", end="|"
),
linebetweenrows=None,
linebelow=None,
headerrow=DataRow("|", "|", "|"),
Expand Down
2 changes: 1 addition & 1 deletion test/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ def test_github():
expected = "\n".join(
[
"| strings | numbers |",
"|-----------|-----------|",
"| :-------- | --------: |",
"| spam | 41.9999 |",
"| eggs | 451 |",
]
Expand Down