python 和 html 中的计时器与 Flask
Timer in python and html with Flask
我想在我的 html 页面中显示一个计时器。我正在使用 Flask python。
当您看到秒数减少时,我想显示动画。
我尝试了以下代码但没有取得很大成功。当我只尝试 python 功能时,它在我的终端上工作,但当我想在我的 HTML 页面上显示它时,它不再工作,我遇到以下问题:
The browser (or proxy) sent a request that this server could not understand.
这是我的代码:
file.py
:
from flask import Flask, flash, redirect, render_template, request, session, abort
import time
app = Flask(__name__)
@app.route("/")
def index():
value = "bonjour"
title_html = value
# function call
t = request.form['time']
countdown(int(t))
return render_template(
'display.html',message=title_html, time=t)
# define the countdown func.
def countdown(t):
while t:
mins, secs = divmod(t, 60)
timer = '{:02d}:{:02d}'.format(mins, secs)
print(timer, end="\r")
time.sleep(1)
t -= 1
return t
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80)
和我的 html display.html
:
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" href='/static/main.css'/>
<!-- <script type=text/javascript src="{{
url_for('static', filename='js/main.js') }}"></script> -->
<title>Rotation of the motor</title>
</head>
<body>
<h1>Hello! Here are some information about your dish:</h1>
<p>{{message}}</p>
<p><input type='text' value="{{time}}" name="time">{{time}}</p>
</body>
</html>
如果您希望您的代码像这样工作,那么您必须多次发送请求。因为如果您向服务器发送一次请求,那么它将在那里停留 t
秒,然后最后发送响应。但是,对于这种情况,您也可以在客户端使用 javascript,这是最好的主意。
我想在我的 html 页面中显示一个计时器。我正在使用 Flask python。
当您看到秒数减少时,我想显示动画。
我尝试了以下代码但没有取得很大成功。当我只尝试 python 功能时,它在我的终端上工作,但当我想在我的 HTML 页面上显示它时,它不再工作,我遇到以下问题:
The browser (or proxy) sent a request that this server could not understand.
这是我的代码:
file.py
:
from flask import Flask, flash, redirect, render_template, request, session, abort
import time
app = Flask(__name__)
@app.route("/")
def index():
value = "bonjour"
title_html = value
# function call
t = request.form['time']
countdown(int(t))
return render_template(
'display.html',message=title_html, time=t)
# define the countdown func.
def countdown(t):
while t:
mins, secs = divmod(t, 60)
timer = '{:02d}:{:02d}'.format(mins, secs)
print(timer, end="\r")
time.sleep(1)
t -= 1
return t
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80)
和我的 html display.html
:
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" href='/static/main.css'/>
<!-- <script type=text/javascript src="{{
url_for('static', filename='js/main.js') }}"></script> -->
<title>Rotation of the motor</title>
</head>
<body>
<h1>Hello! Here are some information about your dish:</h1>
<p>{{message}}</p>
<p><input type='text' value="{{time}}" name="time">{{time}}</p>
</body>
</html>
如果您希望您的代码像这样工作,那么您必须多次发送请求。因为如果您向服务器发送一次请求,那么它将在那里停留 t
秒,然后最后发送响应。但是,对于这种情况,您也可以在客户端使用 javascript,这是最好的主意。