【问题标题】:How to display text on the web page using python and HTML如何使用 python 和 HTML 在网页上显示文本
【发布时间】:2012-11-11 23:33:30
【问题描述】:

我有一个非常简单的网页示例,使用 python 从 html 文件中读取。名为 led.html 的 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)

网页只包含一个名为“on”的按钮,当我单击它时,我可以看到终端上显示文本“Hello World”。 我的问题是单击该按钮后如何使此文本显示在网页上的“打开”按钮上? 提前致谢。

【问题讨论】:

    标签: python html text webpage


    【解决方案1】:

    您会想要使用某种模板系统。我用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)
    

    希望这会有所帮助!

    安德鲁

    【讨论】:

    • 嗨 Andrew,非常感谢您的解决方案,只需将“import”更改为“from”即可。将它们合并到一个文件中很好,因为我的原始 html 也不大.我试图修改代码,让它显示“请稍候......”大约 5 秒钟,然后显示“Ok!它完成了”,但我做不到。 if switch: self.led_switch = int(switch) myText = "Please wait ......" 做一些大约需要 5 秒的任务 myText = "Ok! it done"
    • 好的,我更新了代码以显示“请稍候”,然后 5 秒后它变为“好的!完成了”。这就是你想要的吗?
    • @majidhantoosh 你能更新你的问题以反映更新代码吗?
    【解决方案2】:

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

    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)
    

    【讨论】:

    • 感谢btel的回复,如果点击按钮后显示“请稍候....”,然后显示“Hello World”,我该怎么办?你有想法吗?谢谢
    • 你可以在 JavaScript 中使用setTimeout 方法。或者,您可以强制重新加载网页。
    猜你喜欢
    • 1970-01-01
    • 2021-12-06
    • 2016-10-18
    • 2018-05-25
    • 2015-06-16
    • 1970-01-01
    • 2013-06-24
    • 2014-12-11
    • 1970-01-01
    相关资源
    最近更新 更多