首页 > 解决方案 > python中的计时器与烧瓶

问题描述

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

我有两个问题:

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

我在 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 的值,并且在它减少时看不到所有结果。我希望我的解释清楚。

  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 代码的 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

标签: pythonflasktimer

解决方案


我尝试在本地运行您的脚本,但我不确定您希望在哪里看到计时器;我假设您从这里使用了倒计时功能。

我想向您推荐一种不同的方法:使用 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>

推荐阅读