开发者

Having issues with function re-running after page refresh with python & web.py

Like the title says, I'm having issues with web.py.

I have a script that starts a web.py instance and is serving basic pages. Using the web.py template system, I'm able to get functions like time.time() to refresh every time I reload(meaning, when I refresh, the time printed has increased). However, I have a self written function that performs some math and returns a multiline string. When the function is called either from a script or on the command line, it returns a different value each time, however, when I call it using the web.py templating system, it see开发者_如何学JAVAms to only run the function once, and then will not re-run it again on additional page reloads (even though time.time() updates each time). The only way to get it to re-run is to restart the script.

Here's the function in question:

    def casecount():
      curcount = 80
      sc = startcount(date)
      delta = sc - curcount
      pace = float(delta) / float(ElTime(time, "9:00"))
      eta = float(curcount) / float(pace)
      return '''We started with %d cases as of 9AM<br> 
      Remaining cases as of %s: %d<br>
      We\'re working at a pace of %.3f cases per hour. At this rate, we will finish in approximately %.3f hours<br>''' % (sc, time, curcount, pace, eta)

Obviously there are other functions I've written that the script calls, but I know for a fact that it works when run on its own.

I'm providing function access to the web.py templating system using this code:

    render = web.template.render('templates/', cache=False, globals={'cc': casecount, 'time': time} )

and finally, I'm calling the code in the template with this:

    <html>
    <head>
    </head>
    <body>
    $time()
    $cc()
    </body>
    </html>

Any ideas? I've been at it for a while and have searched google to no avail.

FIXED: Welp, I fixed it with the help of a redditor. My problem was using a global variable for time. The variable, of course, was loaded properly the first time the script was imported, but was not subsequently updated on the function reload, because, well, why should it be, it was out of scope.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