[IPython-User] question about frontend-kernel interaction

Matthias BUSSONNIER bussonniermatthias@gmail....
Tue Jul 10 02:46:37 CDT 2012


Le 10 juil. 2012 à 09:03, Zoltán Vörös a écrit :

> Hi Matthias,
> 
> Thanks for the comments! Here are mine.
> 
> On 07/10/2012 08:38 AM, Matthias BUSSONNIER wrote:
>>> 
>>> kernel.execute("pyfunc();", {'output': $.proxy(this.output_area.handle_output, this.output_area)});
>>> 
>>> but I don't know how to get 'this'.  I think, tying the output to the output handler would be fine with me.
>> Either you trust IPython.notebook.selected_cell (or smith like that) at each call.
>> You can take a more object oriented version of your script and store the value
>> of IPython.notebook.selected_cell when the script is inserted.
>> Or, as ou insert the script through python, try to find yourself, then the containing `codecell`.
>> Something like inserting before the script a<div>  with a known random id, `$('myid')`the get get the correct nth parent.
> OK, I will try to experiment with that. This sounds much less trouble than creating a custom widget for the purpose.
> 
>>> By the way, the problem is not related to figures. This example does not display anything, while the code is actually executed. This I know from printing out the value of 'a'.
>> Yes, whatever you try to display, if you don't bound a request to a handler the fronted has no way of knowing `where the hell should I print this message I got from the kernel ?`
> Very nicely put:)  What really confuses me, however, is the fact that, from Animation_and_Progress.ipynb, this works
> 
> for n in range(1,10):
>    time.sleep(1)
>    ax.plot(x, jn(x,n))
>    clear_output()
>    display(f)

The difference is here that everything is executed in the kernel, and the codecell take care of binding the code it send to the kernel to its own _handle_output. Then, every call in the kernel to clear_output and display(f) are known to be owned by this cell. 
 the codecell does (using half js/half python syntax ):

IPython.kernel.execute(
"""
for n in range(1,10):
   time.sleep(1)
   ax.plot(x, jn(x,n))
   clear_output()
   display(f)
""", 
callbacks = {		
	stdout: self.stdout_handler,		
	stderr: self.stderr_handler		
	…		
 }
)


The second JS progress bar generate a string with a random uuid `divid = str(uuid.uuid4())` and then the progress bar can be updated by any js, displayed anywhere as long as you know the id of the div : 
"$('div#%s').width('%i%%')" % (divid, i)

Which can read : select the dis which id is `divid` and apply a width of `i%` to it.

If you look at the source code, there is actually 102 (1+101) div appended (one every 0.1 sec), with all but the first one that are "display:none".


> 
> while if I try to do the very same by connecting a button to kernel.execute directly, what happens is not what one would expect. (In that case, the plots will be displayed after the next command in a code cell was entered manually.)

That is strange, and might be a bug.
Brian does know much more on how JS side works, so we might want to ask him. 
You can try to open an issue about it on github, but we'll all be pretty low on bandwidth for the next 2 weeks,
That is to say until after SciPy.

-- 
Matthias
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/ipython-user/attachments/20120710/be2c7347/attachment-0001.html 


More information about the IPython-User mailing list