[IPython-dev] Data from js to Python

Doug Blank doug.blank at gmail.com
Thu Sep 24 19:19:18 EDT 2015


On Thu, Sep 24, 2015 at 6:34 PM, Kiko <kikocorreoso at gmail.com> wrote:

>
>
> 2015-09-24 23:59 GMT+02:00 Doug Blank <doug.blank at gmail.com>:
>
>> It isn't too complicated, if you can format your JavaScript in the kernel
>> language; here is JavaScript talking to Python:
>>
>> %%javascript
>> var javascript_var = 2 * 4 + 8;
>> IPython.notebook.kernel.execute("python_var = " + javascript_var);
>>
>> Hope that helps.
>>
>
> Hi Doug,
>
> It helps but it is not what I want.
>
> I want to access a object stored in the global js namespace of the
> browser. For instance, this is available in your js console on the browser
> when you work with IPython/Jupyter:
> *IPython.notebook.notebook_name*
>
> But if I do:
>
> *%%javascriptIPython.notebook.kernel.execute("python_var = " +
> IPython.notebook.notebook_name);*
>
> It doesn't work.
>

You have to construct valid Python with Javascript:

%%javascript
IPython.notebook.kernel.execute("python_var = '" +
IPython.notebook.notebook_name + "'");

(putting string in quotes). The suggestions to move from Javascript to
Python via JSON is a good one.


> In your example, where is *javascript_var* stored?
>

You can hang it off of window, or document.

-Doug




>
>
>> -Doug
>>
>>
>> On Thu, Sep 24, 2015 at 5:44 PM, Kiko <kikocorreoso at gmail.com> wrote:
>>
>>>
>>>
>>> 2015-09-24 23:41 GMT+02:00 Wes Turner <wes.turner at gmail.com>:
>>>
>>>>
>>>> On Sep 24, 2015 4:32 PM, "Kiko" <kikocorreoso at gmail.com> wrote:
>>>> >
>>>> > 2015-09-24 23:25 GMT+02:00 Wes Turner <wes.turner at gmail.com>:
>>>> >>
>>>> >> https://www.google.com/search?q=ipython+d3
>>>> >>
>>>> >> Vega visualization grammar
>>>> >> | Docs: https://github.com/vega/vega/wiki/Vega-and-D3
>>>> >> | Homepage: https://vega.github.io/
>>>> >>
>>>> >> mpld3: matplotlib + d3
>>>> >> | Homepage: http://mpld3.github.io/
>>>> >>
>>>> >> IPython widgets (interactive)
>>>> >> | Docs: https://github.com/ipython/ipython/wiki/widgets
>>>> >>
>>>> >> pythreejs IPython Notebook extension
>>>> >> | Src:
>>>> https://github.com/jovyan/pythreejs/blob/master/pythreejs/pythreejs.py
>>>> three.js
>>>> >> | Src:
>>>> https://github.com/jovyan/pythreejs/blob/master/pythreejs/nbextension/pythreejs.js
>>>> >>
>>>> >> * [ ] JSON-LD
>>>> >>    * http://json-ld.org/playground/
>>>> >
>>>> > Thanks.
>>>> >
>>>> > Yes, I think all of these allow you to send information from Python
>>>> to js but I'm talking about the opposite way. For instance, select an area
>>>> in an openlayers map and then get the coordinates of the area back to
>>>> Python.
>>>>
>>>> IIUC, that's more or less how widget controls operate.
>>>>
>>>> also, here's this:
>>>>
>>>> ipython-jsobject
>>>> Src: https://github.com/jdfreder/ipython-jsobject
>>>>
>>>>     JSON-LD (and/or e.g. CSVW) with RDFJS could be helpful:
>>>> https://github.com/rdfjs/rdfjs.org
>>>>      * https://github.com/rdfjs/rdfjs.org/wiki/Projects #LinkedData
>>>>      *
>>>> http://www.w3.org/community/rdfjs/wiki/Comparison_of_RDFJS_libraries
>>>>        * [ ] TODO: link these resources
>>>>
>>> Thanks, I will review that to check if I can obtain any hints.
>>>
>>>
>>>>
>>>>
>> _______________________________________________
>> IPython-dev mailing list
>> IPython-dev at scipy.org
>> https://mail.scipy.org/mailman/listinfo/ipython-dev
>>
>>
>
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> https://mail.scipy.org/mailman/listinfo/ipython-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20150924/d6488668/attachment.html>


More information about the IPython-dev mailing list