Is it possible to change the pretty printer that IPython uses?
I'd like to switch out the default pretty printer for pprint++
, which I prefer for things like nested structures:
In [42]: {"foo": [{"bar": 42}, {"bar": 16}] * 3, "bar": [1,2,3,4,5]}
Out[42]:
{'bar': [1, 2, 3, 4, 5],
'foo': [{'bar': 42},
{'bar': 16},
{'bar': 42},
{'bar': 16},
{'bar': 42},
{'bar': 16}]}
In [43]: pprintpp.pprint({"foo": [{"bar": 42}, {"bar": 16}] * 5, "bar": [1,2,3,4,5]})
{
'bar': [1, 2, 3, 4, 5],
'foo': [
{'bar': 42},
{'bar': 16},
{'bar': 42},
{'bar': 16},
{'bar': 42},
{'bar': 16},
{'bar': 42},
{'bar': 16},
{'bar': 42},
{'bar': 16},
],
}
This can technically be done by monkey-patching the class
IPython.lib.pretty.RepresentationPrinter
used here in IPython.This is how one might do it:
This is a bad idea for a multitude of reasons, but should technically work for now. Currently it doesn't seem there's an official, supported way to override all pretty-printing in IPython, at least simply.
(note: the
.rstrip()
is needed because IPython doesn't expect a trailing newline on the result)