Wednesday, April 17, 2024

Python : Using Modules to Consume JSON Data from RESTful APIs

This is the source code for creating a RESTful API server that will send data in JSON format over the network.
 from flask import Flask, render_template, request, url_for, redirect, jsonify  
 from jinja2 import Template, Environment, FileSystemLoader  
 import csv  
 import sys  
 import json  
 aplikasi = Flask(__name__)  
 @aplikasi.route('/data')  
 # ini adalah source code untuk membuat server RESTFUL API yang akan mengirimkan data dalam bentuk JSON di jaringan  
 def halaman1():  
   data = {}  
   with open('/home/steven/proyekFlask/latihan15/templates/file2.csv','r') as file:  
     pembacaCSV = csv.DictReader(file)  
     for rows in pembacaCSV:  
       key = rows['nomer']  
       data[key] = rows  
     return jsonify(data)  
 if __name__ == '__main__':  
   aplikasi.run(host="0.0.0.0",port=8543,debug=True)  

The following source code is used to consume JSON data sent by the RESTful API server. The RESTful API server is also created using the Python language.


 from flask import Flask  
 from flask_restful import Resource, Api  
 from flask_cors import CORS  
 import requests  
 aplikasi = Flask(__name__)  
 CORS(aplikasi)  
 # ini adalah program komputer untuk mengkonsumsi data JSON yang di kirimkan oleh server lain  
 @aplikasi.route('/', methods=['GET'])  
 def tampilData():  
   r = requests.get('http://1.1.3.4:8543/data')  
   return r.json()  
 if __name__ == '__main__':  
   aplikasi.run(host="0.0.0.0",port=8544,debug=True)  

Next, we will learn everything about flask_restful. So that we can learn various things related to RESTful API.

Thursday, March 28, 2024

Python : Learning to Connect Between the OS Module and Flask

We will start to integrate Flask with other Python programming code, in this case between Flask and the OS module.


For the first one, we learn to use the OS module.


 import os  
 alamatFolder = os.getcwd()  
 print("Lokasi Folder Saat ini : ", alamatFolder)  


 from flask import Flask, render_template, request, url_for, redirect  
 from jinja2 import Template, Environment, FileSystemLoader  
 import mariadb  
 import sys  
 import os  
 aplikasi = Flask(__name__)  
 @aplikasi.route('/')  
 def halaman1():  
   return render_template("halaman4.html")  
 if __name__ == '__main__':  
   aplikasi.run(host="0.0.0.0",port=8543,debug=True)  

This is the initial source code for displaying a message box from JavaScript, using a server made with Flask.


 <!DOCTYPE html>  
 <html>  
      <head>  
           <meta name="viewport" content="width=device-width, initial-scale=1">  
           <style>  
                .button {  
                     background-color: #04AA6D;  
                     border: none;  
                     color: white;  
                     padding: 15px 32px;  
                     text-align: center;  
                     text-decoration: none;  
                     display: block;  
                     font-size: 16px;  
                     margin: 10px 10px;  
                     cursor: pointer;  
                     -webkit-transition-duration: 0.4s;  
                     transition-duration: 0.4s;  
                }  
                .button2:hover {  
                     box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19)  
                }  
           </style>  
      </head>  
      <body>  
           <button class="button button2" type="button" onclick="tampilkanPesan()">Uji Coba OS dan Flask</button>  
           <script>  
                function tampilkanPesan() {  
                     alert("Menampilkan Kotak Pesan")  
                }  
           </script>  
      </body>  
 </html>  

We will try to build data delivery over the network using a RESTful API. In building the server and backend RESTFUL API, we use the Python programming language. We will use JavaScript to build the UI. JavaScript will also be used to consume JSON data sent through the RESTFUL API. JavaScript will be used to build AJAX technology.


 import json  
 from flask import Flask  
 import mariadb  
 import sys  
 aplikasi = Flask(__name__)  
 @aplikasi.route('/')  
 # di bawah ini adalah source code untuk mengirimkan data dalam bentuk JSON ke jaringan dan ditampilkan di browser  
 def restApi1():  
   return json.dumps({'nama':'alesandro','e-mail':'alesandro@toto.com'})  
 if __name__ == '__main__':  
   aplikasi.run(host="0.0.0.0",port=8543,debug=True)  

