Understanding namespace when using Javascript from Ipython.core.display in Jupyter Notebook

拥有回忆 提交于 2019-12-12 20:28:16

问题


I am having trouble understanding the namespace of the Javascript function that is provided in IPython.

from IPython.core.display import display, Javascript
display(Javascript(u"""
var variable = 'Hello'
console.log(variable)
"""))
[output]
Hello

The above seems to work fine, but the following throws an error.

display(Javascript(u"""
var variable = 'Hello'
"""))
display(Javascript(u"""
console.log(variable)
"""))
[output]
Javascript error adding output!
ReferenceError: variable is not defined
See your browser Javascript console for more details.

If I want to save data into a variable in one function, from say an ajax call, and use it from another function in a separate cell later in a jupyter notebook, what is the best practice method for doing this? My current implementation uses the Window to store the variable.

display(Javascript(u"""
var variable = 'Hello'
Window.variable = variable
"""))
display(Javascript(u"""
console.log(Window.variable)
"""))
[output]
Hello

Another related question - even after storing it in Window, I'm unable to access the variable from the javascript console. I'd also like access to it from the javascript console for debugging, but would like to use best practices going forward. Suggestions?

Edit :

The following seems to work fine.

display(HTML(u"""

<script>
variable='Hello'
</script>
"""))

display(HTML(u"""

<script>
console.log(variable)
</script>
"""))

来源:https://stackoverflow.com/questions/40538578/understanding-namespace-when-using-javascript-from-ipython-core-display-in-jupyt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!