Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass variables from javascript to python in Jupyter?

As I understand it, I should be able to print the variable foo in the snippet below.

from IPython.display import HTML
HTML('''
    <script type="text/javascript">
        IPython.notebook.kernel.execute("foo=97")
    </script>
     ''')
print(foo)

Instead, I see this error message:

NameErrorTraceback (most recent call last)
<ipython-input-2-91b73ee49ec6> in <module>()
      5     </script>
      6      ''')
----> 7 print(foo)

NameError: name 'foo' is not defined

I'm trying to use this answer but struggling to make it work.

FWIW, this is the latest Jupyter code (according to pip) running on Fedora 23. What are the prerequisites to make this work?

like image 353
John Schmitt Avatar asked Apr 17 '26 21:04

John Schmitt


1 Answers

This is how I made your code work: enter image description here

or even simpler:

enter image description here

like image 102
DevLounge Avatar answered Apr 20 '26 14:04

DevLounge