IPython:将 Javascript 脚本添加到 IPython 笔记本 [英] IPython: Adding Javascript scripts to IPython notebook
问题描述
作为项目的一部分,我需要在 IPython 模块中嵌入一些 javascript.这就是我想要做的:
As a part of a project, I need to embedd some javascripts inside an IPython module. This is what I want to do:
from IPython.display import display,Javascript
Javascript('echo("sdfds");',lib='/home/student/Gl.js')
我的 Gl.js 看起来像这样
My Gl.js looks like this
function echo(a){
alert(a);
}
有什么方法可以让我在笔记本中嵌入Gl.js"和其他这样的外部脚本,这样我就不必每次尝试执行一些需要那个图书馆.
Is there some way so that I can embed "Gl.js" and other such external scripts inside the notebook, such that I dont have to include them as 'lib' argument everytime I try to execute some Javascript code which requires to that library.
推荐答案
作为一个非常短期的解决方案,您可以使用 IPython display()
和 HTML()
函数将一些 JavaScript 注入页面.
As a very short-term solution, you can make use of the IPython display()
and HTML()
functions to inject some JavaScript into the page.
from IPython.display import display, HTML
js = "<script>alert('Hello World!');</script>"
display(HTML(js))
尽管我不推荐使用官方 custom.js
方法,但有时我确实发现快速测试某些内容或动态生成小的 JavaScript 片段很有用.
Although I do not recommend this over the official custom.js
method, I do sometimes find it useful to quickly test something or to dynamically generate a small JavaScript snippet.
这篇关于IPython:将 Javascript 脚本添加到 IPython 笔记本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!