Skip to content

Commit 4846dd6

Browse files
Flask calculator
1 parent d1bf9ce commit 4846dd6

File tree

4 files changed

+31
-69
lines changed

4 files changed

+31
-69
lines changed
Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
1-
from flask import Flask, render_template, request
2-
from handle_calculation import calculate
1+
from flask import Flask, render_template, request, jsonify
2+
# from handle_calculation import calculate
33

44
app = Flask(__name__)
5-
app.config.from_object(__name__)
65

76

8-
@app.route('/')
9-
def welcome():
7+
@app.route("/")
8+
def home():
109
return render_template('form.html')
1110

12-
@app.route('/result', methods=['POST'])
13-
def result():
14-
operand_1 = request.form.get("operand_1", type=int)
15-
operand_2 = request.form.get("operand_2", type=int)
16-
operator = request.form.get("operator")
17-
result = calculate(operand_1, operand_2, operator)
18-
entry = result
19-
return render_template('result.html', entry=entry)
11+
@app.route("/results", methods=['POST','GET'])
12+
def predict():
13+
if request.method == 'POST' and 'operand_1' in request.form and 'operand_2' in request.form and 'operator' in request.form:
14+
operand_1 = float(request.form.get('operand_1'))
15+
operand_2 = float(request.form.get('operand_2'))
16+
operator = request.form.get('operator')
17+
if(operand_2 == 0 and operator=='Division'):
18+
result='Invalid_operation'
19+
elif(operator == 'Addition'):
20+
result = operand_1 + operand_2
21+
elif(operator == 'Subtraction'):
22+
result = operand_1 - operand_2
23+
elif(operator == 'Multiplication'):
24+
result = operand_1 * operand_2
25+
elif(operator == 'Division'):
26+
result = operand_1 / operand_2
27+
else:
28+
result = 'Invalid_Choice'
29+
# result = calculate(operand_1, operand_2, operator)
30+
return render_template("form.html",prediction_text=str(result))
2031

2132
if __name__ == '__main__':
2233
app.run(debug=True)

FLASK PROJECTS/FlaskSimpleCalculator/handle_calculation.py

Lines changed: 0 additions & 26 deletions
This file was deleted.

FLASK PROJECTS/FlaskSimpleCalculator/templates/form.html

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,23 @@
1515
<div class="row">
1616
<div class="col-md-4"></div>
1717
<div class="col-md-4">
18-
<form class="form-horizontal" action={{ url_for('result') }} method="post">
18+
<form class="form-horizontal" action="/results" method="post">
1919
<div class="form-group">
2020
<label class="control-label col-xs-3" for="operand_1">OPERAND 1:</label>
2121
<div class="col-xs-7">
22-
<input type="number" class="form-control" name="operand_1" placeholder="&nbsp;0&nbsp;">
22+
<input type="number" class="form-control" name="operand_1" placeholder="&nbsp;0&nbsp;" required="required">
2323
</div>
2424
</div>
2525
<div class="form-group">
2626
<label class="control-label col-xs-3" for="operand_1">OPERAND 2:</label>
2727
<div class="col-xs-7">
28-
<input type="number" class="form-control" name="operand_2" placeholder="&nbsp;0&nbsp;">
28+
<input type="number" class="form-control" name="operand_2" placeholder="&nbsp;0&nbsp;" required="required">
2929
</div>
3030
</div>
3131
<div class="form-group">
3232
<label class="control-label col-xs-3">OPERATOR:</label>
3333
<div class="col-xs-5">
34-
<select class="form-control" name="operator">
34+
<select class="form-control" name="operator" required="required">
3535
<option>Addition</option>
3636
<option>Subtraction</option>
3737
<option>Multiplication</option>
@@ -46,6 +46,9 @@
4646
</div>
4747
</div>
4848
</form>
49+
<br>
50+
<br>
51+
{{prediction_text}}
4952
</div>
5053
<div class="col-md-4"></div></div>
5154
</div>

FLASK PROJECTS/FlaskSimpleCalculator/templates/result.html

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)