Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions eco_project/backend/app.py
Original file line number Diff line number Diff line change
@@ -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('/<path:path>')
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():
Expand Down Expand Up @@ -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)
2 changes: 2 additions & 0 deletions eco_project/frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ <h2>Community Forums</h2>
<h2>Development Projects</h2>
<p>Tracking and managing sustainable development goals. Here is some data from the World Bank:</p>
<div id="world-bank-data"></div>
<p>Here is some data from the UNDP:</p>
<div id="undp-data"></div>
</section>
<section id="ai-agent">
<h2>AI Assistant</h2>
Expand Down
39 changes: 39 additions & 0 deletions eco_project/frontend/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,4 +55,40 @@ document.addEventListener('DOMContentLoaded', () => {
worldBankDataContainer.innerHTML = `<p>Error fetching data: ${error.message}</p>`;
}
}

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 = `<p>Error fetching data: ${data.error}</p>`;
return;
}

if (data.length === 0) {
undpDataContainer.innerHTML = '<p>No UNDP data available for the selected countries.</p>';
return;
}

let html = '<ul>';
data.forEach(item => {
html += `<li><b>${item.country} (Target ${item.target_id}):</b> ${item.description}
<ul>
<li>Budget: $${item.budget ? item.budget.toFixed(2) : 'N/A'}</li>
<li>Expense: $${item.expense ? item.expense.toFixed(2) : 'N/A'}</li>
Comment on lines +81 to +82
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Potential for runtime errors if budget or expense is not a number.

Explicitly convert budget and expense to numbers before using toFixed, or use a formatting method that handles non-numeric values safely.

</ul>
</li>`;
});
html += '</ul>';

undpDataContainer.innerHTML = html;

} catch (error) {
undpDataContainer.innerHTML = `<p>Error fetching data: ${error.message}</p>`;
}
}
Comment on lines +59 to +93
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (code-quality): Avoid function declarations, favouring function assignment expressions, inside blocks. (avoid-function-declarations-in-blocks)

ExplanationFunction declarations may be hoisted in Javascript, but the behaviour is inconsistent between browsers. Hoisting is generally confusing and should be avoided. Rather than using function declarations inside blocks, you should use function expressions, which create functions in-scope.

});