Wednesday, February 21, 2024

Python : Exercises on Using Dictionaries in Python

The following are exercises on using Dictionaries in Python that have been completed today.


file14.py

 kamusku = {}  
 kamusku['nama'] = "Steven Nathaniel"  
 kamusku['usia'] = 1  
 print (kamusku)  

file15.py

 kamusku = {}  
 print(kamusku)  
 print(type(kamusku))  

file16.py

 informasi = {'nama':'steven','usia':15,'lokasi':'Athena'}  
 print(informasi)  
 print(type(informasi))  

file17.py

 informasi = dict({'nama':'steven','usia':15,'lokasi':'Athena'})  
 print(informasi)  
 print(type(informasi))  

file18.py

 namaKota = ('Samarinda','Athena','Balikpapan')  
 kamusKu = dict.fromkeys(namaKota)  
 print(kamusKu)  

file19.py

 namaKota = ('Balikpapan','Samarinda','Jakarta')  
 negara = 'Indonesia'  
 kamus = dict.fromkeys(namaKota,negara)  
 print(kamus)  

file20.py

 namaKota = ('Balikpapan','Samarinda','Jakarta')  
 negara = 'indonesia'  
 kamus = dict.fromkeys(namaKota,negara)  
 print(len(kamus))  

file21.py

 tahunPembuatanMobil = {'Kijang': 1997, 'Lancer': 1995, 'Accord': 1999}  
 print(tahunPembuatanMobil.values())  

file22.py

 dataRoti = {'merekRoti': 'Sari Roti', 'tanggalProduksi': 22, 'bulanProduksi': 'Februari'}  
 print(dataRoti['merekRoti'])  

file23.py

 dataRoti = {'merekRoti': 'Sari Roti', 'tanggalProduksi': 22, 'bulanProduksi': 'Februari'}  
 print('merekNasi' in dataRoti)  

file24.py

 dataRoti = {'merekRoti': 'Sari Roti', 'tanggalProduksi': 22, 'bulanProduksi': 'Februari'}  
 print(dataRoti.get('merekRoti'))  

file25.py

 dataRoti = {}  
 dataRoti["namaRoti"] = "Roti Bakar Kepiting"  
 print(dataRoti)  

file26.py

 dataRoti = {}  
 dataRoti['namaRoti'] = "Roti Bakar Mentega"  
 dataRoti['jumlahRoti'] = 10  
 print(dataRoti)  

file27.py

 dataRoti = {'namaRoti':"Roti Bakar Mentega", 'jumlahRoti':9}  
 print(dataRoti)  
 dataRoti['jumlahRoti'] = 11  
 print(dataRoti)  

file28.py

 dataRoti = {'namaRoti':"Roti Goreng Mentega",'kemasanRoti':"Plastik"}  
 print(dataRoti)  
 dataRoti['kemasanRoti'] = "Kayu"  
 print('kemasanRoti' in dataRoti)  

file29.py

 dataRoti = {'merekRoti':"Bondy", 'jumlahLembar':10}  
 dataRoti.update(merekRoti='Holland', jumlahLembar=50, penggunaanKulit='Kulit')  
 print(dataRoti)  

file30.py

 dataRoti = {'merekRoti':'Bondy','bulanPembuatan':'Februari','lokasiToko':'Gunung Sari'}  
 del dataRoti['bulanPembuatan']  
 print(dataRoti)  

This is a source code that almost successfully displays data from the database into a table format.


This is the source code for the Python section.

 import mariadb  
 import sys  
 from jinja2 import Template, Environment, FileSystemLoader  
 from flask import Flask, render_template, request  
 aplikasi = Flask(__name__)  
 @aplikasi.route('/')  
 def tampilkanData():  
   koneksi = mariadb.connect(user="steven",password="kucing",host="1.1.3.7",port=3306,database="saham")  
   kursor = koneksi.cursor(dictionary=True)  
   kursor.execute("SELECT idnama1,namaawal1,namaakhir1 FROM nama1")  
   hasil = kursor.fetchall()  
   koneksi.commit()  
   koneksi.close()  
   return render_template("halaman6.html",hasil=hasil)  
 if __name__ == '__main__':  
   aplikasi.run(host="0.0.0.0", port=8543, debug=True)  

This is the source code for the HTML section.


 <!DOCTYPE html>  
 <html>  
      <head>  
      </head>  
      <body>  
           <h1>Tampilkan Tabel</h1>  
           <table>  
                {% for hasil_item in hasil %}  
                     {% for key, value in hasil_item.items() %}  
                          <tr>  
                               <th>{{ key }}</th>  
                          </tr>  
                          <tr>  
                               <td>{{ value }}</td>  
                          </tr>  
                     {% endfor %}  
                {% endfor %}  
           </table>  
      </body>  
 </html>  

