[IPython-dev] Data from js to Python

Kiko kikocorreoso at gmail.com
Sat Sep 26 07:29:01 EDT 2015


2015-09-25 1:19 GMT+02:00 Doug Blank <doug.blank at gmail.com>:

> 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.
>

Great, now I better understand how it works.

Thanks, Doug and others for your time.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20150926/902f6e20/attachment.html>


More information about the IPython-dev mailing list