diff --git a/eco_project/backend/app.py b/eco_project/backend/app.py index 7fdad93..8059cd8 100644 --- a/eco_project/backend/app.py +++ b/eco_project/backend/app.py @@ -1,11 +1,17 @@ -from flask import Flask, jsonify +from flask import Flask, jsonify, send_from_directory import requests +import os app = Flask(__name__) +# Path for frontend static files +@app.route('/') +def send_static(path): + return send_from_directory('../frontend', path) + @app.route('/') def home(): - return "Welcome to the Environment Protection Software Backend!" + return send_from_directory('../frontend', 'index.html') @app.route('/api/forest_area') def forest_area(): @@ -36,5 +42,35 @@ def forest_area(): except requests.exceptions.RequestException as e: return jsonify({"error": str(e)}), 500 +@app.route('/api/sdg_data') +def sdg_data(): + # UNDP API URL for SDG 15: Life on Land + url = "https://api.open.undp.org/api/target-data.json?sdg=15" + countries_of_interest = ["BRA", "IDN", "COD"] + + try: + response = requests.get(url) + data = response.json() + + if data: + formatted_data = [] + for target in data: + for recipient in target.get('top_recipients', []): + if recipient.get('iso3') in countries_of_interest: + formatted_data.append({ + 'country': recipient.get('name'), + 'country_iso3_code': recipient.get('iso3'), + 'target_id': target.get('target_id'), + 'description': target.get('description'), + 'budget': recipient.get('total_budget'), + 'expense': recipient.get('total_expense') + }) + return jsonify(formatted_data) + else: + return jsonify({"error": "No data found for the selected criteria."}), 404 + + except requests.exceptions.RequestException as e: + return jsonify({"error": str(e)}), 500 + if __name__ == '__main__': app.run(debug=True) diff --git a/eco_project/frontend/index.html b/eco_project/frontend/index.html index 320a601..5cb14d2 100644 --- a/eco_project/frontend/index.html +++ b/eco_project/frontend/index.html @@ -27,6 +27,8 @@

Community Forums

Development Projects

Tracking and managing sustainable development goals. Here is some data from the World Bank:

+

Here is some data from the UNDP:

+

AI Assistant

diff --git a/eco_project/frontend/script.js b/eco_project/frontend/script.js index 4e14520..215a8a5 100644 --- a/eco_project/frontend/script.js +++ b/eco_project/frontend/script.js @@ -3,9 +3,12 @@ document.addEventListener('DOMContentLoaded', () => { const chatInput = document.getElementById('chat-input'); const chatWindow = document.getElementById('chat-window'); const worldBankDataContainer = document.getElementById('world-bank-data'); + const undpDataContainer = document.getElementById('undp-data'); // Fetch and display World Bank data on page load fetchWorldBankData(); + // Fetch and display UNDP data on page load + fetchUNDPData(); sendBtn.addEventListener('click', () => { const userInput = chatInput.value; @@ -52,4 +55,40 @@ document.addEventListener('DOMContentLoaded', () => { worldBankDataContainer.innerHTML = `

Error fetching data: ${error.message}

`; } } + + async function fetchUNDPData() { + try { + const response = await fetch('/api/sdg_data'); + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + const data = await response.json(); + + if (data.error) { + undpDataContainer.innerHTML = `

Error fetching data: ${data.error}

`; + return; + } + + if (data.length === 0) { + undpDataContainer.innerHTML = '

No UNDP data available for the selected countries.

'; + return; + } + + let html = ''; + + undpDataContainer.innerHTML = html; + + } catch (error) { + undpDataContainer.innerHTML = `

Error fetching data: ${error.message}

`; + } + } });