2012-11-11 46 views
0

我使用python从html文件中读取了非常简单的网页示例。在HTML叫led.html在波纹管:如何使用python和HTML在网页上显示文本

<html> 
<body> 
<br> 
<p> 
<p> 
<a href="?switch=1"><img src="images/on.png"></a> 
</body> 
</html> 

和Python代码是:

import cherrypy 
import os.path 
import struct 
class Server(object): 
    led_switch=1 
    def index(self, switch=''): 
     html = open('led.html','r').read() 
     if switch: 
      self.led_switch = int(switch)    
      print "Hellow world"    
     return html 
    index.exposed = True 

conf = { 
     'global' : { 
      'server.socket_host': '0.0.0.0', #0.0.0.0 or specific IP 
      'server.socket_port': 8080 #server port 
     }, 

     '/images': { #images served as static files 
      'tools.staticdir.on': True, 
      'tools.staticdir.dir': os.path.abspath('images') 
     }, 

     '/favicon.ico': { #favorite icon 
      'tools.staticfile.on': True, 
      'tools.staticfile.filename': os.path.abspath("images/bulb.ico") 
     } 
    } 
cherrypy.quickstart(Server(), config=conf) 

该网页只包含名为“上的”一键,当我点击它,我可以看到在终端上显示文本“Hello World”。 我的问题是如何使该文本显示在网页上的“开启”按钮后点击该按钮? 在此先感谢。

回答

0

你会想要使用某种模板系统。我使用Jinja2太棒了!

而不是...

html = open('led.html','r').read() 

你会用......

import cherrypy 
import os.path 
import struct 
from jinja2 import Template 

class Server(object): 
    led_switch=1 
    def index(self, switch=''): 
     myText = '' 
     if switch: 
      self.led_switch = int(switch)    
      myText = "Please Wait" 
     html = Template(""" 
       <html> 
       <body onload='setTimeout(function(){document.getElementById("UserMessage").innerHTML = "Ok! it's done"}, 5000)'> 
       <br> 
       <p id="UserMessage">{{ htmlText }}<p> 
       <a href="?switch=1"><img src="images/on.png"></a> 
       </body> 
       </html> 
       """) 

     return html.render(htmlText=myText) 
    index.exposed = True 

    conf = { 
     'global' : { 
      'server.socket_host': '0.0.0.0', #0.0.0.0 or specific IP 
      'server.socket_port': 8080 #server port 
     }, 

     '/images': { #images served as static files 
      'tools.staticdir.on': True, 
      'tools.staticdir.dir': os.path.abspath('images') 
     }, 

     '/favicon.ico': { #favorite icon 
      'tools.staticfile.on': True, 
      'tools.staticfile.filename': os.path.abspath("images/bulb.ico") 
     } 
    } 
cherrypy.quickstart(Server(), config=conf) 

希望这有助于!

安德鲁

+0

安德鲁嗨, 谢谢你这么多的解决方案,只是其需要将“导入”改为“来自”,因为我的原始HTML也不大,所以它很好地将它们合并到一个文件中。 我试图修改代码,让它显示“Please wait ....”约5秒钟,然后显示“Ok!its done”,但是我不能。 如果开关: self.led_switch = INT(开关) =会将myText “请稍候......” 做某些任务的花费大约5秒钟会将myText = “OK!它做” – Linux

+0

好吧,我更新显示“Please Wait”的代码在5秒后显示为“Ok!it's done”。那是你要做的吗? –

+0

@majidhantoosh你可以更新你的问题,以反映更新代码? – btel

0

如果你不想使用神社(以避免额外的依赖),你仍然可以使用字符串格式化:

class Server(object): 
    led_switch=1 
    def index(self, switch=''): 
     myText = '' 
     if switch: 
      self.led_switch = int(switch)    
      myText = "Hellow world" 
     html = """ 
     <html> 
      <body> 
      <br> 
      <p>{htmlText} 
      <p> 
      <a href="?switch=1"><img src="images/on.png"></a> 
      </body> 
      </html> 
       """ 
     return html.format(htmlText=myText) 
+0

感谢btel的回复,点击按钮后显示“Please wait ....”几秒钟后应该怎么办,然后显示“Hello World”?你有想法吗?谢谢 – Linux

+0

你可以在JavaScript中使用'setTimeout'方法。或者,您可以强制重新加载网页。 – btel

相关问题