Understanding namespace when using Javascript from

2019-07-18 23:51发布

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>
"""))

0条回答
登录 后发表回答