带烧瓶的蟒蛇计时器



我正在尝试显示一个5分钟的计时器(例如)。我在用烧瓶。我知道使用javascript可能很好,但我真的想用python来做。

我有两个问题:

  1. 第一个问题:显示计时器-要覆盖的问题

我在python中为计时器编写了一个函数,该函数应该显示(例如50秒):然后00:50移除00:50并具有00:49,依此类推。。。但它正在显示:

00:50
00:49
00:48
...

这是我的代码:screen.py

from flask import Flask, Response, request, render_template, render_template_string, stream_with_context
import time
app = Flask(__name__)
timing=0
@app.route('/content', methods=['POST', 'GET']) # render the content a url differnt from index. This will be streamed into the iframe
def content():
global timing
timing = 10
# if request.form.get("submit"):
# timing = request.form['timing']
# print(timing)
def countdown(t):

while t:
mins, secs = divmod(t, 60)
timer = '{:02d}:{:02d}'.format(mins, secs)
print(timer, end="r")
yield timer
time.sleep(1)
t -= 1
# return timer

return app.response_class(countdown(timing)) #at the moment the time value is hardcoded in the function just for simplicity
# return render_template('display.html')
@app.route('/')
def index():
value = "Bonjour"
title_html = value
return render_template('display.html', message=title_html) # render a template at the index. The content will be embedded in this template
if __name__ == '__main__':
app.run(use_reloader=False)

我想找到print(timer, end="r")yield的等价性,以便覆盖计时器的值,并且在计时器减少时看不到所有结果。我希望我的解释是清楚的。

  1. 第二个问题:计时器的输入值

正如您在我的代码screen.py中看到的,timing的值是硬编码的timing=10。但我想允许用户在输入中输入他想要的值,比如:

if request.form.get("submit"):
timing = request.form['timing']
print(timing)

你可以在screen.py中看到这些行,我评论它们离开timing=10,因为当我写这些行时,我得到了以下错误:

Method Not Allowed
The method is not allowed for the requested URL.
127.0.0.1 - - [02/Aug/2021 12:50:26] "POST / HTTP/1.1" 405 -

这是链接到我的python代码display.html:的HTML代码

<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" href='/static/main.css'/>
<title>your dish</title>
</head>
<body>
<h1>{{message}}! Here are some informations about your dish:</h1>
<h2> countdown </h2>

<!-- <p>{{message}}</p> -->
<form method="POST" action=".">
<p><input name="timing" value="{{timing}}" placeholder="Enter your time"></p>
<input type="submit" name="submit" value="submit">

</form>
<div>
<iframe frameborder="0" noresize="noresize"
style='background: transparent; width: 100%; height:100%;' src="{{ url_for('content')}}"></iframe>
</div>
</body>
</html>

如何避免此错误并考虑用户在display.html的输入字段中输入的值?

我尝试在本地运行您的脚本,但我不确定您希望在哪里看到计时器;我想你在这里使用了倒计时函数。

我想向您提出一种不同的方法:使用iframe:将计数器动态流式传输到网页

from flask import Flask, render_template, Response
import time
app = Flask(__name__)
@app.route('/content') # render the content a url differnt from index. This will be streamed into the iframe
def content():
def timer(t):
for i in range(t):
time.sleep(5) #put 60 here if you want to have seconds
yield str(i)
return Response(timer(10), mimetype='text/html') #at the moment the time value is hardcoded in the function just for simplicity
@app.route('/')
def index():
return render_template('test.html.jinja') # render a template at the index. The content will be embedded in this template
if __name__ == '__main__':
app.run(use_reloader=False)

然后在你的html 中添加一个你喜欢的iframe

<!doctype html>
<head>
<title>Title</title>
</head>
<body>
<h2> countdown </h2>
<div>
<iframe frameborder="0" noresize="noresize"
style='background: transparent; width: 100%; height:100%;' src="{{ url_for('content')}}"></iframe>
</div>
</body>

结果将是你的网页上的动态倒计时

倒计时0123456789

你可以在我的回复上看到它做得又快又脏

虽然它还没有围绕你的应用程序进行调整,但(图形上也不是特别漂亮)你可以修改该功能,用表单接受用户的输入(我看到你实际上已经在应用程序中这样做了),也可以直接调整倒计时功能。

t = request.form['t']

并将表单添加到您的html中

<form method="post" action=".">
<p><input name="t" placeholder="your time"/></p>
<p><input type="submit" value="Submit"/></p>
</form>

您有相同的路由@app.route("/")出现3次。系统将选择第一个简单显示display.html的。我怀疑这目前也不起作用,因为您的页面需要message, timing的值,但这些属性在您的第一个路由中不存在。

你应该试试之类的东西


@app.route("/", methods=['POST', 'GET'])
def display():

page = 'display.html'
params = {"message":"", "timing":0} # initialize values for both message and timing. These will be returned when user loads the page (a GET call)

if request.method == 'POST':
timing = request.values.get("timing")
# do whatever processing you want
params["timing"]  = <your computed value>
params["message"] = <your message>
params["message_2"] = <your other message>
return render_template(page, **params)

删除@app.route("/")的所有其他路线

最新更新