Python与浏览器的交互(Raspberry WebioPi)

时间:2016-04-15 11:58:37

标签: javascript python html raspberry-pi webiopi

我使用名为" webiopi"通过Raspberry pi和继电器板控制健康设备。此工具使用Python脚本连接网页上的按钮。 我可以从我创建的按钮上的GPIO引脚读取状态(低/高)。

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

有没有办法可以将python脚本的输出显示给webbrowser?

    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>

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

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

给Pete 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>