简体   繁体   中英

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

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

#server code

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

#html page eg 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 {{}}

  1. Flask is a web framework. It allows you to build applications which use Python.应用程序。 Another framework is Django. If your target is a web application, then you can use any of them.

Note: Right now, your code doesn't include anything to make it web-based. You haven't even imported the Flask libraries to your python code and that brings me to my next point.

  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. A quick Googling returned the following - 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. For web hosting, there are cheap (or free) hosts like python anywhere , heroku , Google App Engine . If you decide to go the route of Google App Engine, check out our website where we have a GUI for it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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