简体   繁体   English

使用 Flask 更新 HTML 页面

[英]Updating HTML page using Flask

I am making a info screen for a few 3D printers, I have a rest API where I get the data from.我正在为一些 3D 打印机制作信息屏幕,我有一个休息 API,我可以从中获取数据。 The data should be updated continuously and it will be name and time.数据应不断更新,将是名称和时间。 I however want to just start getting the current name displaying on the HTML site.但是,我只想开始在 HTML 站点上显示当前名称。

I have a working Python program that loops and updating the current name and that output needs to go to a specific field, in this case (HTML file field INPUT NAME 1) I will have multiple printers so needs to be easy to direct the data to the HTML tags... Have been trying flask but I do not know if that is the best for this?我有一个运行的 Python 程序,它循环并更新当前名称,并且输出需要转到特定字段,在这种情况下(HTML 文件字段 INPUT NAME 1)我将有多个打印机,因此需要易于将数据定向到HTML 标签...一直在尝试烧瓶,但我不知道这是否是最好的? python program:蟒蛇程序:

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> ```

  1. In Flask, if you want to send data back from server to the front end, then you have to use the return command.在 Flask 中,如果要将数据从服务器发送回前端,则必须使用return命令。 Right now, all you're doing is sending output (your print command) to your console.现在,您所做的就是将输出(您的打印命令)发送到您的控制台。 When you return values from your server to the front end, you'll have to use Jinja Template to display the values eg当您将值从服务器返回到前端时,您必须使用Jinja Template来显示值,例如

#server code #服务器代码

if response.status_code == 202:
    return render_template("index.htm", INPUT_NAME_1 = response.text)

#html page eg index.htm #html 页面例如 index.htm

....
<td>{{INPUT_NAME_1}}</td>
...

So from the above code (#server code), you're sending a variable called INPUT_NAME_1 back to an html page called index.htm and on that page you get the value of the variable by using the Jinja syntax of double curly brackets ie {{}}因此,从上面的代码(#server 代码),您将一个名为INPUT_NAME_1的变量发送回名为index.htm的 html 页面,并在该页面上通过使用双大括号的 Jinja 语法(即{{}}

  1. Flask is a web framework. Flask 是一个网络框架。 It allows you to build web-based applications which use Python.它允许您构建使用 Python的基于 Web 的应用程序。 Another framework is Django.另一个框架是 Django。 If your target is a web application, then you can use any of them.如果您的目标是 Web 应用程序,那么您可以使用其中任何一个。

Note: Right now, your code doesn't include anything to make it web-based.注意:现在,您的代码不包含任何使其基于 Web 的内容。 You haven't even imported the Flask libraries to your python code and that brings me to my next point.您甚至还没有将 Flask 库导入到您的 Python 代码中,这就引出了我的下一点。

  1. Your question shows you're still missing some basics and continuing down this path will lead to more frustration for you.你的问题表明你仍然缺少一些基础知识,继续沿着这条路走下去会让你更加沮丧。 I would advise that you first understand the basics of Flask eg read materials that introduce you to Flask.我建议您首先了解 Flask 的基础知识,例如阅读介绍 Flask 的材料。 A quick Googling returned the following - https://pymbook.readthedocs.io/en/latest/flask.html , https://flask.palletsprojects.com/en/2.0.x/快速谷歌搜索返回以下内容 - https://pymbook.readthedocs.io/en/latest/flask.html , https://flask.palletsprojects.com/en/2.0.x/

  2. Once you get the basics of Flask, you should then figure out if you want your web application to reside on your local machine or you want to host it on the web.一旦您掌握了 Flask 的基础知识,您就应该弄清楚您是希望将 Web 应用程序驻留在本地计算机上还是希望将其托管在 Web 上。 For web hosting, there are cheap (or free) hosts like python anywhere , heroku , Google App Engine .对于网络托管,有便宜(或免费)的主机,如python 任何地方herokuGoogle App Engine If you decide to go the route of Google App Engine, check out our website where we have a GUI for it如果您决定采用 Google App Engine 的路线,请查看我们的网站,我们为它提供了 GUI

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM