python-tabulate icon indicating copy to clipboard operation
python-tabulate copied to clipboard

An option to set minimum colum width per column

Open risoms opened this issue 4 years ago • 4 comments

Is it possible to set the width of a column? I'm hoping to do this for cells that will include a hyperlink and are processed in an external service like sphinx or GitHub to create an anchor for a block of text.

date          RQ
------------  ----------------------------------------------------------------------
2020-09-01    [RQ111111](https://github.com/user/repo/blob/branch/other_file.md)

risoms avatar Sep 16 '20 19:09 risoms

There should be a tablefmt that will "ignore" Markdown syntax such as [URLs](https://...), **bold**, *cursive* (or __cursive__), and more. A use case example of this is generating a formatted Telegram message using a Telegram API that supports Markdown parsing such as Telethon.

Unfortunately the simple and github tablefmt don't do this, resulting in a table with a column width that's wider than necessary.

FPSUsername avatar Feb 20 '21 11:02 FPSUsername

That's a good point, but how exactly would you like your example to be formatted in plain text? Especially if there is a third column?

table = [['date', 'RQ', 'comment'],
 ['2020-09-01',
  '[RQ111111](https://github.com/user/repo/blob/branch/other_file.md)',
  'Hello World']]
print(tabulate(table, headers="firstrow"))

Right now the third column is not overlapped with the second. If we somehow reduce the width of the second column to be 10 characters, where will the link be written?

date        RQ                                                                  comment
----------  ------------------------------------------------------------------  -----------
2020-09-01  [RQ111111](https://github.com/user/repo/blob/branch/other_file.md)  Hello World

The output we have now is readable in plain text, and if it is parsed by pandoc, sphinx, or other markup processor, it will be converted to a table, where the width of the plain-text columns will be forgotten.

astanin avatar Feb 20 '21 13:02 astanin

It is useful on special occasions as I mentioned above. In the case of the Telegram message, the API will convert the markdown formatted string into a clickable link. I am not entirely sure what you mean with

the width of the plain-text columns will be forgotten

Here is an example using my Telegram Bot. The output is indeed readable in plain text, however, the output in the form of a message is completely messed up, especially the spacing. Telegram

Now that I'm thinking about it, in my case, it would also be useful to have the property of setting a minimum width (per column).

FPSUsername avatar Feb 20 '21 14:02 FPSUsername

Hi! great library! Also would be interesting to add a maximum width so the rows does not appear in multiple lines if the data in the cells are too large. With the maximum width either the data is truncated or splitted in several lines but inside the same cell delimiters.

JBalanza avatar Apr 12 '23 15:04 JBalanza