[IPython-dev] Detecting ipython notebook on the browser

Matthias BUSSONNIER bussonniermatthias at gmail.com
Fri May 23 07:36:23 EDT 2014

Hi Tiago, 

Le 23 mai 2014 à 13:19, Tiago Antao a écrit :

> Hi,
> The way things work now is by overriding abjad's show function: instead
> of generating a postscript file and firing a postscript viewer I
> generate a PNG file and send it (after some cropping) to ipython
> notebook. Instead of overriding the show function, the function itself
> could decide what to do based on context...
> So, what I needed to know is:
> 1. How to detect that ipython notebook is running?
> 2. A bit more subtle than that: Can I detect that it is running in a
> browser context? I mean: we can run a notebook from the command line
> (with the %run magic) and in that case I might still want to generate a
> PS instead of a PNG file…

This is a question which is often asked, you will find answers on 
stack-overflow and archive of the mailing list. 

There are non way to know that a kernel is run from a notebook, 
and actually the question does not make sense, to retake example 
i have already use, it's like a book authors wondering the eye color of 
its reader. 

The way to do it is to have your score object : 

 score_template = templatetools.TwoStaffPianoScoreTemplate()
 score = score_template()

have a _repr_png_ that return the PNG data. 

IPython will take care of the rest and call _repr_png_ if present and display the PNG.

basically rename your _get_png(expr) as _repr_png_ and make it a method of score. 
though it should return only one PNG. 

Hope that helps. 


> Thanks,
> Tiago
> PS - I am interested in extending the notebook metaphor to
> creative/artistic stuff. If you have an ongoing project/idea on this,
> and need some volunteer hands, get in touch...
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev

More information about the IPython-dev mailing list