kernel execution being overridden

172 views
Skip to first unread message

marq h

unread,
Aug 3, 2016, 1:38:51 PM8/3/16
to Project Jupyter
Hi Jupyter folks

I am puzzled by the behaviour in this case:

Cell 1
import IPython
javascript
= """
    var url = window.location;

        var command = "
url" + " = '" + url + "'";

        var kernel = IPython.notebook.kernel;

       
        kernel.execute(command) ;
"""

IPython.display.display(IPython.display.Javascript(javascript))



Cell 2
print(url)


If I run cell 1, then run cell 2, by hand, one after the other, then all is fine: the url is printed.  If i 'run all' then the "name 'url' is not defined" (this is the case if i rerun cell 2, after having run all)

It's like that kernel.execute is being trampled on by the print and the url variable is never properly assigned.

I've tried time.sleep and other work around steps but to no avail, if i run all, then the kernel.execute doesn't make a result.  Any python code following cell 1 seems to trigger this problem

any advice on how to deal with this would be very gratefully received
cheers
mark

Matthias Bussonnier

unread,
Aug 3, 2016, 1:57:35 PM8/3/16
to jup...@googlegroups.com
These is no way to go around that, the messages and execution requests
are queued as the ZMQ layer.
So you submit cell 2 before `kernel.execute(command) ;` to be back on the bage.

So execute all will always have this behavior.

--
M
> --
> You received this message because you are subscribed to the Google Groups
> "Project Jupyter" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to jupyter+u...@googlegroups.com.
> To post to this group, send email to jup...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/jupyter/473bf13c-13a9-46cc-8ed1-8f49170d3506%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Thomas Kluyver

unread,
Aug 3, 2016, 5:05:48 PM8/3/16
to Project Jupyter
One way that you can sometimes get round this is to define a function in Python first, and then call it from JS, rather than setting a variable from JS.

marq h

unread,
Aug 5, 2016, 11:16:27 AM8/5/16
to Project Jupyter
Thank you for the feedback.  I have found a potentially working approach, which I thought to share

I can create a cell, which creates a javascript button to run all cells, and I can include my kernel call in this cell, so

%%HTML
<button id="do_run_all">Click Here to Explore your Data</button>
<script>
$("#do_run_all").click(
    function () {

        var url = window.location;
        var command = "url" + " = '" + url + "'";
        var kernel = IPython.notebook.kernel;
        kernel.execute(command) ;
        $("#run_all_cells").click();
    });
</
script>

This enables me to provide a 'run all cells' button, which captures the url for the page using javascript and pushes this into a python kernel name.

As this is run before the 'run all cells' call is executed, there appears not to be interference causing the kernel.execute to be missed.

I think this works, I'd be interested in feedback on the approach and concerns it may raise

many thanks
mark

Matthias Bussonnier

unread,
Aug 5, 2016, 1:04:51 PM8/5/16
to jup...@googlegroups.com
At that point you might want to just create a notebook extension that
adds a button to the toolbar.

And keep in mind that this will work only when using the live notebook.

--
M
> --
> You received this message because you are subscribed to the Google Groups
> "Project Jupyter" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to jupyter+u...@googlegroups.com.
> To post to this group, send email to jup...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/jupyter/747969dd-2a5b-43a6-bf59-bf32a2bb498e%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages