-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
209 lines (183 loc) · 7.34 KB
/
Copy pathindex.html
File metadata and controls
209 lines (183 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BMI Calculator</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body {
background-color: #f4f7fc;
font-family: 'Arial', sans-serif;
}
.container {
max-width: 600px;
margin-top: 50px;
}
.card {
border-radius: 10px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
.card-header {
background-color: #007bff;
color: white;
font-size: 1.25rem;
text-align: center;
padding: 15px;
}
.card-body {
background-color: white;
padding: 30px;
}
.form-control {
border-radius: 8px;
padding: 10px;
font-size: 1rem;
}
.btn {
border-radius: 8px;
padding: 10px 20px;
}
.btn-primary {
background-color: #007bff;
border: none;
}
.btn-secondary {
background-color: #6c757d;
border: none;
}
.table th, .table td {
text-align: center;
}
.result-card {
margin-top: 30px;
padding: 20px;
background-color: #e9f7fd;
border-radius: 8px;
box-shadow: 0 4px 10px rgba(0, 123, 255, 0.1);
}
.history-table-container {
display: none;
margin-top: 30px;
}
.history-table-container table {
background-color: white;
border-radius: 8px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}
</style>
</head>
<body>
<div class="container">
<div class="card">
<div class="card-header">
<h4>BMI Calculator</h4>
</div>
<div class="card-body">
<form id="bmiForm">
<div class="mb-3">
<label for="weight" class="form-label">Weight (kg)</label>
<input type="number" id="weight" class="form-control" step="0.1" required placeholder="Enter your weight (30 - 150 kg)">
<div class="invalid-feedback">
Weight must be between 30 kg and 150 kg.
</div>
</div>
<div class="mb-3">
<label for="height" class="form-label">Height (cm)</label>
<input type="number" id="height" class="form-control" step="0.1" required placeholder="Enter your height (140 - 210 cm)">
<div class="invalid-feedback">
Height must be between 140 cm and 210 cm.
</div>
</div>
<button type="submit" class="btn btn-primary w-100">Calculate BMI</button>
</form>
</div>
</div>
<!-- BMI Calculation Result -->
<div id="result" class="result-card d-none">
<h5>BMI Calculation Result</h5>
<p id="bmiValue"></p>
<p id="categoryValue"></p>
<p id="changePercentageValue"></p>
</div>
<!-- Load History Button -->
<button id="loadHistory" class="btn btn-secondary w-100 mt-3">See The Results!</button>
<!-- History Table -->
<div id="historyTableContainer" class="history-table-container">
<table class="table table-bordered mt-4">
<thead>
<tr>
<th>ID</th>
<th>Weight</th>
<th>Height</th>
<th>BMI</th>
<th>Category</th>
<th>% Change</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
<script>
const API_BASE_URL = 'http://127.0.0.1:8000';
// Handle BMI calculation form submission
document.getElementById("bmiForm").addEventListener("submit", async (event) => {
event.preventDefault();
const weight = parseFloat(document.getElementById("weight").value);
const height = parseFloat(document.getElementById("height").value);
// Frontend validation for weight and height
if (isNaN(weight) || weight < 30 || weight > 150) {
alert("Please enter a valid weight (between 30 kg and 150 kg).");
return;
}
if (isNaN(height) || height < 140 || height > 210) {
alert("Please enter a valid height (between 140 cm and 210 cm).");
return;
}
// Send the data to the backend API
const response = await fetch(`${API_BASE_URL}/calculate-bmi/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ weight, height }),
});
if (response.ok) {
const data = await response.json();
document.getElementById("result").classList.remove('d-none');
document.getElementById("bmiValue").innerText = `BMI: ${data.bmi}`;
document.getElementById("categoryValue").innerText = `Category: ${data.category}`;
document.getElementById("changePercentageValue").innerText = `Change Percentage: ${data.change_percentage === null ? 'No Change' : data.change_percentage.toFixed(2)}%`;
} else {
alert("Error calculating BMI");
}
});
// Handle history loading
document.getElementById("loadHistory").addEventListener("click", async () => {
const response = await fetch(`${API_BASE_URL}/bmi-history/`);
if (response.ok) {
const history = await response.json();
const tableBody = document.querySelector("#historyTableContainer tbody");
tableBody.innerHTML = ''; // Clear the table before adding new rows
history.forEach((record) => {
const row = document.createElement("tr");
const changePercentage = record.change_percentage === null ? "No Change" : record.change_percentage.toFixed(2);
row.innerHTML = `
<td>${record.id}</td>
<td>${record.weight}</td>
<td>${record.height}</td>
<td>${record.bmi}</td>
<td>${record.category}</td>
<td>${changePercentage}</td>
`;
tableBody.appendChild(row);
});
document.getElementById("historyTableContainer").style.display = 'block';
} else {
alert("Failed to load history.");
}
});
</script>
</body>
</html>