forked from IsinghGitHub/Flask-Calculator-basic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
56 lines (45 loc) · 2.1 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
from flask import Flask, render_template, request, jsonify
application = Flask(__name__)
@application.route('/', methods=['GET', 'POST']) # To render Homepage
def home_page():
return render_template('index.html')
@application.route('/math', methods=['POST']) # This will be called from UI
def math_operation():
if (request.method=='POST'):
operation=request.form['operation']
num1=int(request.form['num1'])
num2 = int(request.form['num2'])
if(operation=='add'):
r=num1+num2
result= 'the sum of '+str(num1)+' and '+str(num2) +' is '+str(r)
if (operation == 'subtract'):
r = num1 - num2
result = 'the difference of ' + str(num1) + ' and ' + str(num2) + ' is ' + str(r)
if (operation == 'multiply'):
r = num1 * num2
result = 'the product of ' + str(num1) + ' and ' + str(num2) + ' is ' + str(r)
if (operation == 'divide'):
r = num1 / num2
result = 'the quotient when ' + str(num1) + ' is divided by ' + str(num2) + ' is ' + str(r)
return render_template('results.html',result=result)
@application.route('/via_postman', methods=['POST']) # for calling the API from Postman/SOAPUI
def math_operation_via_postman():
if (request.method=='POST'):
operation=request.json['operation']
num1=int(request.json['num1'])
num2 = int(request.json['num2'])
if(operation=='add'):
r=num1+num2
result= 'the sum of '+str(num1)+' and '+str(num2) +' is '+str(r)
if (operation == 'subtract'):
r = num1 - num2
result = 'the difference of ' + str(num1) + ' and ' + str(num2) + ' is ' + str(r)
if (operation == 'multiply'):
r = num1 * num2
result = 'the product of ' + str(num1) + ' and ' + str(num2) + ' is ' + str(r)
if (operation == 'divide'):
r = num1 / num2
result = 'the quotient when ' + str(num1) + ' is divided by ' + str(num2) + ' is ' + str(r)
return jsonify(result)
if __name__ == '__main__':
application.run(debug=True)