I use a tool called "webiopi" to control a wellness device via a Raspberry pi and a relay board. This tool connect buttons on a webpage with a Python script. I can read the state (low/high) from the GPIO pins on the buttons i've created.
What i want is to show values from the script in the browser. (Like ' temperature_measured' or. 'Calculated_time')
Is there a way i can show output from the python script to the 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>
In a this forum [Wpio forum][1]https://groups.google.com/forum/#!topic/webiopi/DreW_74gm0o
gives Pete Dudash the answer to this question, which i copy here
Yes, this is possible. What you’re looking to do is add a callback routine to your javascript. First you execute some action in javascript (either from a button press or a timer) and call a python macro. This time, you specify a call back routine when calling the macro. The call back routine receives data from the macro and then you can do whatever you want with it.
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>