The following is the source code for reading a text file. The results of the reading will then be displayed on the web page. Alternatively, the results of the reading can be sent using a RESTful API. Once the reading results are converted to JSON, they will be displayed on the web page using AJAX.


Python
 from flask import Flask, render_template, request, url_for, redirect  
 from jinja2 import Template, Environment, FileSystemLoader  
 import mariadb  
 import sys  
 aplikasi = Flask(__name__)  
 @aplikasi.route('/')  
 def halaman1():  
   file = open('/home/steven/proyekFlask/latihan15/templates/file1.txt','r')  
   angka1 = file.read()  
   file.close()  
   return render_template("halaman5.html", angka1 = angka1)  
 if __name__ == '__main__':  
   aplikasi.run(host="0.0.0.0",port=8543,debug=True)  

HTML
 <!DOCTYPE html>  
 <html>  
      <head>  
      </head>  
      <body>  
           <p>{{ angka1 }}<p>  
      </body>  
 </html>  

Wednesday, March 27, 2024

JavaScript : Combining Python with Javascript to Run Python Source Code on an HTML Page

We are currently learning how to combine Python and Javascript programming code to run a command on a web page.
 from flask import Flask, render_template, request, url_for, redirect  
 from jinja2 import Template, Environment,FileSystemLoader  
 import mariadb  
 import sys  
 aplikasi = Flask(__name__)  
 @aplikasi.route('/')  
 def halaman1():  
   return render_template("halaman2.html")  
 if __name__ == '__main__':  
   aplikasi.run(host="0.0.0.0",port=8543,debug=True)  

 <!DOCTYPE html>  
 <html>  
      <head>  
           <meta name="viewport" content="width=device-width, initial-scale=1">  
           <style>  
                .button {  
                     background-color: #04AA6D;  
                     border: none;  
                     color: white;  
                     padding: 15px 32px;  
                     text-align: center;  
                     text-decoration: none;  
                     display: block;  
                     font-size: 16px;  
                     margin: 10px 10px;  
                     cursor: pointer;  
                     -webkit-transition-duration: 0.4s;  
                     transition-duration: 0.4s;  
                }  
                .button2:hover {  
                     box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19);  
                }  
           </style>  
      </head>  
      <body>  
           <button class="button button2" onclick="document.getElementById('cetakTanggal').innerHTML = Date()">Uji Coba Javascript 1</button>  
           <p id="cetakTanggal"></p>  
      </body>  
 </html>  

Tuesday, March 26, 2024

Python : Basic Source Code to Create a Web Page Link to Display a Table

This source code will be further developed to display a table containing data from MariaDB.


This is the Python source code to connect two HTML pages through a web link :


 from flask import Flask, render_template, request, url_for, redirect  
 from jinja2 import Template, Environment, FileSystemLoader  
 import mariadb  
 import sys  
 aplikasi = Flask(__name__)  
 @aplikasi.route('/')  
 def halaman1():  
   return render_template("halaman1.html")  
 @aplikasi.route('/halaman2')  
 def halaman2():  
   return render_template("halaman2.html")  
 if __name__ == '__main__':  
   aplikasi.run(host='0.0.0.0',port=8543,debug=True)  

This is the source code for the first HTML page :


 <!DOCTYPE html>  
 <html>  
      <head>  
           <meta name="viewport" content="width=device-width, initial-scale=1">  
           <style>  
           </style>  
      </head>  
      <body>  
           <h1>Halaman 1</h1>  
                <a href="{{ url_for('halaman2')}}">Halaman 2</a>  
      </body>  
 </html>  

This is the source code for the second HTML page :


 <!DOCTYPE html>  
 <html>  
      <head>  
           <meta name="viewport" content="width=device-width, initial-scale=1">  
      </head>  
      <body>  
           <h1>Halaman 2</h1>  
           <a href="{{ url_for('halaman1')}}">Halaman 1</a>  
      </body>  
 </html>  

