【发布时间】:2021-06-22 12:58:42
【问题描述】:
我正在为几台 3D 打印机制作信息屏幕,我有一个休息 API 可以从中获取数据。数据应该不断更新,它将是名称和时间。但是,我只想开始在 HTML 站点上显示当前名称。
我有一个正在运行的 Python 程序,它循环和更新当前名称,并且输出需要转到特定字段,在这种情况下(HTML 文件字段 INPUT NAME 1)我将有多个打印机,因此需要易于指导HTML标签的数据......一直在尝试烧瓶,但我不知道这是否最适合这个? python程序:
import sched, time
from time import time, sleep
import flask
from flask import Flask, redirect, url_for, render_template, request, flash
import requests
app=Flask(__name__,template_folder='templates')
#printer-S3-nr1
response_ums3_1 = requests.get("http://192.168.50.203/api/v1/print_job/name")
response_ums5_1 = requests.get("http://192.168.50.201/api/v1/print_job/name")
@app.route("/")
def profile():
while (True):
sleep(10 - time()%10)
#ULTIMAKER S3 NR 1
if response_ums3_1.status_code == 404:
return render_template("server.html", s3name1 = "No Prints Running")
if response_ums3_1.status_code == 200:
return render_template("server.html", s3name1 = response_ums3_1.text.strip('"'))
#ULTIMAKER S3 NR 1
#ULTIMAKER S5 NR 1
if response_ums5_1.status_code == 404:
return render_template("server.html", s5name1 = "No Prints Running")
if response_ums5_1.status_code == 200:
return render_template("server.html", s5name1 = response_ums5_1.text.strip('"'))
#ULTIMAKER S5 NR 1
if __name__ == "__main__":
app.run(debug=True)
```
<!DOCTYPE html>
<html>
<head>
<style>
#customers {
font-family: Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}
#customers td,
#customers th {
border: 1px solid #ddd;
padding: 8px;
}
#customers tr:nth-child(even) {
background-color: #f2f2f2;
}
#customers tr:hover {
background-color: #ddd;
}
#customers th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #1b77f7;
color: white;
}
</style>
</head>
<H1 align="center"> Printer Status </H1>
<body>
<table id="customers">
<tr>
<th>Skrivare</th>
<th>Färdig</th>
<th>Namn</th>
<th>Video</th>
</tr>
<tr>
<td>Ultimaker-S5-nr1</td>
<td>INPUT TIME</td>
<td>{{s5name1}}</td>
<td><img src="http://192.168.50.201:8080/?action=stream" alt="Ultimaker-S5-nr1" style="width:202.5px;height:151.85px;" </td>
</tr>
<tr>
<td>Ultimaker S3 nr1</td>
<td>INPUT TIME</td>
<td>{{s3name1}}</td>
<td><img src="http://192.168.50.203:8080/?action=stream" alt="Ultimaker S3 nr1" style="width:202.5px;height:151.85px;" </td>
</tr>
<tr>
</table>
</body>
</html>
```
【问题讨论】: