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>
No comments:
Post a Comment