question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Speed of console.print with rich.table.Table

See original GitHub issue

I 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:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
mafrosiscommented, Jan 16, 2022

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.

1reaction
github-actions[bot]commented, Jan 16, 2022

Did I solve your problem?

Why not buy the devs a coffee to say thanks?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found