所以,我一直在玩IPython笔记本几天,我喜欢它!但是,现在我需要做一些有点花哨的事情:
我有一个降价单元; 在其中,有一个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>
Run Code Online (Sandbox Code Playgroud)
奇迹般有效!接下来我有一个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()
Run Code Online (Sandbox Code Playgroud)
如果我手动去运行单元格也没问题 - 太棒了!但是我真正想要的是,在推广testVar变量后,当我点击上面的降价单元格中的那个按钮时,这个python单元会自动运行.抱歉并提前感谢 - 这对我来说只是python的第二天,所以它可能非常简单.
我花了一天时间讨论一个有趣的问题 - 我有一些画布,我想使用原生的HTML5拖放功能来拖延.一切都很好,除了我最终发现在Chrome 28.0.1500.95中,如果画布是内联块div的子画面,则不会出现画布的默认重影图像.看看这个最小的工作示例:
<html>
<body>
<div style='display:inline-block'>
<canvas id='canvas1' width='100px' height='100px' draggable='true'></canvas>
</div>
<div>
<canvas id='canvas2' width='100px' height='100px' draggable='true'></canvas>
</div>
<script type="text/JavaScript">
var canvas1, canvas2, context1, context2;
canvas1 = document.getElementById('canvas1');
context1 = canvas1.getContext('2d');
canvas2 = document.getElementById('canvas2');
context2 = canvas2.getContext('2d');
context1.fillText('no drag', 10, 30);
context2.fillText('yes drag', 10, 30);
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
当我尝试拖动"是拖动"时会出现重影图像,但不会显示"无拖动".但是,如果我也坚持放下目标,我可以通过按照正常情况放下"无拖拽"来触发它,尽管没有幽灵.我想知道为什么幽灵图像显然会根据父母的CSS消失,或者如果这里还有其他东西 - 提前感谢!
所以 - 我正在开发一个 Heroku + Node + Express + MongoDB 应用程序,我整天都在尝试找出如何获得我想要的 mongo 查询。在 mongo 集合中,每个文档都有一个属性“fruits”,它是一个字符串数组,例如
fruits : ['apple', 'orange', 'lemon']
Run Code Online (Sandbox Code Playgroud)
水果的大小和内容是任意的。我想在数据库中查询所有文档,其水果数组至少有一个元素与我在查询时提供的另一个数组有共同点。我怎样才能实现这个目标?
到目前为止我所尝试的主要是 $where 查询和一些服务器端 JS。如果我做类似的事情
mongo.Db.connect(mongoUri, function(err, db) {
db.collection('Users', function(er, collection) {
collection.find( {$where: 'false' } ).toArray(function(err, matches){
console.log(matches)
...
});
});
});
Run Code Online (Sandbox Code Playgroud)
我得到一个空数组 - 很好!如果我将 false 更改为 true,我将获得整个数据库 - 也很好!如果出现以下情况,情况会变得不太好:
collection.find( {$where: function(){return false} } ).toArray(function(err, matches){...
Run Code Online (Sandbox Code Playgroud)
我又得到了整个数据库!我认为这显然会返回与上面给出的第一个示例相同的空数组,按照http://docs.mongodb.org/manual/reference/operator/query/where/中提供的语法
我尝试上述只是为了看看我是否可以得到任何与 $where 一起工作的东西,但是当我尝试将服务器端 JS 添加到组合中时,会出现更多问题。我查看了 http://docs.mongodb.org/manual/tutorial/store-javascript-function-on-server/
并将以下内容添加到我的数据库中的 system.js 集合中:
{
"_id": "isMatch",
"value": "function(){return false}"
}
Run Code Online (Sandbox Code Playgroud)
并用以下行修改了上面的示例 …
所以 - ROOT社区中的优秀人员产生了以下魔力:
# This is for intercepting the output of ROOT
# In a cell, put %%rootprint so that the output that would normally be
# sent directly to the stdout will instead be displayed in the cell.
# It must be the first element in the cell.
import tempfile
import ROOT
from IPython.core.magic import (Magics, magics_class, cell_magic)
@magics_class
class RootMagics(Magics):
"""Magics related to Root.
%%rootprint - Capture Root stdout output and show in result cell
"""
def __init__(self, …Run Code Online (Sandbox Code Playgroud)