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

Currently rich supports printing tables without columns, but the title will be set to 1 character width: #1044

Closed
willmcgugan opened this issue Feb 22, 2021 · 4 comments
Labels
bug Something isn't working

Comments

@willmcgugan
Copy link
Collaborator

Currently rich supports printing tables without columns, but the title will be set to 1 character width:

from rich.console import Console
from rich.table import Table

table = Table(title="Star Wars Movies")

console = Console()
console.print(table)

output:

S
t
a
r

W
a
r
s

M
o
v
i
e
s
┏┓
└┘

So I want to set a min-width for the table, but raised a AssertionError:

....
File "/Users/waynerv/Repos/QData/qdatamgr-cli/venv/lib/python3.8/site-packages/rich/console.py", line 893, in render
    for render_output in iter_render:
  File "/Users/waynerv/Repos/QData/qdatamgr-cli/venv/lib/python3.8/site-packages/rich/table.py", line 411, in __rich_console__
    widths = self._calculate_column_widths(console, max_width - extra_width)
  File "/Users/waynerv/Repos/QData/qdatamgr-cli/venv/lib/python3.8/site-packages/rich/table.py", line 500, in _calculate_column_widths
    pad_widths = ratio_distribute(_max_width - table_width, widths)
  File "/Users/waynerv/Repos/QData/qdatamgr-cli/venv/lib/python3.8/site-packages/rich/_ratio.py", line 53, in ratio_distribute
    assert total_ratio > 0, "Sum of ratios must be > 0"
AssertionError: Sum of ratios must be > 0

I take response from a API to generate columns and rows dynamically so they could be None.

Is there any way I can properly print a table without columns?

Originally posted by @waynerv in #1043

@willmcgugan willmcgugan added the bug Something isn't working label Feb 22, 2021
@waynerv
Copy link

waynerv commented Feb 22, 2021

It looks like the table width was calculated without taking into account the case of that Table.columns is an empty list. Perhaps the table width should be set to the character width of title if title is presented.

The width calculation process in source code looks a bit complicated, but I can take my effort to fix this if you need.

@willmcgugan
Copy link
Collaborator Author

Actually, what should a table with no column display? You could argue that it should display nothing other than the title.

@waynerv
Copy link

waynerv commented Feb 24, 2021

Are you talking about not rendering the empty box?

I think both are acceptable, as long as there's consistent behaviour.

But for customize I would choose to rendering box, in this way we can decide whether to render box or just title by manually check its columns.

@willmcgugan
Copy link
Collaborator Author

v9.12.0 will display a blank line for tables with no columns, regardless of minimum width. You can test for emptiness with table.columns

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants