[IPython-dev] wxPython console

Emmanuel Bacry emmanuel.bacry@polytechnique...
Mon Nov 7 08:51:37 CST 2011


On 3 nov. 2011 à 15:27, Thomas Kluyver wrote :


> In Python, there are two kinds of output: firstly from "print x", for which you can simply redirect stdout - replace sys.stdout with an object with a .write() method, which will capture strings written to it and display them in your console.
> 
This one is "easy", and it works

> Secondly, if you simply enter "x" at a prompt, IPython displays the value of x at an "Out [n]:" prompt. In such cases, Python calls sys.displayhook. This is called with the object itself, rather than its repr, so you can produce more complex displays. This is how we pretty-print datastructures, and also how the notebook can display rich objects like youtube videos. Have a look at IPython.core.displayhook to see our implementation of this. If you replace the displayhook attribute of an InteractiveShell object, it will take care of using that displayhook when you call run_cell.

I have some problem using this last advice for redirection. Let me explain.
If I override sys.stdout before creating the InteractiveShell, then it fails :

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in init_io(self)
    568             io.stdout = io.stderr = io.IOStream(self.readline._outputfile)
    569         else:
--> 570             io.stdout = io.IOStream(sys.stdout)
    571             io.stderr = io.IOStream(sys.stderr)
    572 

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/utils/io.pyc in __init__(self, stream, fallback)
     30                 stream = fallback
     31             else:
---> 32                 raise ValueError("fallback required, but not specified")
     33         self.stream = stream
     34         self._swrite = stream.write

ValueError: fallback required, but not specified

If I override sys.stdout after creating the Interactive Shell (sharing or not the sys variable with the interactive shell I am creating), it does not crash but the display_hook does not use my newly defined sys.stdout and keeps the old one...

===============

From what I understand, it looks like when the InteractiveShell is created, the sys.stdout is used to build an IOStream object (via io.stdout = io.IOStream(sys.stdout)) that will be used by the display_hook of the interactiveShell. ?
Is that right?

How do I redirect the printing of the default display_hook to my new sys.stdout (I don't want to go into implementing a new display_hook) ?

Thank you
Emmanuel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/ipython-dev/attachments/20111107/07f278bf/attachment.html 


More information about the IPython-dev mailing list