Skip to content

[REQUEST] RichHandler: do not waste space for the subsequent log lines #3712

@sam-s

Description

@sam-s

How would you improve Rich?

RichHandler formats messages in columns, thus all the message lines after the 1st one have a huge margin on both sides (left: time and level, right: source file:line) and are wrapped.

I would like the subsequent lines (2nd and ff) to be formatted as is, no colors, no columns, no wrapping.

What problem does it solve for you?

I often print pandas data frames on the subsequent lines, so the width real estate is critical for me: I don't want any wrapping whatsoever.

Here is the code:

def df2str(df:pd.DataFrame) -> str:
    """Pretty string representation of the DataFrame.
    Ensure that no rows or columns are omitted."""
    rows,cols = df.shape
    with pd.option_context("display.width",10000,
                           "display.max_columns",cols,
                           "display.max_rows",rows):
        return str(df)

mylogger.info("this is a wide data frame:\n%s", df2str(pd.DataFrame({f"c{c}":range(5) for c in range(25)})))

without RichHandler I get

2025-04-23 16:46:00 INFO 46432/MainThread/2584917216 this is a wide data frame:
   c0  c1  c2  c3  c4  c5  c6  c7  c8  c9  c10  c11  c12  c13  c14  c15  c16  c17  c18  c19  c20  c21  c22  c23  c24
0   0   0   0   0   0   0   0   0   0   0    0    0    0    0    0    0    0    0    0    0    0    0    0    0    0
1   1   1   1   1   1   1   1   1   1   1    1    1    1    1    1    1    1    1    1    1    1    1    1    1    1
2   2   2   2   2   2   2   2   2   2   2    2    2    2    2    2    2    2    2    2    2    2    2    2    2    2
3   3   3   3   3   3   3   3   3   3   3    3    3    3    3    3    3    3    3    3    3    3    3    3    3    3
4   4   4   4   4   4   4   4   4   4   4    4    4    4    4    4    4    4    4    4    4    4    4    4    4    4

with RichHandler I get

[04/23/25 16:46:00] INFO     this is a wide data frame:                                            [2584917216.py](file:///C:/msys64/tmp/ipykernel_46432/2584917216.py):[10](file:///C:/msys64/tmp/ipykernel_46432/2584917216.py#10)
                                c0  c1  c2  c3  c4  c5  c6  c7  c8  c9  c10  c11  c12  c13  c14                    
                             c15  c16  c17  c18  c19  c20  c21  c22  c23  c24                                      
                             0   0   0   0   0   0   0   0   0   0   0    0    0    0    0    0                    
                             0    0    0    0    0    0    0    0    0    0                                        
                             1   1   1   1   1   1   1   1   1   1   1    1    1    1    1    1                    
                             1    1    1    1    1    1    1    1    1    1                                        
                             2   2   2   2   2   2   2   2   2   2   2    2    2    2    2    2                    
                             2    2    2    2    2    2    2    2    2    2                                        
                             3   3   3   3   3   3   3   3   3   3   3    3    3    3    3    3                    
                             3    3    3    3    3    3    3    3    3    3                                        
                             4   4   4   4   4   4   4   4   4   4   4    4    4    4    4    4                    
                             4    4    4    4    4    4    4    4    4    4

Image

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions