Page 1 of 1

webpy: how to change the value of a textbox periodically?

Posted: Thu Oct 29, 2015 3:46 pm
by yum
Hi,

Code: Select all

import web
import RPi.GPIO as GPIO
from web import form
import random
import time
.
.
.
# Defining the buttons. 'id' stands for HTML id of the element.
# #'value' is the value of the button as perceived by Python.
# #'html' is the text displayed in HTML page. 'class_' is HTML class
my_form = form.Form(
    form.Button("btn", id="btnR", value="on", html="on", class_="on"),
    form.Button("btn", id="btnG", value="off", html="off", class_="off"),
    form.Textbox('Temp:', id='temp_now', value="missin g "),
    form.Textbox('Max:', id='temp_max', value="missing"),
    form.Textbox('Min:', id='temp_min', value="not implemented"),
    form.Radio('details', ['Home Page', 'Content', 'Contact Us', 'Sitemap']),
)


# define the task of index page
class index:
    # rendering the HTML page
    def GET(self):
        # return app.root_path()
        temp_now = randomTemp()
        print (temp_now)
        print (" ")
        form_ = my_form()
# UPDATE VALUES OF TEXTBOXES HERE
        return render.index(form_, "Raspberry Pi B+")


# run
if __name__ == '__main__':
    app.run()

I use web.py in my code and have to periodically update the value of textboxes.
My questions are:
1.)
How can i achieve to do some other job (like reading data from a sensor), while
the web-server is running ("app.run()")
2.)
What is the correct syntax to change the value of the textboxes?
e.g:
within def GET(): what syntax do I have to use to set the value of the
textbox "temp_now"?
??? form_.id(temp_now).set_value(temp_now) ???

Re: webpy: how to change the value of a textbox periodically

Posted: Fri Nov 13, 2015 3:59 pm
by -rst-
1) Depends on the sensor(s) and do you want just the last value when page refreshed... you could look at creating another thread for the reading code within the same application or a separate application (both running in the background maybe) that updates a file the web server will then read and server out.

2) Looks like you are using this package https://pypi.python.org/pypi/web.py ? The documentation is not very clear at a quick glance how to assign form field values but educated guess seemed to work:

Code: Select all

import web
from web import form
import random

my_form = form.Form(
    form.Textbox(name="temp", id="tmp_now", value="N/A", description="Temp:")
)

urls = (
    '/', 'index'
)

class index:
    def GET(self):
        print("GET")
        f = my_form()
        f.temp.value = random.random() * 100
        return f.render()

if __name__ == "__main__":
    print("Starting...")
    app = web.application(urls, globals())
    app.run()
Note that I differentiated the name and the label (description) of the text box so that the name can be used to access it in the code as this would not work:

Code: Select all

...
    form.Textbox(name="Temp:", id="tmp_now", value="N/A")
...
        f.Temp:.value = random.random() * 100
...
Could use this though:

Code: Select all

        f["Temp:"].value = random.random() * 100

All documentation seems to concentrate on templates so might be easier to make your form into a template and use something along:

Code: Select all

    render = web.template.render('templates/')
...
        return render.mytemplateform(temp_now = temp_now, temp_max = temp_max, temp_min = temp_min)

To get it to automatically update you would need to add JavaScript or a meta tag on the returned page to reload periodically.