使用CherryPy更新字段

时间:2015-01-11 19:06:35

标签: html cherrypy

我对网络开发很陌生,所以请耐心等待。我有一个控制网页(由cherrypy运行),状态值表示运行服务器端的python进程。如何更新或推送新值?

我现在唯一的方式就是:

<meta http-equiv="refresh" content="1">

更新整个页面。但是我更喜欢只需要更新所需的字段。 html

分别直接使用纯cherrypy可以轻松实现这一点吗?

1 个答案:

答案 0 :(得分:2)

最佳方式是使用javascript / websockets / ajax。

但考虑到“仅服务器端”的限制。我相信你能做到这一点 与iframes。

import  cherrypy as cp

MAIN_PAGE = """
<html>
  <body>
    <h1> Im the main page! </h1>
    <iframe frameBorder="0"  src="/frame"></iframe>
  </body>
</html>
"""

FRAME_PAGE = """
<html>
  <head>
   <meta http-equiv="refresh" content="1">
  </head>
  <body>
     Counter: <strong>{}</strong>
  </body>
</html>
"""

class Root:

    def __init__(self):
        self.counter = 0

    @cp.expose
    def default(self):
        return MAIN_PAGE

    @cp.expose
    def frame(self):
        self.counter += 1
        return FRAME_PAGE.format(self.counter)

cp.quickstart(Root())

确实正在重新加载整页,但只是在框架内。要避免iframe上的闪烁,您需要一些javascript/css