Postgres to HTML

Issue

so, here is the issue, i have a database with Postgres, the database is local.
i have a WebSite (local too) and i need to bring my registers on the database and display it on cards on my page, but actually i don’t have any idea of how i can do this, i’m coding on Python and everything is on python my code is like
Python code:

def home():
    #HERE IS MY ARDUINO
    time.sleep(1)
    arduinoData = arduino.readline().decode('ascii')
    arduino.inWaiting()
    print (arduinoData)
    arduinoData = arduinoData[:2]

    #HERE IS THE DB
    connection= psycopg2.connect(connection_address)
    cursor = connection.cursor()

    if (request.method == 'POST'):
        serie=request.form.get('serie')
        print(serie)
        altaClanmp= "INSERT INTO registros (serie, carril) VALUES ('"+ serie +"', '"+ arduinoData +"');"
        print(altaClanmp)
        cursor.execute(altaClanmp)
        connection.commit()
        connection.close()
        arduinoData = arduino.flushInput()
    return render_template("home.html")

My database Struct is very simple, just a couple of columns with info like ID, Serial_Number, Date

And my HTML cards looks like

<div class="container-custom">
  <div class="row row-cols-2 row-cols-md-12 g-4" >
    <div class="col-sm-auto">
      <div class="card" style="width: 10rem;">
        <div class="progress" >
            <div class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 50%">75%</div>
        </div>
        <div>
            <p>TIMER</p>
        </div>
        <div class="card-body">
          <p class="card-text">DATA DISPLAY</p>
        </div>
      </div>
    </div>
</div>

Solution

You have two problems here. First, you don’t have any SELECT queries on the database, only an insert, so you never see the existing data. You should add a SELECT query in python, and print the data, to make sure it is what you expect.

Second, you don’t pass any data to your HTML page.

To review how to pass data to the HTML page, see this: https://riptutorial.com/flask/example/5303/render-template-usage

To give a brief explanation, the named arguments/params of render_template are data accessible on the page, and can be accessed in the HTML with {{ VARIABLE_NAME }}. In your use case, it will probably be easier to add an object, but start with adding a simple string to make sure it’s working.

These two code blocks will hopefully make it clear:

from flask import Flask, render_template


app = Flask(__name__)

@app.route("/")
def index():
    pagetitle = "HomePage"
    return render_template("index.html",
                            mytitle=pagetitle,
                            mycontent="Hello World")
<html>
    <head>
        <title>{{ mytitle }}</title>
    </head>
    <body>
        <p>{{ mycontent }}</p>
    </body>
</html>

Answered By – ᴓᴓᴓ

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published