-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
47 lines (30 loc) · 1.17 KB
/
main.py
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
import os
from flask import Flask, render_template, request, redirect, url_for
from form import IMCForm
app = Flask(__name__)
app.config['SECRET_KEY'] = 'who-let-the-dogs-out'
@app.route('/', methods=['GET'])
def index():
return render_template('index.jinja2')
@app.route('/error')
@app.route('/error/<error_message>')
def error(error_message=None):
return render_template("error.jinja2", error=error_message)
@app.route('/imc', methods=['GET', 'POST'])
def imc_route():
form = IMCForm()
if request.method == 'POST':
if form.validate_on_submit():
imc = imc_calc(form.peso.data, form.altura.data)
return render_template('imc_result.jinja2', imc_result=imc)
else:
return redirect(url_for('error', error_message=form.errors))
return render_template('imc_calc.jinja2', form=form)
def imc_calc(peso: float, altura: float) -> float:
return round(peso/altura**2, 2)
@app.errorhandler(Exception)
def all_exception_handler(error_):
return redirect(url_for('error', error_message=error_))
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(debug=True, host='0.0.0.0', port=port)