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

Can pprint/pretty be temporarily be disabled? #1646

Closed
willmcgugan opened this issue Nov 6, 2021 Discussed in #1603 · 5 comments · Fixed by #1647
Closed

Can pprint/pretty be temporarily be disabled? #1646

willmcgugan opened this issue Nov 6, 2021 Discussed in #1603 · 5 comments · Fixed by #1647

Comments

@willmcgugan
Copy link
Collaborator

Discussed in #1603

Originally posted by aroberge October 16, 2021
This question came as the result of an issue [1] filed in one of my projects.

When using IPython (on its own), it is possible to toggle on and off the pretty printing. For example, doing

dir(__builtins__)
In [1]: dir(__builtins__)
Out[1]:
['ArithmeticError',
 'AssertionError',
 'AttributeError',
 'BaseException',
 'BlockingIOError',
 'BrokenPipeError',
 'BufferError',
 'BytesWarning',
 'ChildProcessError',
 'ConnectionAbortedError',
 'ConnectionError',
...

outputs names one at a time on a line. However, after turning off the pretty printing with the magic command %pprint, the display is shown without pretty formatting:

In [2]: %pprint
Pretty printing has been turned OFF

In [3]: dir(__builtins__)
Out[3]: ['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError', ...

When using Rich with IPython, attempting to use the magic toggle %pprint results in a traceback.
In [4]: from rich import pretty

In [5]: pretty.install()

In [6]: %pprint

AttributeError Traceback (most recent call last)
in
----> 1 get_ipython().run_line_magic('pprint', '')
306 """Toggle pretty printing on/off."""
307 ptformatter = self.shell.display_formatter.formatters['text/plain']
--> 308 ptformatter.pprint = bool(1 - ptformatter.pprint)
309 print('Pretty printing has been turned',
310 ['OFF','ON'][ptformatter.pprint])

AttributeError: 'BaseFormatter' object has no attribute 'pprint'
...

Looking at the source code for Rich, this is not surprising since I can see that it monkeypatches IPython's code, substituting its own pretty printer.

I am wondering if there is a way to (temporarily) disable Rich's pretty printer once it is installed.

[1] friendly-traceback/friendly#14

@willmcgugan
Copy link
Collaborator Author

@aroberge any thoughts on #1647 ?

@aroberge
Copy link

aroberge commented Nov 6, 2021

@willmcgugan It almost looks too simple! :-D

I would have to try and experiment with it ... I'll see if I can do this right away.

@aroberge
Copy link

aroberge commented Nov 6, 2021

It works perfectly here! So much simpler too.

@aroberge
Copy link

aroberge commented Nov 6, 2021

I have tested it with iPython in a terminal as well as in a Jupyter notebook and it works flawlessly.

@github-actions
Copy link

github-actions bot commented Nov 6, 2021

Did I solve your problem?

Consider sponsoring the ongoing work on Rich and Textual.

Or buy me a coffee to say thanks.

Will McGugan

netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this issue Nov 14, 2021
10.13.0

Added

Added json.dumps parameters to print_json Textualize/rich#1638

Fixed

Fixed an edge case bug when console module try to detect if they are in a tty at the end of a pytest run
Fixed a bug where logging handler raises an exception when running with pythonw (related to https://bugs.python.org/issue13807)
Fixed issue with TERM env vars that have more than one hyphen Textualize/rich#1640
Fixed missing new line after progress bar when terminal is not interactive Textualize/rich#1606
Fixed exception in IPython when disabling pprint with %pprint Textualize/rich#1646
Fixed issue where values longer than the console width produced invalid JSON Textualize/rich#1653
Fixes trailing comma when pretty printing dataclass with last field repr=False Textualize/rich#1599

Changed

Markdown codeblocks now word-wrap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants