Speed of console.print with rich.table.Table
See original GitHub issueI spent some time porting some code which displays tabular data from using the tabulate
library to using rich
. The table has just less than 300 rows, so in most cases we would consider this not a lot of data.
Using cProfile
, I can see the time for the call to a function print_table
has blown out from 0.067s to 1.241s. I get similar numbers on each test.
My questions are: is this expected? And, am I doing something obviously wrong?
Code for reference:
(For the record it’s not the df.iterrows()
call which is slow, the profiling shows venv/lib/python3.9/site-packages/rich/console.py::1545::print
as the culprit.)
With tabulate
:
def print_table(df: pd.DataFrame):
'''Helper to pretty print dataframes'''
click.echo(tabulate(df, headers='keys', tablefmt='psql'))
With rich
:
def print_table(df: pd.DataFrame):
'''Helper to pretty print dataframes'''
table = Table()
# Add the index column header
table.add_column(df.index.name)
# Include all remaining column headers
for col in df.columns.to_list():
table.add_column(col)
# Add rows from the passed DataFrame
for index, row in df.iterrows():
table.add_row(index, *[str(x) for x in row.to_list()])
# Render table
console.print(table)
Issue Analytics
- State:
- Created 2 years ago
- Comments:6
Top Results From Across the Web
rich.console — Rich 12.6.0 documentation
Context manager to capture the result of printing to the console. See capture() for how to use. Parameters. console (Console) – A console...
Read more >[BUG] Printing table into console results in infinite loop #1682
Following code ends up with rich being in infinite loop and never finishing: Platform used: IOS Code to reproduce the issue: from ...
Read more >Sebastián Ramírez on Twitter: "We just create a Rich table ...
We just create a Rich table and print with Rich's console. ... Python is a super-fast language... when you compare it to the...
Read more >rich: Tables - Calmcode
Rich Python Tables. Tables are another nice feature that are nice to have in the terminal! from rich.console import Console from rich.table import...
Read more >Constructing a table with multilevel headers using `rich.table`
from rich.console import Console, Group from rich.columns import Columns ... table.add_row(st, tt) console = Console() console.print(table).
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This issue was evidently heading towards a straight close, but I have to disagree with your logic. A
git show
can render thousands of lines of parsed, coloured text in milliseconds. I don’t need to be a movie character to find this useful.Sure the comparison is not apples to apples, but your dismissal of my report is a little sad.
Did I solve your problem?
Why not buy the devs a coffee to say thanks?