astanin / python-tabulate

Pretty-print tabular data in Python, a library and a command-line utility. Repository migrated from bitbucket.org/astanin/python-tabulate.
https://pypi.org/project/tabulate/
MIT License
2.14k stars 164 forks source link

An option to set minimum colum width per column #86

Open risoms opened 4 years ago

risoms commented 4 years ago

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)
FPSUsername commented 3 years ago

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.

astanin commented 3 years ago

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.

FPSUsername commented 3 years ago

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).

JBalanza commented 1 year ago

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.