[IPython-User] question about frontend-kernel interaction

Matthias BUSSONNIER bussonniermatthias@gmail....
Mon Jul 9 15:46:28 CDT 2012


Le 8 juil. 2012 à 08:43, Zoltán Vörös a écrit :

> Hi all,
> 
> I would like to ask how the display function in the html notebook works. 
> I wanted to add some interactivity to plots. Here is a minimal example, 
> which should produce random plots on a button press in the notebook.
> 
> from IPython.core.display import display, HTML,  clear_output
> 
> html = """<script type="text/javascript">
> function jsfunction () {
>     IPython.notebook.kernel.execute("pyfunc()");

Kernel.execute take a second argument that is a dict of callback (see IPython/frontend/html/notebook/static/js/codecell.js L161)
If you don't specify how or by what the respond should be handled, the frontend will ignore the messages. 

Do you can either bound it directly to the current cell output handler, or, deal with it in a custom widget. 

> }
> </script>
> <input type="button" value="Replot" onclick="jsfunction()" />
> """
> 
> display(HTML(html))
> 
> f, ax = plt.subplots()
> f.hold(False)
> 
> def pyfunc():
> #    clear_output()
> #    display(HTML(js))
>     f.clear()
>     plot(rand(100))
>     show()
> 
> If I call %pylab, this snippet populates the plot in a popup window, and 
> it does what I expect.

I suppose you run the server and the browser on same side.
So the window is triggers on the machine that runs the kernel, 
then in this case there is no need to know which frontend
sent the message to have something displayed. 
Which make look likes it works correctly.

> However, when I call %pylab inline (in this case, 
> the two commented-out lines are part of the code), the plot is never 
> updated,


> and it seems to me that subsequent plots are never loaded to 
> the notebook.

This is strange, I don't think it's an expected behavior. Do you have any output in the JS console ?
The fact that kernel.execute didn't get any callback might screw up something somewhere.

> Could someone point out what is missing, or whether what I 
> want to do is doable at all?

I goes the answer is that it's doable :-)
keep us posted, 

Cheers,
-- 
Matthias

> 
> Cheers,
> Zoltán
> 
> _______________________________________________
> IPython-User mailing list
> IPython-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-user



More information about the IPython-User mailing list