Can pprint/pretty be temporarily be disabled?
See original GitHub issueDiscussed in https://github.com/willmcgugan/rich/discussions/1603
<div type='discussions-op-text'>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) <ipython-input-6-1efe34cfb034> in <module> ----> 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] https://github.com/friendly-traceback/friendly/issues/14</div>
Issue Analytics
- State:
- Created 2 years ago
- Comments:5
It works perfectly here! So much simpler too.
Did I solve your problem?
Consider sponsoring the ongoing work on Rich and Textual.
Or buy me a coffee to say thanks.
– Will McGugan