Monday, February 19, 2024

Python: Source Code That Successfully Displays Data in a Table

This is the basic source code that can display data from a MariaDB table.

This is the source code that has successfully displayed data into an HTML table.

This is source for python :

 from flask import Flask, render_template, request  
 from jinja2 import Template, Environment, FileSystemLoader  
 import mariadb  
 import sys  
 aplikasi = Flask(__name__)  
 @aplikasi.route('/')  
 def tabelData1():  
   return render_template("halaman2.html",)  
 @aplikasi.route('/tampilData',methods=['POST'])  
 def tampilkanData():  
   if request.method=='POST':  
     if request.form['tombolPerintah'] == 'Tampilkan Data':  
       koneksi = mariadb.connect(user="steven",password="kucing",host="1.1.3.13",port=3306,database="saham")  
       kursor = koneksi.cursor(dictionary=True)  
       kursor.execute("SELECT idnama1,namaawal1,namaakhir1 FROM nama1")  
       hasil = kursor.fetchall()  
       # print(hasil)  
       # Tampilkan isi Baris pertama dari tabel  
       idnama1 = hasil[0]["idnama1"]  
       namaawal1 = hasil[0]["namaawal1"]  
       namaakhir1 = hasil[0]["namaakhir1"]  
       # Tampilkan isi Baris kedua dari tabel  
       idnama2 = hasil[1]["idnama1"]  
       namaawal2 = hasil[1]["namaawal1"]  
       namaakhir2 = hasil[1]["namaakhir1"]  
       return render_template("halaman2.html", IdNama1 = idnama1, NamaAwal1 = namaawal1, NamaAkhir1 = namaakhir1, IdNama2 = idnama2, NamaAwal2 = namaawal2, NamaAkhir2 = namaakhir2)  
       koneksi.commit()  
       koneksi.close()  
 if __name__ == '__main__':  
   aplikasi.run(host='0.0.0.0',port=8543,debug=True)  

This is source code for HTML page :


 <!DOCTYPE html>  
 <html>  
      <head>  
      </head>  
      <body>  
           <h3>Tabel Menampilkan Data</h3>  
                <form action="http://1.1.3.13:8543/tampilData" method="POST" id="formMenampilkanData" name="formMenampilkanData">  
                          <table>  
                               <thead>  
                                    <tr>  
                                         <th>ID Nama</th>  
                                         <th>Nama Awal</th>  
                                         <th>Nama Akhir</th>  
                                    </tr>  
                               </thead>  
                               <tbody>  
                                         <tr>  
                                              <td>{{ IdNama1}}</td>  
                                              <td>{{ NamaAwal1 }}</td>  
                                              <td>{{ NamaAkhir1 }}</td>  
                                         </tr>  
                                         <tr>  
                                              <td>{{ IdNama2 }}</td>  
                                              <td>{{ NamaAwal2 }}</td>  
                                              <td>{{ NamaAkhir2 }}</td>  
                                         </tr>  
                               </tbody>  
                          </table>  
                          <input type="submit" name="tombolPerintah" value="Tampilkan Data">  
                </form>  
      </body>  
 </html>  

This is a link that contains an example of how to create a loop, in an effort to display data in a table.

Next, we will try to be able to create a table using this looping function.

This source code successfully displays data based on the looping results, but the data is still in the form of a list, not data that can be separated by rows and columns.

We will use the Dictionary concept to collect data from Mariadb. The data is the result of a query process from Mariadb. The Dictionary will be tried to be used to build a table on an HTML page.

This is an example of a link that can help you display data in an HTML table.
This link is suitable for learning in depth about dictionaries.

No comments:

Post a Comment