【问题标题】:How to display "Please wait...." on the web page when click on a button using Python&HTML使用Python HTML单击按钮时如何在网页上显示“请稍候......”
【发布时间】:2012-11-01 09:15:01
【问题描述】:

当我单击 ON 按钮直到 guvcview 打开时,我需要显示文本“请稍候.....”,然后显示“guvcview 正在运行”。当我关闭 guvcview 时也是如此?以下python代码,我试图显示“请稍候.....”但我做不到。 有人说需要重新加载页面。这只是示例,在我的代码中,我对页面登录和注销进行了身份验证。 我需要最简单的方法,谢谢。

import cherrypy
import os.path
import struct
import time
import subprocess
import commands

class Server(object):
    led_on=1 
    led_off=1 
    def index(self,  on='', off=''):
        html = """
         <html>
           <body>
             <br>
             <p>{htmlText}
             <p>
             <a href="?on=1"><img src="images/on.png"></a>
             <a href="?off=1"><img src="images/off.png"></a>
           </body>
          </html>
                """
        myText = ''
        if on:
            self.led_on = int(on)             
            myText = "Please wait ....."
            html.format(htmlText=myText)
            subprocess.call(['guvcview &'], shell=True)
            time.sleep(2)
            output = commands.getoutput('ps -A')
            if 'guvcview' in output:
                myText = "guvcview is running"

        if off:
            self.led_off = int(off)             
            myText = "Please wait ....."
            html.format(htmlText=myText)
            subprocess.call(['sudo pkill guvcview'], shell=True)
            time.sleep(2)
            output = commands.getoutput('ps -A')
            if 'guvcview' in output:
                myText = "Please wait ....."
            else:
                myText = "guvcview closed"

        return html.format(htmlText=myText)
    index.exposed = True
conf = {
        'global' : { 
            'server.socket_host': '0.0.0.0', 
            'server.socket_port': 8085 
        },

        '/images': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': os.path.abspath('images')
        }
    }
cherrypy.quickstart(Server(), config=conf)

【问题讨论】:

  • 这不应该在客户端完成吗,即:javascript?
  • 不使用javascript可以吗?
  • 我不知道。单击浏览器中的按钮是客户端事件,只能使用 javascript 拦截。另一种只能在客户端完成的替代方法是执行 HTTP POST,但这会使浏览器导航到新页面,这不是您想要的,我怀疑
  • 怀疑没有一些客户端技术(可能是javascript或actionscript ...)也能做到
  • 如果使用javascript,应该添加到这个python代码中还是不使用python?我需要使用python代码,因为这只是一段较长的python代码。

标签: python html web-applications textbox reload


【解决方案1】:

就像其他人建议的那样,您将需要客户端脚本才能使其正常工作。但是,如果您实施 ajax 解决方案,则不需要重定向到另一个页面。试试这个,看看这是不是你想要的。

import cherrypy
import os.path
import struct
import time
import subprocess
import commands

class Server(object):
    led_on=1 
    led_off=1 
    def index(self):
        html = """
         <html>
           <body>
           <script language="javascript" type="text/javascript">
           function Activate(CurrentState)
           {
               // code for IE7+, Firefox, Chrome, Opera, Safari
               if(window.XMLHttpRequest)
                   xmlhttp=new XMLHttpRequest();
               else// code for IE6, IE5
                   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

               xmlhttp.onreadystatechange=function()
               {
                   if (xmlhttp.readyState==4 && xmlhttp.status==200)
                   {
                       document.getElementById("UserMessage").innerHTML = xmlhttp.responseText;
                   }
               }

               xmlhttp.open("GET","/Process?on=" + CurrentState, true);
               xmlhttp.send();
           }
           </script>
             <br>
             <p id="UserMessage"><p>
             <a onclick="document.getElementById('UserMessage').innerHTML = 'Please wait .....';Activate('1');"><img src="images/on.png"></a>
             <a onclick="document.getElementById('UserMessage').innerHTML = 'Please wait .....';Activate('0');"><img src="images/off.png"></a>
           </body>
          </html>
                """
        return html
    index.exposed = True

    def Process(self,  on='0'):
        if on == '1':
            self.led_on = int(on)             
            subprocess.call(['guvcview &'], shell=True)
            time.sleep(2)
            output = commands.getoutput('ps -A')
            if 'guvcview' in output:
                return "guvcview is running"

        if on == '0':
            self.led_off = int(on)
            subprocess.call(['sudo pkill guvcview'], shell=True)
            time.sleep(2)
            output = commands.getoutput('ps -A')
            if 'guvcview' in output:
                return "Please wait ....."
            else:
                return "guvcview closed"

        return "Please wait ....."
    Process.exposed = True

conf = {
        'global' : { 
            'server.socket_host': '0.0.0.0', 
            'server.socket_port': 8085 
        },

        '/images': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': os.path.abspath('images')
        }
    }
cherrypy.quickstart(Server(), config=conf)

希望这会有所帮助!

安德鲁

【讨论】:

  • 感谢 Andrew 的回复,不幸的是,代码不起作用,当我既不点击“开”也不点击“关”按钮时,什么也没有发生!
  • 好的,再试一次代码 - 我已经更新了几个修复程序。我设置了一个测试环境并验证了 html、js 和cherrypy 工作正常……但我无法测试你的子进程。
  • 太好了,代码工作得很好,非常感谢安德鲁,你是个很有帮助的人。再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-13
  • 2012-02-05
  • 2021-06-23
  • 1970-01-01
相关资源
最近更新 更多