From 8dd16900c1cbc3d767b3e10db35ff2ff4321600a Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 18 Sep 2025 16:21:35 +0000 Subject: [PATCH] feat: Integrate UNDP API for SDG data This commit introduces an integration with the United Nations Development Programme (UNDP) API to display data related to Sustainable Development Goals (SDGs). - Adds a new backend endpoint `/api/sdg_data` to fetch data for SDG 15 (Life on Land) from the UNDP API. - The backend processes the API response to filter data for Brazil, Indonesia, and the Democratic Republic of Congo. - Updates the frontend to display the fetched SDG data in a new section on the main page. - Modifies the Flask application to serve the frontend static files, enabling the application to function as a single-page app and avoiding same-origin policy issues during development and testing. --- eco_project/backend/app.py | 40 +++++++++++++++++++++++++++++++-- eco_project/frontend/index.html | 2 ++ eco_project/frontend/script.js | 39 ++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 2 deletions(-) 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}

`; + } + } });