Created
May 7, 2026 04:29
-
-
Save fariedrahmat/633b8b7b1ce290d1760f9500dc6d08f2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from flask import Flask, render_template, request, redirect | |
| from flask_mysqldb import MySQL | |
| app = Flask(__name__) | |
| app.config['MYSQL_HOST'] = 'localhost' | |
| app.config['MYSQL_USER'] = 'root' | |
| app.config['MYSQL_PASSWORD'] = '' | |
| app.config['MYSQL_DB'] = 'datasensor' | |
| mysql = MySQL(app) | |
| @app.route('/', methods=['GET', 'POST']) | |
| def index(): | |
| if request.method == "POST": | |
| data = request.form | |
| nama_ruangan = data['ruangan'] | |
| suhu = data['suhu'] | |
| kelembaban = data['kelembaban'] | |
| cur = mysql.connection.cursor() | |
| cur.execute(""" | |
| INSERT INTO sensor_data(nama_ruangan, suhu, kelembaban) | |
| VALUES (%s, %s, %s) | |
| """, (nama_ruangan, suhu, kelembaban)) | |
| mysql.connection.commit() | |
| cur.close() | |
| return redirect('/') | |
| cur = mysql.connection.cursor() | |
| cur.execute("SELECT * FROM sensor_data ORDER BY waktu DESC") | |
| data = cur.fetchall() | |
| cur.close() | |
| return render_template('index.html', data=data) | |
| if __name__ == '__main__': | |
| app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment