Hello,
I'm a new to web2py and web programming, so bear with me if this is a
basic question. I'm trying to figure out how to dynamically update a
button based on a return value from a python function.
Here's a little more background on my project. I'm trying to create
an instrument panel that has a single button which controls power on/
off to a remote device on my local network. Without going into too
much detail, when the button is pressed it passes a '1' to a python
function which ultimately turns the device on through tcp modbus. The
remote device then responds with a '1' indicating power is on and I
pass this value back from the controller to the view. When I press
the same button again, it sends a '0' out which turns off the device
and it responds with a '0'. Now how do I use this '1' or '0' response
to either update the button color or refresh the entire page so the
button gets updated. Currently, I have to manually refresh the page
as can seen below.
Everything is working except for the dynamic button refresh. I am use
button colors to indicate the instrument status as follows green=on,
red=off. Here is my code so far:
CSS
<style type="text/css">
input.greenbutton
{background-color:#33ff33;}
input.redbutton
{background-color:#FF0000;}
HTML
{{if CH1==1:}}
<input type="button" value="click me" class="greenbutton"
onclick="ajax('CH1_ctrl?q=0',[],'target');"/>
{{else:}}
<input type="button" value="click me" class="redbutton"
onclick="ajax('CH1_ctrl?q=1',[],'target');"/>
{{pass:}}
div id="target"></div>
<a href="javascript:history.go(0)">click to refresh page</a>
PYTHON
def CH1_ctrl():
...
return dict(CH1=CH1)
Thanks in advance for any advice. Please be explicit with your
answers. As I said, I am new this stuff.