Wednesday, March 20, 2024

Python : Python Source Code for Menu Page, Data Entry Form, and Table Page for Data Display

This is the menu page to display the data input menu, the data edit menu, and the menu to display a table containing the data.


This is the source code for the menu page. The menu page is at the forefront and serves as a gateway to access various features in the application.


 <!DOCTYPE html>  
 <html>  
      <head>  
           <meta name="viewport" content="width=device-width, initial-scale=1">  
           <style>  
                .vertical-menu {  
                     width: 200px;  
                }  
                .vertical-menu a{  
                     background-color: #eee;  
                     color: black;  
                     display: block;  
                     padding: 12px;  
                     text-decoration: none;  
                }  
                .vertical-menu a:hover {  
                     background-color: #ccc;  
                }  
                .vertical-menu a.active{  
                     background-color: #04AA6D;  
                     color: white;  
                }  
           </style>  
      </head>  
      <body>  
           <h1>Vertical Menu</h1>  
           <div class="vertical-menu">  
                <a href="{{ url_for('halaman1')}}" class="active">Halaman Menu</a>  
                <a href="{{ url_for('halaman2')}}">Halaman Simpan Data</a>  
                <a href="{{ url_for('halaman3')}}">Halaman Edit Data</a>  
                <a href="{{ url_for('halaman4')}}">Halaman Tabel Data</a>  
           </div>  
      </body>  
 </html>  

This is the source code for the data input page. There is also a button to display a table containing the data stored in the MariaDB table. There is also a button to return to the Menu page.


 <!DOCTYPE html>  
 <html>  
      <head>  
           <meta name="viewport" content="width=device-width, initial-scale=1">  
           <style>  
                input[type=text], select {  
                     width: 100%;  
                     padding: 12px 20px;  
                     margin: 8px 0p;  
                     display: inline-block;  
                     border: 1px solid #ccc;  
                     border-radius: 4px;  
                     box-sizing: border-box;  
                }  
                input[type=submit] {  
                     width: 100%;  
                     background-color: #4CAF50;  
                     color: white;  
                     padding: 14px 20px;  
                     margin: 8px 0;  
                     border: none;  
                     border-radius: 4px;  
                     cursor: pointer;  
                }  
                input[type=submit]:hover {  
                     background-color: #45a049;  
                }  
                div {  
                     border-radius: 5px;  
                     background-color: #8ecaed;  
                     padding: 20px;  
                }  
           </style>  
      </head>  
      <body>  
           <h1>Halaman Input Data</h1>  
           <div>  
                <form action="http://1.1.3.9:8543/halaman2" method="POST" id="formSimpanDataSurat" name="formSimpanDataSurat">  
                     <label for="kodedatanomersurat1">Kode Data Nomer Surat</label>  
                     <input type="text" id="kodedatanomersurat1" name="kodedatanomersurat1" placeholder="Isikan Kode Data Nomer Surat">  
                     <label for="tanggalsurat1">Tanggal Surat</label>  
                     <input type="text" id="tanggalsurat1" name="tanggalsurat1" placeholder="Isikan Tanggal Surat">  
                     <label for="nomersurat1">Nomer Surat</label>  
                     <input type="text" id="nomersurat1" name="nomersurat1" placeholder="Isikan Nomer Surat">  
                     <label for="perihalsurat1">Perihal Surat</label>  
                     <input type="text" id="perihalsurat1" name="perihalsurat1" placeholder="Isikan Perihal Surat">  
                     <input type="submit" name="tombolPerintah" value="Simpan Data">  
                     <input type="submit" name="tombolPerintah" value="Tampilkan Tabel Data">  
                     <input type="submit" name="tombolPerintah" value="Kembali Ke Halaman Menu">  
                </form>  
           </div>  
      </body>  
 </html>  

This is the HTML source code to display a table containing data from the MariaDB server.


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

