[英]Need update value passed by render_template with flask in html template td tag every 10 seconds
I have this:我有这个:
@views.route('/')
def home():
while True:
try:
token=getToken()
if(token!='null' or token!=''):
plazas=getInfo(token,id)
except:
print('Conection failed')
time.sleep(secs)
return render_template("home.html", plazas=plazas)
Need update "plazas" variable value which is constantly refreshing with "while True" loop in my html template on td tag:需要更新“plazas”变量值,它在我的 td 标签上的 html 模板中使用“while True”循环不断刷新:
{% for parking in parkings %}
<tr>
<td class="par"><img src={{parking.image}} alt="img"></td>
<td class="nombre">{{parking.nombre}}</td>
{% if plazas|int >= (totalplazas*30)/100 %}
<td class="num" style="color:#39FF00">
{{plazas}}</td>
{% elif plazas|int < 1%}
<td class="num" style="color:red"><p class="an">COMPLETO</p></td>
{% elif plazas|int <= (totalplazas*10)/100%}
<td class="num" style="color:red">
{{plazas}}</td>
{% else %}
<td class="num" style="color:yellow">
{{plazas}}</td>
{% endif %}
<td class="dir"><img src={{parking.direccion}} alt="img"></td>
</tr>
{% endfor %}
I have tried to use javascript, but when the 10 seconds pass it tells me that the result of {{plazas}} is undefined.我曾尝试使用 javascript,但是当 10 秒过去时,它告诉我 {{plazas}} 的结果未定义。 Any help?
有什么帮助吗?
<script type="text/javascript">
window.onload = setInterval(refresh, 10000);
function refresh(places) {
var elements = document.getElementsByClassName("num");
for(let i = 0; i < elements.length; i++) {
elements[i].innerHTML = places;
}
return elements[i].innerHTML = places;
}
</script>
To refresh it with new text, you can fetch to your own flask route and update the information using setInterval
要用新文本刷新它,您可以获取到您自己的 flask 路由并使用
setInterval
更新信息
HTML HTML
<td id="num" style="color:#39FF00">{{plazas}}</td>
<script>
var element = document.findElementById("num")
async function reload() {
const promise = await fetch('/myroute')
const data = await promise.text()
element.innerHTML = data
}
window.onload = setInterval(reload, 1000)
</script>
Flask Flask
@app.route('/myroute')
def myroute():
# time is just an example
return time.time()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.