【问题标题】:Python interaction with browser (Raspberry WebioPi)Python 与浏览器的交互(Raspberry WebioPi)
【发布时间】:2016-08-07 09:22:27
【问题描述】:

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

我想要的是在浏览器中显示脚本中的值。 (如“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>

【问题讨论】:

    标签: javascript python html raspberry-pi webiopi


    【解决方案1】:

    在这个论坛[Wpio forum][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
    

    定义发送数据():

    HTML:

    <div id="content" align="center">
    <td></td>
    
    <span id="pythonResult">{{print here output from script.py}}</span>
    
    <div id="top"></div>
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 这是我绝对不想要的。我已经不得不处理 Javascript 和 Python,这对我来说是全新的。我不想要 PHP,这对我来说也是新的。不过,谢谢你的建议
      • 好吧,如果您不了解 Perl,这是最简单的解决方案。
      猜你喜欢
      • 2022-07-04
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 2013-12-25
      • 1970-01-01
      • 2020-05-11
      • 2013-06-19
      相关资源
      最近更新 更多