Зафиксировал мысль, потом вернусь к идее с бэкендом на питоне.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
#!/usr/bin/env python3
|
||||
from flask import Flask, request, jsonify, render_template
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
-19
@@ -1,19 +0,0 @@
|
||||
def calculate(height, weight):
|
||||
result = weight / (height ** 2)
|
||||
return round(result, 2)
|
||||
|
||||
def user_input_check(height, weight):
|
||||
# Проверка, что введённые значения — числа
|
||||
if not (height.replace('.', '', 1).isdigit() and weight.replace('.', '', 1).isdigit()):
|
||||
return "Ошибка: введите корректные числовые значения."
|
||||
|
||||
height = float(height)
|
||||
weight = float(weight)
|
||||
|
||||
if height <= 0 or height > 3:
|
||||
return "Ошибка ввода роста. Рост должен быть больше 0 и не превышать 3 метра."
|
||||
|
||||
if weight <= 0 or weight > 200:
|
||||
return "Ошибка ввода веса. Вес должен быть больше 0 и не превышать 200 кг."
|
||||
|
||||
return None # Успешная проверка
|
||||
@@ -0,0 +1,30 @@
|
||||
document.getElementById('bmi-form').addEventListener('submit', function(e) {
|
||||
e.preventDefault();
|
||||
|
||||
const height = document.getElementById('height').value;
|
||||
const weight = document.getElementById('weight').value;
|
||||
const resultDiv = document.getElementById('result');
|
||||
|
||||
fetch('/calculate', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ height, weight })
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.error) {
|
||||
resultDiv.style.display = 'block';
|
||||
resultDiv.style.color = '#cc0000';
|
||||
resultDiv.textContent = data.error;
|
||||
} else {
|
||||
resultDiv.style.display = 'block';
|
||||
resultDiv.style.color = '#003366';
|
||||
resultDiv.textContent = `Ваш BMI: ${data.bmi}`;
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
resultDiv.style.display = 'block';
|
||||
resultDiv.textContent = 'Ошибка соединения с сервером.';
|
||||
resultDiv.style.color = '#cc0000';
|
||||
});
|
||||
});
|
||||
@@ -5,7 +5,7 @@
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Калькулятор BMI</title>
|
||||
<!-- Подключение стилей -->
|
||||
<link rel="stylesheet" href="style.css">
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
|
||||
</head>
|
||||
<body>
|
||||
<!-- 1. Шапка сайта (заголовок, логотип, навигация) -->
|
||||
@@ -52,6 +52,6 @@
|
||||
</footer>
|
||||
|
||||
<!-- Подключение JavaScript-файла для обработки формы -->
|
||||
<script src="script.js"></script>
|
||||
<script src="{{ url_for('static', filename='script.js') }}"></script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user