0

I try to pass a javascript variable to python in a jupyter notebook, as described in How to pass variables from javascript to python in Jupyter?

Somehow the IPython.notebook.kernel.execute("foo=97") seems not to work, but IPython itself works.

Here my code from the jupyter notebook

# [1]
from IPython.display import HTML

# [2]
HTML('''<script>console.log('foo=101')</script>''')

# [3]
HTML('''
  <script type="text/javascript">
    IPython.notebook.kernel.execute("foo=97")
  </script>
 ''')

Executing cell [2] after cell [1] shows in the console foo=101. Executing cell [3] shows this error in the console

VM1448:2 Uncaught ReferenceError: IPython is not defined
    at <anonymous>:2:5
    at t.attachWidget (jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2:1594384)
    at t.insertWidget (jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2:1593735)
    at j._insertOutput (jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2:925178)
    at j.onModelChanged (jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2:922355)
    at m (jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2:1560386)
    at Object.l [as emit] (jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2:1560046)
    at e.emit (jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2:1558291)
    at c._onListChanged (jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2:918778)
    at m (jlab_core.e37d4bbc8c984154bc26.js?v=e37d4bbc8c984154bc26:2:1560386)

Any ideas how to solve my problem?

My jupyter setup:

IPython          : 8.6.0
ipykernel        : 6.17.1
ipywidgets       : 8.1.1
jupyter_client   : 7.4.7
jupyter_core     : 5.0.0
jupyter_server   : 1.23.2
jupyterlab       : 3.5.0
nbclient         : 0.7.0
nbconvert        : 7.2.5
nbformat         : 5.7.0
notebook         : 6.5.2
qtconsole        : not installed
traitlets        : 5.5.0
6
  • 1
    Your first link references a post from 2016. Jupyter has evolved quite a bit since then and you aren't telling us what version of anything you are using? More modern tech in Jupyter won't work with those older more 'hacking'-style approaches. See my comments here, and for a very high-level answer, see here where original question was trying to use kernel.execute() like you. What are you trying to do that you thought you could ... Commented Dec 19, 2023 at 18:23
  • <continued> use that approach to pass between Python and Javascript? I ask because there may be a better way to achieve the actual goal. Commented Dec 19, 2023 at 18:24
  • @Wayne I want to drag and drop csv files to my jupyter notebook and work with this csv files in jupyter. My missing part is, to get the csv-string which I have in javascript, available in python. Commented Dec 22, 2023 at 13:23
  • Sounds like a job for ipywidgets.readthedocs.io/en/8.1.2/examples/… Commented Dec 22, 2023 at 13:28
  • Could not find drag and drop, but the file upload seems interessting. Thanks Commented Dec 22, 2023 at 13:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.