首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python与浏览器的交互(覆盆子WebioPi)

Python与浏览器的交互(覆盆子WebioPi)
EN

Stack Overflow用户
提问于 2016-04-15 11:58:37
回答 2查看 748关注 0票数 0

我使用一个叫做"webiopi“的工具,通过一个Raspberry pi和一个中继板来控制一个健康设备。该工具将网页上的按钮与Python脚本连接起来。我可以从我创建的按钮上的GPIO引脚读取状态(低/高)。

我想要的是在浏览器中显示脚本中的值。(比如‘temperature_measured’或。“Calculated_time”)

有什么方法可以显示从python脚本到way浏览器的输出?

代码语言:javascript
复制
    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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-04-15 18:44:02

在这个论坛Wpio论坛74gm0o

给皮特·杜达什这个问题的答案,我在这里复制

是的,这是可能的。您要做的是在您的javascript中添加一个回调例程。首先,在javascript (按下按钮或定时器)中执行一些操作,并调用python宏。这一次,您在调用宏时指定一个回调例程。回调例程从宏接收数据,然后您可以使用它做任何事。

Javascript:

代码语言: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);

}

代码语言:javascript
复制
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:

代码语言:javascript
复制
@webiopi.macro

def sendData():

HTML:

代码语言:javascript
复制
<div id="content" align="center">
<td></td>

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

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

票数 0
EN

Stack Overflow用户

发布于 2016-04-15 12:22:30

您可以使用PHP调用脚本并将输出回显到页面。您需要安装并启用PHP,并且该文件必须以.php结束。如果你知道你正在使用的网络服务器,我可以给你一些额外的帮助。还请注意,脚本应该打印它想要在网页上的所有信息到标准输出。

代码语言:javascript
复制
    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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36646537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档