在这个论坛 [Wpio 论坛][1] https://groups.google.com/forum/#!topic/webiopi/DreW_74gm0o
给皮特杜达什这个问题的答案,我在这里复制
是的,这是可能的。您要做的是向您的 javascript 添加一个回调例程。首先,您在 javascript 中执行一些操作(通过按钮按下或计时器)并调用 python 宏。这一次,您在调用宏时指定一个回调例程。回调例程从宏接收数据,然后你可以用它做任何你想做的事情。
Javascript:
function getPythonResponse() {
// "sendData" is the name of the macro you specify in your script.py
// "[]" is an empty list. If you want to send data for the macro to use, you would include that here
// "handlePythonResponse" is the name of the callback routine that will execute once the python macro is finished
webiopi().callMacro("sendData", [], handlePythonResponse);
}
var handlePythonResponse = function(macro, args, response) {
// "response" is the variable that holds the data from script.py
// Now we can apply those results to the webpage by assigning the value to the "pythonResult" id. This is the element in your HTML where you want to print the info.
$("#pythonResult").text(response);
}
Python:
@webiopi.macro
定义发送数据():
HTML:
<div id="content" align="center">
<td></td>
<span id="pythonResult">{{print here output from script.py}}</span>
<div id="top"></div>