在Jupyter中将变量从javascript传递给python

在Jupyter中将变量从javascript传递给python

本文介绍了如何在Jupyter中将变量从javascript传递给python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,我应该可以在下面的代码段中打印变量 foo

 来自IPython.display导入HTML 
HTML('''
< script type =text / javascript>
IPython.notebook.kernel.execute(foo = 97)
< / script>
''')
print(foo)

相反,我看到此错误消息:

  NameErrorTraceback(最近一次调用最后一次)
< ipython-input-2-91b73ee49ec6> in< module>()
5< / script>
6''')
----> 7 print(foo)

NameError:名称'foo'未定义

我正在尝试使用



甚至更简单:




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?

解决方案

This is how I made your code work:

or even simpler:

这篇关于如何在Jupyter中将变量从javascript传递给python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 22:22