week3Flask

This commit is contained in:
mohammedcifci78
2024-02-28 12:09:36 +01:00
parent ef18410631
commit fc76dc14a3
6 changed files with 67 additions and 43 deletions

33
app.py Normal file
View File

@@ -0,0 +1,33 @@
from flask import Flask, render_template, jsonify
import requests
app = Flask(__name__)
# Functie om gegevens van de API op te halen
def get_api_data():
api_url = "https://garden.inajar.nl/api/battery_voltage_events/?format=json"
access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c" # Vervang dit met je echte toegangstoken
headers = {"Authorization": f"Token {access_token}"}
response = requests.get(api_url, headers=headers)
if response.ok:
return response.json().get('results', [])
else:
print(f"Fout bij het ophalen van gegevens van de API. Statuscode: {response.status_code}")
return []
@app.route('/')
def index():
return render_template('index.html')
@app.route('/battery_voltage_events')
def battery_voltage_events():
# Haal gegevens op van de API
api_data = get_api_data()
# Return de gegevens als JSON naar de frontend
return jsonify(results=api_data)
if __name__ == "__main__":
app.run(debug=True)

View File

@@ -11,6 +11,8 @@ def database_connect():
database="goodgarden"
)
# Function for creating data in the database based on battery voltage information from the API
def create_data_from_api(url, access_token, repeat_count=5):
for _ in range(repeat_count):

View File

@@ -1,26 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Python Script Interaction</title>
</head>
<body>
<h1>Choose Operation</h1>
<label for="operation_choice">Choose operation (C for Create, R for Read, U for Update, D for Delete): </label>
<input type="text" id="operation_choice" name="operation_choice" required>
<button onclick="executeOperation()">Submit</button>
<script type="module">
import { executeOperation } from './index.js';
if (typeof executeOperation === 'function') {
document.getElementById('operation_choice').addEventListener('input', (event) => {
executeOperation(event.target.value);
});
} else {
console.error('executeOperation function not found.');
}
</script>
</body>
</html>

View File

@@ -1,17 +0,0 @@
// your_script.js
export function executeOperation(operationChoice) {
fetch(`/path/to/your_script.py?operation=${operationChoice}`)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.text();
})
.then(data => {
alert(data); // You can handle the response data as needed
})
.catch(error => {
console.error('Error:', error);
});
}

17
static/index.js Normal file
View File

@@ -0,0 +1,17 @@
function executeOperation(operationChoice) {
fetch(`/battery_voltage_events?operation=${operationChoice}`)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json(); // Gebruik response.json() om JSON-gegevens te parsen
})
.then(data => {
// Verwerk de gegevens zoals nodig
console.log(data); // Bekijk de gegevens in de console
})
.catch(error => {
console.error('Error:', error);
alert(`An error occurred: ${error.message}`);
});
}

15
templates/index.html Normal file
View File

@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Python Script Interaction</title>
<script src="{{ url_for('static', filename='index.js') }}"></script>
</head>
<body>
<h1>Choose Operation</h1>
<label for="operation_choice">Choose operation (C for Create, R for Read, U for Update, D for Delete): </label>
<input type="text" id="operation_choice" name="operation_choice" required>
<button onclick="executeOperation(document.getElementById('operation_choice').value)">Submit</button>
</body>
</html>