我使用一个叫做"webiopi“的工具,通过一个Raspberry pi和一个中继板来控制一个健康设备。该工具将网页上的按钮与Python脚本连接起来。我可以从我创建的按钮上的GPIO引脚读取状态(低/高)。
我想要的是在浏览器中显示脚本中的值。(比如‘temperature_measured’或。“Calculated_time”)
有什么方法可以显示从python脚本到way浏览器的输出?
button = webiopi().createMacroButton("macro", "Normaal", "Prog1");
$("#top").append(button)
<div id="content" align="center">
<td>
</td>
{{print here output from script.py}}
<div id="top"></div>
</div>
</div>发布于 2016-04-15 18:44:02
在这个论坛Wpio论坛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.macrodef sendData():
HTML:
<div id="content" align="center">
<td></td>
<span id="pythonResult">{{print here output from script.py}}</span>
<div id="top"></div>
发布于 2016-04-15 12:22:30
您可以使用PHP调用脚本并将输出回显到页面。您需要安装并启用PHP,并且该文件必须以.php结束。如果你知道你正在使用的网络服务器,我可以给你一些额外的帮助。还请注意,脚本应该打印它想要在网页上的所有信息到标准输出。
button = webiopi().createMacroButton("macro", "Normaal", "Prog1");
$("#top").append(button)
<div id="content" align="center">
<td>
</td>
<?php exec("python myscript.py", $out); echo $out; ?>
<div id="top"></div>
</div>
</div>https://stackoverflow.com/questions/36646537
复制相似问题