python - python中的计时器与烧瓶
问题描述
我正在尝试显示 5 分钟的计时器(例如)。我正在使用烧瓶。我知道使用 javascript 可能很好,但我真的想用 python 来做。
我有两个问题:
- 第一个问题:计时器的显示 - 要覆盖的问题
我在 python 中为计时器编写了一个函数,它应该显示(例如 50 秒):
00:50
然后 remove00:50
和 have 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)
我想找到 for 的等价物,print(timer, end="\r")
以便yield
覆盖 timer 的值,并且在它减少时看不到所有结果。我希望我的解释清楚。
- 第二题:定时器的输入值
正如您在我的代码中看到的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 代码的 HTML 代码display.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>
如何避免此错误并考虑用户在 my 的输入字段中输入的值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
你可以在我的 repl 上看到它做得又快又脏
虽然它还没有围绕您的应用程序进行调整,(并且图形不是特别漂亮)您可以修改该函数以接受用户使用表单的输入(我看到您实际上已经在您的应用程序中这样做了),或者也可以直接调整倒计时功能.
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>
推荐阅读
- flutter - Flutter - 如何填充 Stepper 控件构建器中的剩余空间?
- hyperlink - JIRA 评论中不显示链接别名
- c - 提示用户输入后出现“分段错误(核心转储)”
- python - 如何在给定特定规则的情况下将字符串中的特定字母大写
- javascript - 如何通过拖放上传多个文件并使用ajax浏览
- spring - 无法在服务中启动spring cloud eureka客户端
- reporting-services - 直接在报告上过滤多个值
- sql - 在 PLSQL 中声明一个常量
- c# - 在 Net Framework 4.7.1 上启动 Apache Ignite 2.7 时出现序列化异常
- node.js - 为什么我使用 google-api-nodejs-client 对 Google Analytics 的 API 调用在生产中不起作用?