This is the source code written in Python. This source code is used to control and run the features on the three web pages above.

 from flask import Flask, render_template, request, url_for, redirect  
 from jinja2 import Template, Environment, FileSystemLoader  
 import mariadb  
 import sys  
 aplikasi = Flask(__name__)  
 @aplikasi.route('/')  
 def halaman1():  
   return render_template("halaman1.html")  
 @aplikasi.route('/halaman2', methods=['GET','POST'])  
 def halaman2():  
   if request.method == 'POST':  
     if request.form['tombolPerintah'] == 'Simpan Data':  
       kodeDataNomerSurat1 = request.form.get('kodedatanomersurat1')  
       tanggalSurat1 = request.form.get('tanggalsurat1')  
       nomerSurat1 = request.form.get('nomersurat1')  
       perihalSurat1 = request.form.get('perihalsurat1')  
       koneksi = mariadb.connect(user="steven",password="kucing",host="1.1.3.9",port=3306,database="saham")  
       kursor = koneksi.cursor()  
       kursor.execute("INSERT INTO nomersurat(kodedatanomersurat,tanggalsurat,nomersurat,perihalsurat) VALUES (%s,%s,%s,%s)",(kodeDataNomerSurat1,tanggalSurat1,nomerSurat1,perihalSurat1))  
       koneksi.commit()  
       koneksi.close()  
       return redirect(url_for('halaman1'))  
     # elif request.form['tombolPerintah'] == 'Tampilkan Tabel Data':  
     elif request.form['tombolPerintah'] == 'Tampilkan Tabel Data':  
       koneksi = mariadb.connect(user="steven",password="kucing",host="1.1.3.9",port=3306,database="saham")  
       kursor = koneksi.cursor(dictionary=True)  
       kursor.execute("SELECT kodedatanomersurat,tanggalsurat,nomersurat,perihalsurat FROM nomersurat")  
       hasil = kursor.fetchall()  
       return render_template("halaman4.html",hasil1=hasil)  
     # elif request.form['tombolPerintah'] == 'Kembali Ke Halaman Menu':  
     elif request.form['tombolPerintah'] == 'Kembali Ke Halaman Menu':  
       return redirect(url_for('halaman1'))  
   return render_template("halaman2.html")  
 @aplikasi.route('/halaman3')  
 def halaman3():  
   return render_template("halaman3.html")  
 @aplikasi.route('/halaman4')  
 def halaman4():  
   return render_template("halaman4.html")  
 if __name__ == '__main__':  
   aplikasi.run(host="0.0.0.0",port=8543,debug=True)  

Friday, March 15, 2024

Python : Source Code of The Menu For Accessing The Data Entry Page

This is the HTML source code for a vertical menu. The menu contains a list of actions that can be performed on the data.


 <!DOCTYPE html>  
 <html>  
      <head>  
           <meta name="viewport" content="width=device-width, initial-scale=1">  
           <style>  
                .vertical-menu{  
                     width: 200px;  
                }  
                .vertical-menu a{  
                     background-color: #eee;  
                     color: black;  
                     display: block;  
                     padding: 12px;  
                     text-decoration: none;  
                }  
                .vertical-menu a:hover {  
                     background-color: #ccc;  
                }  
                .vertical-menu a.active {  
                     background-color: #04AA6D;  
                     color: white;  
                }  
           </style>  
      </head>  
      <body>  
           <h1>Vertical Menu</h1>  
           <div class="vertical-menu">  
                <a href="{{ url_for('halaman1')}}" class="active">Halaman Menu</a>  
                <a href="{{ url_for('halaman2')}}">Halaman Simpan Data</a>  
                <a href="#">Halaman 3</a>  
                <a href="#">Halaman 4</a>  
                <a href="#">Halaman 5</a>  
           </div>  
      </body>  
 </html>  

