From fc76dc14a34c9ad474b49f6835c57f48a373a184 Mon Sep 17 00:00:00 2001 From: mohammedcifci78 Date: Wed, 28 Feb 2024 12:09:36 +0100 Subject: [PATCH] week3Flask --- app.py | 33 +++++++++++++++++++++++++++++++++ battery_voltage_events.py | 2 ++ index.html | 26 -------------------------- index.js | 17 ----------------- static/index.js | 17 +++++++++++++++++ templates/index.html | 15 +++++++++++++++ 6 files changed, 67 insertions(+), 43 deletions(-) create mode 100644 app.py delete mode 100644 index.html delete mode 100644 index.js create mode 100644 static/index.js create mode 100644 templates/index.html diff --git a/app.py b/app.py new file mode 100644 index 0000000..64a39d8 --- /dev/null +++ b/app.py @@ -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) diff --git a/battery_voltage_events.py b/battery_voltage_events.py index 423d5bd..5a803b2 100644 --- a/battery_voltage_events.py +++ b/battery_voltage_events.py @@ -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): diff --git a/index.html b/index.html deleted file mode 100644 index 708e083..0000000 --- a/index.html +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - Python Script Interaction - - -

Choose Operation

- - - - - - - diff --git a/index.js b/index.js deleted file mode 100644 index c501ea7..0000000 --- a/index.js +++ /dev/null @@ -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); - }); -} diff --git a/static/index.js b/static/index.js new file mode 100644 index 0000000..b26fcb4 --- /dev/null +++ b/static/index.js @@ -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}`); + }); +} diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..9b5a071 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,15 @@ + + + + + + Python Script Interaction + + + +

Choose Operation

+ + + + +