forked from alanbanks229/flask_calculator_app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
88 lines (70 loc) · 2.28 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
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
from flask import Flask, render_template, request
import numpy as np
Flask_App = Flask(__name__) # Creating our Flask Instance
#get method doesn't change state of page
@Flask_App.route('/', methods=['GET'])
def index():
""" Displays the index page accessible at '/' """
return render_template('index.html')
@Flask_App.route('/about')
def about():
return render_template('about.html')
@Flask_App.route('/operation_result/', methods=['POST'])
def operation_result():
"""Route where we send calculator form input"""
error = None
result = None
# request.form looks for:
# html tags with matching "name= "
first_input = request.form['Input1']
second_input = request.form['Input2']
operation = request.form['operation']
try:
#must convert since seen as strings
input1 = float(first_input)
input2 = float(second_input)
# On default, the operation on webpage is addition
if operation == "+":
result = input1 + input2
elif operation == "-":
result = input1 - input2
elif operation == "/":
result = input1 / input2
elif operation == "*":
result = input1 * input2
elif operation == "^":
result = np.power(input1, input2)
else:
operation = "%"
result = input1 % input2
return render_template(
'index.html',
input1=input1,
input2=input2,
operation=operation,
result=result,
calculation_success=True
)
except ZeroDivisionError:
return render_template(
'index.html',
input1=input1,
input2=input2,
operation=operation,
result="Bad Input",
calculation_success=False,
error="You cannot divide by zero"
)
except ValueError:
return render_template(
'index.html',
input1=first_input,
input2=second_input,
operation=operation,
result="Bad Input",
calculation_success=False,
error="Cannot perform numeric operations with provided input"
)
if __name__ == '__main__':
Flask_App.debug = True
Flask_App.run()