33 lines
952 B
Python
33 lines
952 B
Python
from flask import Flask, request, jsonify, render_template
|
|
|
|
app = Flask(__name__)
|
|
|
|
def calculate_bmi(height, weight):
|
|
return round(weight / (height ** 2), 2)
|
|
|
|
def user_input_check(height, weight):
|
|
if height <= 0 or height > 3:
|
|
return "Рост должен быть больше 0 и не превышать 3 метра."
|
|
if weight <= 0 or weight > 200:
|
|
return "Вес должен быть больше 0 и не превышать 200 кг."
|
|
return None
|
|
|
|
@app.route('/')
|
|
def index():
|
|
return render_template('index.html')
|
|
|
|
@app.route('/calculate', methods=['POST'])
|
|
def calculate():
|
|
data = request.get_json()
|
|
height = float(data['height'])
|
|
weight = float(data['weight'])
|
|
|
|
error = user_input_check(height, weight)
|
|
if error:
|
|
return jsonify({'error': error}), 400
|
|
|
|
bmi = calculate_bmi(height, weight)
|
|
return jsonify({'bmi': bmi})
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True) |