首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何显示“请稍候...”在网页上单击使用Python和HTML的按钮

如何显示“请稍候...”在网页上单击使用Python和HTML的按钮
EN

Stack Overflow用户
提问于 2012-11-13 06:03:15
回答 1查看 1.1K关注 0票数 0

我需要显示文本“请稍候...”当我点击on按钮,直到guvcview打开,然后显示"guvcview is .The running“,而不是关闭下面的guvcview?.In代码时,我试图显示”请稍候...“。但是我不能。有些人说这需要重新加载页面。这只是一个例子,在我的代码中,我对页面登录和注销进行了身份验证。我需要最简单的方法,谢谢。

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-14 01:03:54

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

代码语言:javascript
复制
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)

希望这能有所帮助!

安德鲁

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13352332

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档