[IPython-dev] IPython nosetests extension

Matthias BUSSONNIER bussonniermatthias at gmail.com
Tue Dec 18 03:13:10 EST 2012


> 
> I am not quite sure what you mean and how you "peek" and
> sys.displayhook for this.
> 
> For now, I stole a page from https://github.com/catherinedevlin/ipython_doctester with respect to displaying text/plain when using ipython console vs displaying text/html+application/javascript in the notebook. But it feels like a REALLY ugly hack (and requires pyzmq to be installed just for an import to see that we're NOT using the notebook…).
> 
> The particular code is https://github.com/taavi/ipython_nose/blob/master/ipython_nose.py#L293 .

Haven't read the all thread, but, why not having a object that have 
both an _repr_ and and _repr_html_ (supported by qtconsole) 


class Bunch(object):
    
    def __init__(self,value):
        self.value = value
    
    
    def _repr_javascript_(self):
        """only notebook can do
        
        will result in output in JS console **not in notebook itself**
        """
        return 'console.log("'+self.value+'")'
    
    def _repr_html_(self):
        """qtconsole can do too"""
        return 'html:'+self.value
    
    def __repr__(self):
        """ the rest """
        return 'plain:'+self.value
   
from IPython.display import display

def displayer(string):
    display(Bunch(string))

...
displayer('foobar')

-> what you expect depending on the frontend.

My 2 cents.
-- 
Matthias


More information about the IPython-dev mailing list