Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从JavaScript调用Brython函数_Javascript_Python 3.x_Brython - Fatal编程技术网

从JavaScript调用Brython函数

从JavaScript调用Brython函数,javascript,python-3.x,brython,Javascript,Python 3.x,Brython,编辑: 我在和你玩 我试图弄清楚如何从JavaScript执行Brython代码 已经一年了,所以我想你很久以前就解决了这个问题,但是fwiw你的方法原则上是正确的,我也这么做,当你像使用window.js_func=py_func那样映射它时,它就会起作用。以下是我目前在chromium/firefox上使用的代码中的一些相关信息: <!-- Bottom of HTML.. --> <script type="text/javascript"> var jv_func

编辑:

我在和你玩

我试图弄清楚如何从JavaScript执行Brython代码


已经一年了,所以我想你很久以前就解决了这个问题,但是fwiw你的方法原则上是正确的,我也这么做,当你像使用window.js_func=py_func那样映射它时,它就会起作用。以下是我目前在chromium/firefox上使用的代码中的一些相关信息:

<!-- Bottom of HTML.. -->
<script type="text/javascript">
var jv_funcs = {
    openfile:function(iptElement, fileType){
        // some file opening code then a call back to python
        open_file(reader.result, fileType);
    }
};
</script>
<script type="text/python3" src="src/pythonscript.py"></script>


# separate python script in my case 'pythonscript.py'
def open_file(fileContent, fileType):
    # show the file in the dom..

def open_clicked():
    window.jv_funcs.openfile(document["idOpen"], "fileType")


window.open_file = open_file
document["btnOpen"].bind("click", open_clicked)

var jv_funcs={
openfile:函数(iptElement、fileType){
//一些文件打开代码,然后调用python
打开_文件(reader.result,fileType);
}
};
#在我的例子“pythonscript.py”中使用单独的python脚本
def open_文件(文件内容、文件类型):
#在dom中显示文件。。
def open_clicked():
window.jv_funcs.openfile(文档[“idOpen”],“文件类型”)
window.open\u file=打开\u文件
文档[“btnOpen”]。绑定(“单击”,打开并单击)
需要知道的是,如果您使用window.console.log(event),您会将事件作为一个对象返回,您可以从开发工具中进行探索。另一方面,“打印”将其展平为纯文本

一个更大的问题可能是,使用Brython来找出某些类型错误的原因可能非常棘手(尽管这一切都与Brython有关,但它令人惊讶并且工作得非常好)

有一件事可以让我们更容易地找到这类问题的根源,那就是sourcemapping。我最近将一个个人项目从Brython转移到了,发现Transcrypt的sourcemap支持有助于找出错误的原因。如此之多,以至于我没有尝试以增量方式进行,而是大胆地编译了python源代码,并一个接一个地跟踪错误,直到一切正常为止(python部分大约有2700行)。在另一个方向上,这对我来说是不可能的,但对于了解布莱顿内部结构的人来说可能不是

<script type="text/python3">

from browser import document as doc, window, html

def foo(event):
    print("BRYTHON!" + event);

window.brythonListener = foo

</script>
<!-- Bottom of HTML.. -->
<script type="text/javascript">
var jv_funcs = {
    openfile:function(iptElement, fileType){
        // some file opening code then a call back to python
        open_file(reader.result, fileType);
    }
};
</script>
<script type="text/python3" src="src/pythonscript.py"></script>


# separate python script in my case 'pythonscript.py'
def open_file(fileContent, fileType):
    # show the file in the dom..

def open_clicked():
    window.jv_funcs.openfile(document["idOpen"], "fileType")


window.open_file = open_file
document["btnOpen"].bind("click", open_clicked)