如何显示";请稍候……”;在网页上使用Python&;单击按钮时;HTML
我需要在点击on按钮直到guvcview打开时显示文本“请稍候…”,然后显示“guvcview正在运行”。关闭guvcview?时也是如此。在下面的python代码中,我尝试显示“请稍候…”,但我无法显示。 有人说需要重新加载页面。这只是一个示例,在我的代码中,我对页面登录和注销进行了身份验证。 我需要最简单的方法,谢谢如何显示";请稍候……”;在网页上使用Python&;单击按钮时;HTML,python,html,web-applications,textbox,reload,Python,Html,Web Applications,Textbox,Reload,我需要在点击on按钮直到guvcview打开时显示文本“请稍候…”,然后显示“guvcview正在运行”。关闭guvcview?时也是如此。在下面的python代码中,我尝试显示“请稍候…”,但我无法显示。 有人说需要重新加载页面。这只是一个示例,在我的代码中,我对页面登录和注销进行了身份验证。 我需要最简单的方法,谢谢 import cherrypy import os.path import struct import time import subprocess import comman
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)
import cherrypy
导入操作系统路径
导入结构
导入时间
导入子流程
导入命令
类服务器(对象):
led_开启=1
led_关闭=1
def索引(自、开=“”、关=“”):
html=”“”
{htmlText}
"""
myText=“”
如果启用:
self.led_on=int(on)
myText=“请稍候…”
format(htmlText=myText)
subprocess.call(['guvcview&',shell=True)
时间。睡眠(2)
output=commands.getoutput('ps-A')
如果输出中有“guvcview”:
myText=“guvcview正在运行”
如果关闭:
self.led_off=int(关)
myText=“请稍候…”
format(htmlText=myText)
subprocess.call(['sudo-pkill-guvcview'],shell=True)
时间。睡眠(2)
output=commands.getoutput('ps-A')
如果输出中有“guvcview”:
myText=“请稍候…”
其他:
myText=“guvcview已关闭”
返回html.format(htmlText=myText)
index.exposed=True
形态={
“全局”:
'server.socket_host':'0.0.0.0',
'server.socket_port':8085
},
“/图像”:{
'tools.staticdir.on':True,
'tools.staticdir.dir':os.path.abspath('images')
}
}
quickstart(Server(),config=conf)
像其他人建议的那样,您需要客户端脚本来实现此功能。但是,如果实现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)
import cherrypy
导入操作系统路径
导入结构
导入时间
导入子流程
导入命令
类服务器(对象):
led_开启=1
led_关闭=1
def索引(自):
html=”“”
功能激活(当前状态)
{
//IE7+、Firefox、Chrome、Opera、Safari的代码
if(window.XMLHttpRequest)
xmlhttp=新的XMLHttpRequest();
else//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(“UserMessage”).innerHTML=xmlhttp.responseText;
}
}
open(“GET”、“/Process?on=“+CurrentState,true”);
xmlhttp.send();
}
"""
返回html
index.exposed=True
def进程(自身,在='0'):
如果打开=='1':
self.led_on=int(on)
subprocess.call(['guvcview&',shell=True)
时间。睡眠(2)
output=commands.getoutput('ps-A')
如果输出中有“guvcview”:
返回“guvcview正在运行”
如果打开==“0”:
self.led_off=int(开)
subprocess.call(['sudo-pkill-guvcview'],shell=True)
时间。睡眠(2)
output=commands.getoutput('ps-A')
如果输出中有“guvcview”:
return“请稍候…”
其他:
返回“guvcview已关闭”
return“请稍候…”
Process.exposed=True
形态={
“全局”:
'server.socket_host':'0.0.0.0',
'server.socket_port':8085
},
“/图像”:{
'tools.staticdir.on':True,
'tools.staticdir.dir':os.path.abspath('images')
}
}
quickstart(Server(),config=conf)
希望这有帮助
Andrew这不应该在客户端完成吗,例如:javascript?不使用javascript是否可能?我不知道。单击浏览器中的按钮是客户端事件,只能使用javascript进行拦截。另一种方法,只能在客户端完成,即使用HTTP POST,但这会使浏览器导航到新页面,这不是您想要的,我怀疑在使用javascript的情况下,如果没有客户端技术(可能是javascript或actionscript…),是否可能做到这一点,它应该添加到这个python代码中,还是在没有python的情况下使用它?我需要使用python代码,因为这只是一段稍长的python代码。感谢Andrew的回复,不幸的是,代码不起作用,当我单击“开”或“关”按钮时不会发生任何事情!好的,再试一次代码-我已经更新了几个补丁。我设置了一个测试环境,并验证了html、js和cherrypy是否正常工作。。。但是我不能测试你的子流程。太好了,代码运行得很好,非常感谢你,安德鲁,你是一个非常有帮助的人。再次感谢。