Hi I am starting with web.py and there are still plenty of things I do not understand. This one is really annoying. I try to do a simple page by using web.py and javascript. The idea is to display a random value on a div which is updated every second by a python function. This is the code I am using:
import web
import random
def rnd():
return random.randint(0, 10)
render = web.template.render('templates', globals={'rndm':rnd})
urls = (
'/mhs(.*)', 'mhs',
'/(.*)', 'index',
)
app = web.application(urls, globals())
class reloader:
def GET(self):
return render.reloader(rnd)
class index:
def GET(self, name='Bob'):
return render.index(name)
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()
Then the reloader.html file:
$def with(rnd)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<div id='demo'>Status: ? </div>
<script type="text/javascript">
function UpdateLeds() {
var x = document.getElementById('demo');
x.innerHTML = "Status: $rndm()";
}
setInterval(UpdateLeds, 1000);
</script>
</body>
</html>
I expect the code to display a page with a random number in 0 to 10 refreshed every second. However all I can get is a page which displays a static value (i.e: the first random number obtained by rnd). However if I manually refresh the page, the value is updated as I expect. I can't understand if the problem is in my code or in my understanding of the architecture. Can someone help me please?