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.

[REQUEST] rich Pretty: truncate long variables

See original GitHub issue

How would you improve Rich?

Hi,

I’m using Pretty to style the locals panels in pyinspect’s tracebacks. However when the object being printed is a list, or a dictionary whose values are long lists/arrays Pretty prints the whole thing, cluttering the console and defeating the purpose of trying to have more informative tracebacks.

I don’t if I’ve missed this option, but it would be great if there was a way to truncate long objects when printing them. (I’ve tried using the overflow argument but didn’t make a difference.

Here’s some code to reproduce the issue:


from rich.pretty import Pretty
from rich.console import Console

my_list = [1] * 400

Console().print(Pretty(my_list))

Ideally it would only print the start of the list.

Thank you, Federico

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14

github_iconTop GitHub Comments

2reactions
willmcgugancommented, Oct 5, 2020

The simplest approach may be to set a limit of n items to all containers however they are nested. Then show ‘n’ items plus an ellipsis (perhaps with text like ... +23

@fakhrip raises a good point about long strings, and I like the solution.

It’s never going to be perfect, even with the above, you could have a very large pretty printed data-structure. But would work for the most part.

I think the Pretty class would have to grow “max_items” and “max_string_size” arguments, which should be optional.

Note that there is a ellipsis unicode character, so … can be replaced with …

You can see the difference with a fixed width font.

...
…

@fakhrip This would be a tricky change! If you are still interested, have a look at pretty.py

1reaction
hhoeflincommented, Nov 27, 2020

Thank you - I have been looking for this abbreviation option. I saw it in pretty, but could not find how to enable it in the logging Handler. Is that possible as well? Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

[REQUEST] rich Pretty: truncate long variables #318 - GitHub
My idea on this is, we may be able to truncate the variable by adding a function which take 2 optional parameters, and...
Read more >
Pretty Printing — Rich 12.6.0 documentation
Rich can truncate containers and long strings to give you an overview without swamping your terminal. If you set the max_length argument to...
Read more >
Gitlab CI: Truncate variable names in the environment section
Currently I am restricting the length of a branchname in order for my review apps to work. I have seen syntax like: ${CI_COMMIT_REF_SLUG:0:20}....
Read more >
Creating rich message layouts - Slack API
Any content displayed within attachments may be wrapped, truncated, or hidden behind a "show more" style option by Slack clients. This isn't the...
Read more >
Solved: POST request data is getting truncated and error C...
I appreciate your endpoint might be expecting a certain length value so youll probably get a 400 or 422 or some other client...
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