This is the HTML source code for creating a form that will be used to fill data into the Mariadb server.


 <!DOCTYPE html>  
 <html>  
 <head>  
      <style>  
           input[type=text], select{  
                width: 100%;  
                padding: 12px 20px;  
                margin: 8px 0;  
                display: inline-block;  
                border: 1px solid #ccc;  
                border-radius: 4px;  
                box-sizing: border-box;  
           }  
           input[type=submit] {  
                width: 100%;  
                background-color: #4CAF50;  
                color: white;  
                padding: 14px 20px;  
                margin: 8px 0;  
                border: none;  
                border-radius: 4px;  
                cursor: pointer;  
           }  
           input[type=submit]:hover{  
                background-color: #45a049;  
           }  
           div{  
                border-radius: 5px;  
                background-color: #8ecaed;  
                padding: 20px;  
           }  
      </style>  
 </head>  
 <body>  
      <h1>Halaman Input Data</h1>  
      <di>  
           <form action="http://1.1.3.10:8543/halaman2" method="POST" id="formSimpanDataSurat" name="formSimpanDataSurat">  
                <label for="kodedatanomersurat1">Kode Data Nomer Surat</label>  
                <input type="text" id="kodedatanomersurat1" name="kodedatanomersurat1" placeholder="Isikan Kode Data Nomer Surat">  
                <label for="tanggalsurat1">Tanggal Surat</label>  
                <input type="text" id="tanggalsurat1" name="tanggalsurat1" placeholder="Isikan Tanggal Surat">  
                <label for="nomersurat1">Nomer Surat</label>  
                <input type="text" id="nomersurat1" name="nomersurat1" placeholder="Isikan Nomer Surat">  
                <label for="perihalsurat1">Perihal Surat</label>  
                <input type="text" id="perihalsurat1" name="perihalsurat1" placeholder="Isikan Perihal Surat">  
                <input type="submit" name="tombolPerintah" value="Simpan Data">  
                <input type="submit" name="tombolPerintah" value="Kembali Ke Halaman Menu">  
           </form>  
      </div>  
 </body>  
 </html>  

This is the Python source code for displaying a menu, data entry page, and executing the data entry command from the form to the Mariadb database.


 from flask import Flask, render_template, request, url_for, redirect  
 from jinja2 import Template, Environment, FileSystemLoader  
 import mariadb  
 import sys  
 aplikasi = Flask(__name__)  
 @aplikasi.route('/')  
 def halaman1():  
   return render_template("halaman1.html")  
 @aplikasi.route('/halaman2', methods=['GET','POST'])  
 def halaman2():  
   if request.method == 'POST':  
     if request.form['tombolPerintah'] == 'Simpan Data':  
       kodeDataNomerSurat1 = request.form.get('kodedatanomersurat1')  
       tanggalSurat1 = request.form.get('tanggalsurat1')  
       nomerSurat1 = request.form.get('nomersurat1')  
       perihalSurat1 = request.form.get('perihalsurat1')  
       koneksi = mariadb.connect(user="steven",password="kucing",host="1.1.3.10",port=3306,database="saham")  
       kursor = koneksi.cursor()  
       kursor.execute("INSERT INTO nomersurat(kodedatanomersurat,tanggalsurat,nomersurat,perihalsurat) VALUES (%s,%s,%s,%s)",(kodeDataNomerSurat1,tanggalSurat1,nomerSurat1,perihalSurat1))  
       koneksi.commit()  
       koneksi.close()  
       return redirect(url_for('halaman1'))  
     elif request.form['tombolPerintah'] == 'Kembali Ke Halaman Menu':  
       return redirect(url_for('halaman1'))  
     # return redirect(url_for('halaman1'))  
   return render_template('halaman2.html')  
 if __name__ == '__main__':  
   aplikasi.run(host="0.0.0.0",port=8543,debug=True)  

Thursday, March 14, 2024

Python : This is An Example of Source Code That Inspires How to Create Menus and Links.

This is the source code for displaying a menu that already uses CSS. This source code example is taken from a source on the internet.


HTML Source Code


HTML Source Code For Page 1


 <!DOCTYPE html>  
 <html>  
      <head>  
      </head>  
      <body>  
           <p><a href="{{ url_for('halaman2')}}">Coba anda lihat form ini</a></p>  
      </body>  
 </html>  

HTML Source Code For Page 2


 <!DOCTYPE html>  
 <html>  
      <head>  
      </head>  
      <body>  
           <form method="post">  
                <button type="submit">Uji Coba</button>  
           </form>  
      </body>  
 </html>  

