0

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?

1 Answer 1

1

If I change your javascript function to this, it works for me:

function UpdateLeds() {
    var x = document.getElementById('demo');
    var random = Math.floor((Math.random() * 10) + 1);
    x.innerHTML = "Status: "+random;
}

I'm not sure exactly what you're trying to do with $def with(rnd) in your HTML. I think you might be mixing web.py python and javascript syntax (just a guess), but once the page gets rendered in the browser, the python isn't doing you any good, and you have to do what you want with pure javascript.

Here's a JSFiddle: https://jsfiddle.net/s7n6nxng/

Sign up to request clarification or add additional context in comments.

3 Comments

I tried with pure JS, and as you remark it works. However I would like to update the value of demo id with the result of a python function to display some led status (as the function name suggests). Thus I thought web.py was a good choice.
You can't call a python function from within JS like that. Javascript like this runs on the client-side (in the browser), while the python runs on the server-side. If you want to use the python function, you're either going to have write a javascript function which refreshes the page every second (and therefore makes a call to the server) or use something like AJAX to do an independent call to the server where you can get the result of the function and insert it into the page.
Ah thanks this makes things clearer. I'll give AJAX a try!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.