[IPython-User] How to configure/control line width?

Robert Kern robert.kern@gmail....
Fri Mar 11 18:03:58 CST 2011


On 3/11/11 4:12 PM, Jorge Scandaliaris wrote:
> Robert Kern<robert.kern<at>  gmail.com>  writes:
>
> <snip>
>>
>> See np.set_printoptions(). In the 0.11 branch of development, we have a
>> configurable printing system such that you could replace the printer for
>> nnumpy.ndarray instances. However, you will have to ask the terminal for how
>> wide it is, and this information is platform-specific, though not very
>> difficult to obtain. You can see how I do this in my personal IPython
>> extensions:
>>
>> https://bitbucket.org/robertkern/kernmagic/src/tip/kernmagic/utils.py
>>
>
> Thanks, that's what I was looking for. I am using the 0.11 branch, is the
> configurable printing system documented? I'll have a closer look at your
> extensions, maybe I can use it as inspiration for my own needs.

I don't think it's documented outside of the code, but here is how you would 
configure it in your ipython_config.py file:

# Get the config being loaded so we can set attributes on it
c = get_config()


def ndarray_pprinter(obj, p, cycle):
     if cycle:
         return p.text('array(...)')
     import numpy as np
     from kernmagic.utils import terminal_size
     width, height = terminal_size()
     old = np.get_printoptions()
     np.set_printoptions(linewidth=width-1)
     # Note that you could add whichever other options suit your taste, too.
     try:
         text = repr(obj)
     finally:
         np.set_printoptions(**old)
     p.text(text)

c.PlainTextFormatter.pprint = True
c.PlainTextFormatter.deferred_printers = {
     ('numpy', 'ndarray'): ndarray_pprinter,
}


-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco



More information about the IPython-User mailing list