This is the working source code to display the contents of a dictionary from the results of a database query. The data is displayed on an HTML page. This is an example of source code that uses HTML and Python.


HTML :


 <!DOCTYPE html>  
 <html>  
      <head>  
      </head>  
      <body>  
           {{ hasil1[0] }}  
      </body>  
 </html>  

This is an example of an HTML source code that displays the entire contents of a dictionary.


 <!DOCTYPE html>  
 <html>  
      <head>  
      </head>  
      <body>  
           {{ hasil1 }}  
      </body>  
 </html>  

The following are variations of HTML source code that will display different data results.


 <!DOCTYPE html>  
 <html>  
      <head>  
      </head>  
      <body>  
           {% for hasil2 in hasil1 %}  
                {% for key, value in hasil2.items() %}  
                     {{ value }}  
                {% endfor %}  
           {% endfor %}  
      </body>  
 </html>  

 <!DOCTYPE html>  
 <html>  
      <head>  
      </head>  
      <body>  
           {% for hasil2 in hasil1 %}  
                {{ hasil2.keys() }}  
           {% endfor %}  
      </body>  
 </html>  

This is the source code that successfully displays data in an HTML table correctly.


 <!DOCTYPE HTML>  
 <HTML>  
      <HEAD>  
      </HEAD>  
      <BODY>  
           <table>  
                <!----- Ini adalah source code untuk membuat header table ----->  
                     {% if hasil1 %}  
                     <tr>  
                          {% for key in hasil1[0] %}  
                               <th> {{ key }} </th>  
                          {% endfor %}  
                     </tr>  
                     {% endif %}  
                <!--- Ini adalah source code untuk membuat isi tabel -->  
                {% for isitabel in hasil1 %}  
                <tr>  
                     {% for value in isitabel.values() %}  
                          <td>{{ value }}</td>  
                     {% endfor %}  
                </tr>  
                {% endfor %}  
           </table>  
      </BODY>  
 </HTML>  

This is a combination of querying the correct database and displaying the correct table (using CSS).

Python Code :


 from flask import Flask, render_template, request  
 from jinja2 import Template, Environment, FileSystemLoader  
 import mariadb  
 import sys  
 aplikasi = Flask(__name__)  
 @aplikasi.route('/')  
 def tabelData():  
   koneksi = mariadb.connect(user="steven",password="kucing",host="1.1.3.7",port=3306,database="saham")  
   kursor = koneksi.cursor(dictionary=True)  
   kursor.execute("SELECT idnama1,namaawal1,namaakhir1 FROM nama1")  
   hasil = kursor.fetchall()  
   return render_template("halaman11.html",hasil1=hasil)  
 if __name__ == '__main__':  
   aplikasi.run(host="0.0.0.0", port=8543, debug=True)  

HTML Code :


 <!DOCTYPE html>  
 <html>  
 <head>  
      <style>  
           #dataNama {  
                font-family: Arial, Helvetica, sans-serif;  
                border-collapse: collapse;  
                width: 100%;  
           }  
           #dataNama td, #dataNama th {  
                border: 1px solid #ddd;  
                padding: 8px;  
           }  
           #dataNama tr:nth-child(even){background-color: #f2f2f2;}  
           #dataNama tr:hover {background-color: #dataNama}  
           #dataNama th{  
                padding-top: 12px;  
                padding-bottom: 12px;  
                text-align: left;  
                background-color: #04AA6D;  
                color: white;  
           }  
      </style>  
 </head>  
 <body>  
      <table id="dataNama">  
           <!------- Ini adalah source code untuk membuat header table -------->  
                {% if hasil1 %}  
                <tr>  
                     {% for key in hasil1[0] %}  
                          <th> {{ key }} </th>  
                     {% endfor %}  
                </tr>  
                {% endif %}  
                <!--- ini adalah source code untuk membuat isi tabel -->  
                {% for isitabel in hasil1 %}  
                <tr>  
                     {% for value in isitabel.values() %}  
                          <td>{{ value }}</td>  
                     {% endfor %}  
                </tr>  
                {% endfor %}  
      </table>  
 </body>  
 </html>  

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.

Tuesday, February 13, 2024

Wifi Sharing : How to Share a WiFi Connection Using a Cheap USB Dongle Like Tenda and Totolink

Here are the steps on how to share internet from a laptop or PC using a USB WiFi Dongle. This will allow other devices to connect to the internet as well.


