I've followed multiple tutorials to write from sensors to a database and then query this within a webpage.
What I'd like to do is add more queries (so as well as showing temp and humidity, show, say high/low for last 24 hours).
I can do the queries fine (one at a time) - but how do I structure the webpage for more than one query at a time? It just dumps results in rows atm from a single query.
Code: Select all
from flask import Flask, render_template, request
import mysql.connector as mariadb
app = Flask(__name__)
@app.route('/')
def list():
conn = mariadb.connect(user='user',password='pw',database='weather')
cur = conn.cursor()
cur.execute('SELECT TempSensor1, HumidSensor1, ts FROM readings ORDER BY readingID LIMIT 1;')
rows = cur.fetchall()
return render_template("temphumid.html", rows = rows)
if __name__ == '__main__':
app.run(debug = True, host = '0.0.0.0')
Code: Select all
<html>
<body>
<h1>Temperature and Humidity<h1>
<table border = 0>
{% for row in rows%}
<tr><td>Current Temperature:<td>{{ row[0]}}<td>degC
<tr><td>Current Humidity:<td>{{ row[1]}}<td>Rel%
<tr><td>Last Reading:<td>{{ row[2]}}
{% endfor %}
</table>
</body>
</html>
SELECT TempSensor1 FROM readings ORDER BY TempSensor1 DESC LIMIT 1;
How do I query this in the app and call it in the template?!
Many thanks anyone!
Will