Python Code :

 from flask import Flask, request, url_for, redirect, render_template  
 from jinja2 import Template, Environment, FileSystemLoader  
 import mariadb  
 import sys  
 aplikasi = Flask(__name__)  
 @aplikasi.route('/')  
 def halaman1():  
   return render_template('halaman1.html')  
 @aplikasi.route('/halaman2', methods=['GET','POST'])  
 def halaman2():  
   if request.method == 'POST':  
     # lakukan sesuatu ketika form di kumpulkan  
     # dialihkan untuk mengakhiri penanganan POST  
     # pengalihan bisa ke route yang sama atau ke sesuatu yang lain  
     return redirect(url_for('halaman1'))  
   # tampilkan form nya, form nya tidak di kumpulkan  
   return render_template('halaman2.html')  
 if __name__ == '__main__':  
   aplikasi.run(host='0.0.0.0',port=8543,debug=True)  

Python : Learning How To Create A Menu And Link It To Routes To Open Different Pages.

This is the source code for an HTML page containing a menu


HTML Code :

 <!DOCTYPE html>  
 <html>  
      <head>  
           <meta name="viewport" content="width=device-width, initial-scale=1">  
           <style>  
                .vertical-menu{  
                     width: 200px;  
                }  
                .vertical-menu a{  
                     background-color: #eee;  
                     color: black;  
                     display: block;  
                     padding: 12px;  
                     text-decoration: none;  
                }  
                .vertical-menu a:hover {  
                     background-color: #ccc;  
                }  
                .vertical-menu a.active {  
                     background-color: #04AA6D;  
                     color: white;  
                }  
           </style>  
      </head>  
      <body>  
           <h1>Vertical Menu</h1>  
           <div class="vertical-menu">  
                <a href="#" class="active">Halaman Utama</a>  
                <a href="#">Halaman 1</a>  
                <a href="#">Halaman 2</a>  
                <a href="#">Halaman 3</a>  
                <a href="#">Halaman 4</a>  
           </div>  
      </body>  
 </html>  

Python Code :


 from flask import Flask, render_template, request  
 from jinja2 import Template, Environment, FileSystemLoader  
 aplikasi = Flask(__name__)  
 @aplikasi.route('/')  
 def tampilHalaman():  
   return render_template("halaman1.html")  
 if __name__ == '__main__':  
   aplikasi.run(host="0.0.0.0",port=8543,debug=True)  

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.

Tuesday, January 30, 2024

Flask: Printing Database Query Results to an HTML Table

In this example, I am trying to query a MariaDB database. Then I am trying to display the results on a web page. However, for the first step, I will try to display the query results to the terminal. The following is the source code for printing database query results to the terminal:


 import mariadb  
 import sys  
 koneksi = mariadb.connect(user="steven",password="kucing",host="1.1.3.6",port=3306,database="saham")  
 kursor = koneksi.cursor(dictionary=True)  
 kursor.execute("SELECT idnama1,namaawal1,namaakhir1 FROM nama1")  
 hasil = kursor.fetchall()  
 print(hasil[0])  

The following source code successfully prints the query results from the MariaDB database to the terminal.


 import mariadb  
 import sys  
 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()  
 print(hasil[1])  


 import mariadb  
 import sys  
 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()  
 idnama1A = hasil[0]["idnama1"]  
 print(idnama1A)  

The following is the source code for rendering an HTML page using Flask.



Thursday, January 11, 2024

Python 3 : The Class

 class kelas1:  
   def __init__(self, nama, usia):  
     self.nama = nama  
     self.usia = usia  
   def __str__(self):  
     return "Tampilkan nama : %s , usia : %s" % (self.nama, self.usia)  
 isian = kelas1("Steven", 17)  
 print(isian)  

This is the Initial Source Code for Learning About Classes in Python Programming.

Understanding classes in Python programming will be useful for understanding how to create tables using Flask Table.

A class is a group of functions.

We want to learn how to inherit in classes in Python programming. We can only inherit within the same class, which is inheritance between functions.