Skip to content

Commit 9b6d2a7

Browse files
committed
Added FlaskSimpleCalculator script in the project
1 parent 34d7d08 commit 9b6d2a7

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed

FlaskSimpleCalculator/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# SIMPLE FLASK CALCULATOR
2+
3+
I have created a simple flask calculator in which user can give two operands and choose one operator to be applied on these operands and the calculated output is shown in simple result page.
4+
5+
The values of operands are taken through a HTML form.
6+
7+
All HTML files are present in templates and the main flask app is present in app.py.
Binary file not shown.

FlaskSimpleCalculator/app.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from flask import Flask, render_template, request
2+
3+
app = Flask(__name__)
4+
app.config.from_object(__name__)
5+
6+
7+
@app.route('/')
8+
def welcome():
9+
return render_template('form.html')
10+
11+
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+
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+
entry = result
30+
return render_template('result.html', entry=entry)
31+
32+
if __name__ == '__main__':
33+
app.run(debug=True)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>Calculator</title>
5+
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet">
6+
</head>
7+
8+
<body align=center>
9+
<div class="row">
10+
<div class="col-md-4"></div>
11+
<div class="col-md-4"><legend><h1>SIMPLE FLASK CALCULATOR</h1></legend></div>
12+
<div class="col-md-4"></div>
13+
</div>
14+
<br>
15+
<div class="row">
16+
<div class="col-md-4"></div>
17+
<div class="col-md-4">
18+
<form class="form-horizontal" action={{ url_for('result') }} method="post">
19+
<div class="form-group">
20+
<label class="control-label col-xs-3" for="operand_1">OPERAND 1:</label>
21+
<div class="col-xs-7">
22+
<input type="number" class="form-control" name="operand_1" placeholder="&nbsp;0&nbsp;">
23+
</div>
24+
</div>
25+
<div class="form-group">
26+
<label class="control-label col-xs-3" for="operand_1">OPERAND 2:</label>
27+
<div class="col-xs-7">
28+
<input type="number" class="form-control" name="operand_2" placeholder="&nbsp;0&nbsp;">
29+
</div>
30+
</div>
31+
<div class="form-group">
32+
<label class="control-label col-xs-3">OPERATOR:</label>
33+
<div class="col-xs-5">
34+
<select class="form-control" name="operator">
35+
<option>Addition</option>
36+
<option>Subtraction</option>
37+
<option>Multiplication</option>
38+
<option>Division</option>
39+
</select>
40+
</div>
41+
</div>
42+
<div class="form-group">
43+
<div class="col-xs-offset-3 col-xs-9">
44+
<input type="submit" class="btn btn-success" value="Submit">
45+
<input type="reset" class="btn btn-danger" value="Reset">
46+
</div>
47+
</div>
48+
</form>
49+
</div>
50+
<div class="col-md-4"></div></div>
51+
</div>
52+
</body>
53+
</html>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>RESULT</title>
5+
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet">
6+
</head>
7+
8+
<body align=center>
9+
<div class="row">
10+
<div class="col-md-4"></div>
11+
<div class="col-md-4"><legend><h1>YOUR RESULT IS</h1></legend></div>
12+
<div class="col-md-4"></div>
13+
</div>
14+
<form class="form-horizontal">
15+
<fieldset disabled>
16+
<div class="form-group">
17+
<label class="control-label col-xs-4" for="result"></label>
18+
<div class="col-xs-4" align="center">
19+
<input type="text" name="result" class="form-control" placeholder={{ entry }}>
20+
</div>
21+
</div>
22+
</fieldset>
23+
</form>
24+
</body>
25+
</html>

0 commit comments

Comments
 (0)