pythonwebsitewerkt
This commit is contained in:
56
app.py
Normal file
56
app.py
Normal file
@@ -0,0 +1,56 @@
|
||||
from flask import Flask, render_template, jsonify
|
||||
import requests
|
||||
import mysql.connector # Import MySQL connector
|
||||
|
||||
app = Flask(__name__, template_folder='src/py/templates', static_folder='src/py/static')
|
||||
|
||||
# Function to get data from the API
|
||||
def get_api_data():
|
||||
api_url = "https://garden.inajar.nl/api/battery_voltage_events/?format=json"
|
||||
access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c" # Replace this with your actual access token
|
||||
|
||||
headers = {"Authorization": f"Token {access_token}"}
|
||||
response = requests.get(api_url, headers=headers)
|
||||
|
||||
if response.ok:
|
||||
return response.json().get('results', [])
|
||||
else:
|
||||
print(f"Error fetching data from the API. Status code: {response.status_code}")
|
||||
return []
|
||||
|
||||
# Function to get data from the MySQL database
|
||||
def get_database_data():
|
||||
connection = mysql.connector.connect(
|
||||
host='localhost',
|
||||
user='root',
|
||||
password='',
|
||||
database='goodgarden' # Specify the correct database name here
|
||||
)
|
||||
cursor = connection.cursor()
|
||||
|
||||
# Query to retrieve the latest battery voltage data
|
||||
query = "SELECT timestamp, gateway_receive_time, device, value FROM battery_voltage_events ORDER BY timestamp DESC LIMIT 1"
|
||||
cursor.execute(query)
|
||||
battery_data = cursor.fetchone()
|
||||
|
||||
connection.close()
|
||||
|
||||
return battery_data
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
# Get data from the API
|
||||
api_data = get_api_data()
|
||||
print("API Data:", api_data) # Add this line for debugging
|
||||
|
||||
# Get data from the database
|
||||
battery_data = get_database_data()
|
||||
print("Battery Data:", battery_data) # Add this line for debugging
|
||||
|
||||
# Pass data to the HTML template
|
||||
return render_template('kas_informatie.html', api_data=api_data, battery_data=battery_data)
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(debug=True)
|
||||
Reference in New Issue
Block a user