[IPython-dev] experiment - remote execution of jquery and d3 code into the browser via ipython

Jason Grout jason-sage@creativetrax....
Mon Mar 19 09:51:00 CDT 2012

On 3/19/12 9:32 AM, hugo wrote:
> On 03/19/2012 09:22 AM, fawce wrote:
>> holy mackerel that's awesome!
>> Can you elaborate on the callback issue? Wondering why you can't pass
>> in a callback that just makes a ws call internally.
> just that I serialize function names and the args they are called with
> via JSON
> so for d3, the normal way you would position points in a scatter plot is
> something like this
> d3.selectAll('circle').attr('cx', function(d){return axis(d['x'])});
> I have no good way to pass function(d){return xaxis(d['x'])}
> I could pass it as a string, but then I would need some way to determine
> whether strings I pass should be eval-ed to retrieve callbacks or not
> also - you would have to write javascript callbacks in string form,
> which is ugly anyways. furthermore, in d3, it is common to define
> scaling axes objects, and use those in your callbacks - I don't have a
> good place to do that either.

fawce's suggestion sounds very intriguing--make the websockets message 
passing two-way between python and javascript.  The way I understand the 
suggestion, what about (in python):

def mycallback(d):
     return d3.axis(d['x'])

d3.selectAll('circle').attr('cx', callback(mycallback));

This gets translated to the javascript code:

d3.selectAll('circle'.attr('cx', function(){
var results= send_message('mycallback', arguments);
return interpret_results(results);})

send_message sends a message back through websockets to call the 
mycallback python function with some sort of proxy object d that knows 
how to generate json messages describing the attribute accesses, etc. 
In the python side, inside mycallback, d['x'] generates the javascript 
code to access arguments[0]['x'], so what is passed back is some sort of 
javascript code like 'd3.axis(arguments[0]['x'])'.  interpret_results 
then runs this code and returns the result.



More information about the IPython-dev mailing list