-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadtemp.py
39 lines (31 loc) · 1.2 KB
/
readtemp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import mysql.connector
import streamlit as st
# Koneksi ke database
def get_temperature():
try:
# Menghubungkan ke database MySQL
connection = mysql.connector.connect(
host="localhost",
user="root", # Ganti dengan username database Anda
password="", # Ganti dengan password database Anda
database="monitorair" # Nama database
)
cursor = connection.cursor()
# Menjalankan query untuk membaca nilai temperature dari tb_kualitas_air
cursor.execute("SELECT temperature FROM tb_kualitas_air ORDER BY id DESC LIMIT 1")
result = cursor.fetchone() # Mengambil data terbaru
if result:
return result[0] # Mengembalikan nilai temperature
else:
return "Data tidak tersedia"
except mysql.connector.Error as err:
return f"Terjadi kesalahan: {err}"
finally:
if connection.is_connected():
cursor.close()
connection.close()
# Streamlit untuk antarmuka
st.title("Monitoring Kualitas Air")
# Ambil dan tampilkan nilai temperature
temperature = get_temperature()
st.header(f"Temperature: {temperature} °C")