forked from manikcloud/manik-python-flask-calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
57 lines (47 loc) · 1.47 KB
/
app.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
48
49
50
51
52
53
54
55
56
57
import os
from flask import Flask, render_template, request, redirect, url_for
# create a Flask application instance
app = Flask(__name__)
# define the index route
@app.route('/')
def index():
return render_template('index.html')
# define the result route
@app.route('/result')
def result():
return render_template('result.html')
# define the error route
@app.route('/error')
def error():
return render_template('error.html')
# define the calculate route
@app.route('/calculate', methods=['POST'])
@app.route('/calculate', methods=['POST'])
def calculate():
try:
num1 = float(request.form['num1'])
num2 = float(request.form['num2'])
except ValueError:
return redirect(url_for('error'))
operation = request.form['operation']
if operation == 'add':
result = num1 + num2
elif operation == 'subtract':
result = num1 - num2
elif operation == 'multiply':
result = num1 * num2
elif operation == 'divide':
if num2 == 0:
return redirect(url_for('error'))
result = num1 / num2
else:
return redirect(url_for('error'))
return render_template('result.html', result=result)
# define the health check route
@app.route('/health')
def health():
return 'OK'
# run the application
if __name__ == '__main__':
# app.run(host='0.0.0.0', port=5000)
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 5000)))