我最近玩了几天IPython笔记本,我非常喜欢它!但是,现在我需要做一些稍微高级的事情:
我有一个Markdown单元格;其中包含一个HTML输入和按钮,以及附加到按钮的一些JavaScript,将输入内容传递给python内核。以下是该单元格:
<h3>Use JS to pass DOM info to Python kernel: </h3>
<input id='testinput' value='FragmentId'></input>
<button id='testComms' onclick='JS2PY()'>:D</button>
<script type="text/javascript">
function JS2PY(){
var input = document.getElementById('testinput').value,
kernel = IPython.notebook.kernel;
kernel.execute('testVar = "' + input + '"');
}
</script>
使用得心应手!接下来,我有一个Python代码单元格;它执行一些ROOT操作,并基于从上面的单元格中注入到Python内核的任何值生成图表。这是Python单元格:
def testfunc():
from ROOT import TH1, TFile, TTree
import rootnotes, numpy
c2 = rootnotes.canvas("treeData", (600,400))
testfile = TFile("fragment27422_000.root")
testtree = testfile.Get("FragmentTree")
buff = numpy.zeros(1, dtype=float)
testtree.Branch(testVar, buff)
testtree.Draw(testVar)
return c2
testfunc()
如果我手动运行单元格,也没有问题 - 太好了! 但是我真正想要的是,当我在上面的markdown单元格中点击那个按钮后,这个python单元格会自动运行,并推广testVar
变量。抱歉并提前感谢 - 这只是我学习Python的第二天,所以可能非常简单。