P i*_*P i 3 javascript python-3.x brython
编辑:谷歌群组帖子
我正在和Brython一起玩。
我想弄清楚如何从 JavaScript 执行 Brython 代码。
http://www.brython.info/static_doc/en/jsobjects.html <--这看起来像相关的文档页面,但似乎缺少从 JavaScript 调用 Brython 函数的示例。
对于我的具体情况,我正在收听 MIDI 事件(使用https://github.com/cwilso/WebMIDIAPIShim)
我希望 Brython 代码响应接收到的 MIDI 事件而执行。
目前我正在尝试:
function myMIDIMessagehandler( event )
{
if( brythonListener != null )
brythonListener( event );
Run Code Online (Sandbox Code Playgroud)
和 Brython 代码:
<script type="text/python3">
from browser import document as doc, window, html
def foo(event):
print("BRYTHON!" + event);
window.brythonListener = foo
</script>
Run Code Online (Sandbox Code Playgroud)
但是当我在我的 MIDI 键盘上按下一个音符时,我得到:
我不知道如何处理这个错误,而且我完全不确定这种方法是否合理。
已经一年了,所以我想你很久以前就已经解决了这个问题,但是你的方法原则上是正确的,我也这样做,并且当你像使用 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)
Run Code Online (Sandbox Code Playgroud)
需要知道的一点是,如果您使用 window.console.log(event),您将把事件作为可以从开发工具探索的对象取回。另一方面,打印将其展平为纯文本。
一个更大的问题可能是使用 Brython 找出某些类型错误的原因可能非常棘手(尽管 Brython 非常棒,而且效果很好)。
更容易深入了解此类问题的一件事是源映射。我最近提出一个个人项目从Brython到Transcrypt,发现Transcrypt的sourcemap支持有很大帮助隔离原因的错误。以至于我没有尝试增量地做,而是大胆地编译python源代码并逐一跟踪错误,直到一切正常(python部分大约2700行)。从另一个方向看,这对我来说是不可能的,但对于了解 Brython 内部结构的人来说可能不是。
| 归档时间: |
|
| 查看次数: |
1090 次 |
| 最近记录: |