-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Description
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.