netsh wlan set hostednetwork mode=allow ssid=nasipecel key=makan1234 keyusage=temporary
netsh wlan start hostednetwork
netsh wlan show hostednetwork
netsh wlan set hostednetwork mode=disallow
netsh wlan stop hostednetwork
netsh wlan show settings
netsh wlan show profiles
netsh wlan delete profile name="swisscom"
netsh wlan delete profile name=* i=*
netsh wlan show drivers
https://stackoverflow.com/questions/18182084/cant-start-hostednetwork
https://support.airserver.com/support/solutions/articles/43000532725-how-can-i-enable-the-wireless-autoconfig-service-the-wireless-autoconfig-service-wlansvc-is-not-
https://youtu.be/2pvK-6321ig?si=tKonB-Tn-LIvcTTX

Python Flask : Getting Ready to Make a Table

The source code below is to start finding ways to create a table on a web page. The data for the table is taken from a MariaDB database query.


 from jinja2 import Template, Environment, FileSystemLoader  
 from flask import Flask, render_template, request, json, jsonify  
 import mariadb  
 import sys  
 aplikasi = Flask(__name__)  
 @aplikasi.route('/')  
 def formUtama():  
   return render_template("halaman1.html")  
 @aplikasi.route('/tampilData',methods=['POST'])  
 def tampilkanData():  
   if request.method=='POST':  
     koneksi = mariadb.connect(user="steven",password="kucing",host="1.1.3.9",port=3306,database="saham")  
     kursor= koneksi.cursor(dictionary=True)  
     kursor.execute("SELECT kodedata,tanggalpendataan,kodebarang,NIP,namabarang,kodebagian,namadivisi,merekprinter,serialprinter,macaddress,jenistinta,namapengguna FROM daftartintaprinter2")  
     hasil = kursor.fetchall()  
     # di bawah ini untuk menampilkan baris data yang urutan 1  
     kodedata1 = hasil[0]["kodedata"]  
     tanggalpendataan1 = hasil[0]["tanggalpendataan"]  
     kodebarang1 = hasil[0]["kodebarang"]  
     nip1 = hasil[0]["NIP"]  
     namabarang1 = hasil[0]["namabarang"]  
     kodebagian1 = hasil[0]["kodebagian"]  
     namadivisi1 = hasil[0]["namadivisi"]  
     merekprinter1 = hasil[0]["merekprinter"]  
     serialprinter1 = hasil[0]["serialprinter"]  
     macaddress1 = hasil[0]["macaddress"]  
     jenistinta1 = hasil[0]["jenistinta"]  
     namapengguna1 = hasil[0]["namapengguna"]  
     # di bawah ini untuk menampilkan baris data yang urutan 2  
     kodedata2 = hasil[1]["kodedata"]  
     tanggalpendataan2 = hasil[1]["tanggalpendataan"]  
     kodebarang2 = hasil[1]["kodebarang"]  
     nip2 = hasil[1]["NIP"]  
     namabarang2 = hasil[1]["namabarang"]  
     kodebagian2 = hasil[1]["kodebagian"]  
     namadivisi2 = hasil[1]["namadivisi"]  
     merekprinter2 = hasil[1]["merekprinter"]  
     serialprinter2 = hasil[1]["serialprinter"]  
     macaddress2 = hasil[1]["macaddress"]  
     jenistinta2 = hasil[1]["jenistinta"]  
     namapengguna2 = hasil[1]["namapengguna"]  
     koneksi.commit()  
     koneksi.close()  
     return render_template("halaman3.html", kodeData1 = kodedata1, tanggalPendataan1 = tanggalpendataan1, kodeBarang1 = kodebarang1, NIP1 = nip1, namaBarang1 = namabarang1, kodeBagian1 = kodebagian1, namaDivisi1 = namadivisi1, merekPrinter1 = merekprinter1, serialPrinter1 = serialprinter1, macAddress1 = macaddress1, jenisTinta1 = jenistinta1, namaPengguna1 = namapengguna1, kodeData2 = kodedata2, tanggalPendataan2 = tanggalpendataan2, kodeBarang2 = kodebarang2, NIP2 = nip2, namaBarang2 = namabarang2, kodeBagian2 = kodebagian2, namaDivisi2 = namadivisi2, merekPrinter2 = merekprinter2, serialPrinter2 = serialprinter2, macAddress2 = macaddress2, jenisTinta2 = jenistinta2, namaPengguna2 = namapengguna2 )  
 if __name__ == '__main__':  
   aplikasi.run(host='0.0.0.0',port=8543,debug=True)  

Wednesday, February 7, 2024

Getting Started with PHP Programming

Getting Started with PHP Programming

Resuming Our PHP Learning Journey in Laravel. Below is a simple source code example to get you started with learning PHP programming again.