alttch/rapidtables

Multiline values or text wrapping support.

Ronserruya opened this issue · 5 comments

Given a multiline value, or a value which is too long, the table doesn't format correctly

In [30]: data = [ 
    ...:     { 'name': 'John', 'salary': 2000, 'job': 'DevOps' }, 
    ...:     { 'name': 'Jack', 'salary': 2500, 'job': 'Multi\nLine\nJob' }, 
    ...:     { 'name': 'Diana', 'salary': None, 'job': 'Student' }, 
    ...:     { 'name': 'Ken', 'salary': 1800, 'job': 'Q/A' } 
    ...: ]                                                                                                                                                                   

In [31]: rapidtables.print_table(data,tablefmt="md")                                                                                                                         
| name  | salary | job            |
|-------|--------|----------------|
| John  |   2000 | DevOps         |
| Jack  |   2500 | Multi
Line
Job |
| Diana |        | Student        |
| Ken   |   1800 | Q/A            |

In [32]: data = [ 
    ...:     { 'name': 'John', 'salary': 2000, 'job': 'DevOps' }, 
    ...:     { 'name': 'Jack', 'salary': "Multi\nLine\nSalary", 'job': 'Developer' }, 
    ...:     { 'name': 'Diana', 'salary': None, 'job': 'Student' }, 
    ...:     { 'name': 'Ken', 'salary': 1800, 'job': 'Q/A' } 
    ...: ]                                                                                                                                                                   

In [33]: rapidtables.print_table(data,tablefmt="md")                                                                                                                         
| name  | salary            | job       |
|-------|-------------------|-----------|
| John  | 2000              | DevOps    |
| Jack  | Multi
Line
Salary | Developer |
| Diana |                   | Student   |
| Ken   | 1800              | Q/A       |

In [34]: data = [ 
    ...:     { 'name': 'John', 'salary': 2000, 'job': 'DevOps' }, 
    ...:     { 'name': 'Jack', 'salary': 2500, 'job': 'a'*200}, 
    ...:     { 'name': 'Diana', 'salary': None, 'job': 'Student' }, 
    ...:     { 'name': 'Ken', 'salary': 1800, 'job': 'Q/A' } 
    ...: ]                                                                                                                                                                   

In [35]: rapidtables.print_table(data,tablefmt="md")                                                                                                                         
| name  | salary | job                                                                                                                                                                                                      |
|-------|--------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| John  |   2000 | DevOps                                                                                                                                                                                                   |
| Jack  |   2500 | aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
| Diana |        | Student                                                                                                                                                                                                  |
| Ken   |   1800 | Q/A                                                                                                                                                                                                      |

The a*200 table looks good on github since it just creates this horizontal scroll view, but looks bad in a terminal cause terminals wrap text.

(This is not specific to md btw)
Im not sure what the solution can be since multiline cells differ between the styles of tables.
md tables need to use <br> instead of \n for new lines within the cell, while that would make them look bad printed, they would render correctly in a markdown viewer.

rst is even more complicated, not sure how multiline works there.

If you have any idea for a solution you want to do, id be happy to try and help to implement it.

multi-line text tables are long discussed, as they're someway unreadable and unusable (at least it's hard to use grep), I don't plan to realize this feature in the nearest future but if someone make a pull request and it doesn't slow down the main job, I would be very appreciated )

if you convert rst/md to html - yes, you may try prepare data, replacing \n to br /> before calling rapidtables, depending on your rst2html converter, it may work.

about looooong rows - I work with rapidtables with pretty long rows (~500-600 chars), but the table is printed in a curses-like pad, so it has some kind of "scroll bar" too. If your terminal wraps rows incorrectly, you may limit column with "max_column_width" function arg (which works like pandas.options.display.max_colwidth). anyway such long wrapped tables are unreadable.

Was about to add same comment today - support for a multi-line table as defined by reStructured Text would be incredibly useful:

+------------+------------+-----------+
| Header 1   | Header 2   | Header 3  |
+============+============+===========+
| body row 1 | column 2   | column 3  |
+------------+------------+-----------+
| body row 2 | Cells may span columns.|
+------------+------------+-----------+
| body row 3 | Cells may  | - Cells   |
+------------+ span rows. | - contain |
| body row 4 |            | - blocks. |
+------------+------------+-----------+

Even a first pass of output that looks like this would be useful:

+------------+------------+-----------+
| Header 1   | Header 2   | Header 3  |
+============+============+===========+
| body row 1 | column 2   | column 3  |
+------------+------------+-----------+
| body row 2 | Line 1     | Test      |
|            | Line 2     | - stuff   |
|            | Line 3     |           | 
+------------+------------+-----------+

okay guys, you won )

starting from 0.1.5:

new param for make_table: allow_multiline=True

new param for format_table:

multiline=MULTILINE_ALLOW - allow multiline cols
multiline=MULTILINE_EXTENDED_INFO - if set, rows are returned as (bool, row) where bool is True if it's a real row start and False if it's a helper row with multi-line data

I've also added tablefmt='rstgrid' for make_table to generate rst grid tables.

Works great, thanks :)