2016-04-15 83 views
0

我使用名为“webiopi”的工具通过Raspberry Pi和中继板控制健康设备。该工具将网页上的按钮与Python脚本连接起来。 我可以从我创建的按钮上的GPIO引脚读取状态(低/高)。与浏览器的Python交互(Raspberry WebioPi)

我想要的是在浏览器中显示脚本的值。 (像'temperature_measured'或''Calculated_time')

有没有一种方法可以显示输出从python脚本到网页浏览器?

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> 

回答

0

在这个论坛[Wpio论坛] [1] https://groups.google.com/forum/#!topic/webiopi/DreW_74gm0o

给皮特Dudash回答这个问题,我在这里复制

是的,这是可能的。你要做的是给你的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 

DEF的SendData():

HTML:

<div id="content" align="center"> 
<td></td> 

<span id="pythonResult">{{print here output from script.py}}</span> 

<div id="top"></div> 

0

您可以使用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> 
+0

这是我绝对不想。我已经必须处理Javascript和Python,这对我来说是全新的。我不希望PHP对我来说也是新的。但是,感谢您的建议 – Richard

+0

那么,如果您不知道Perl,这是最简单的解决方案。 – randomdude999