Skip to content

Commit 6360c94

Browse files
committed
updated
0 parents  commit 6360c94

13 files changed

+351
-0
lines changed

.gitignore

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
pip-wheel-metadata/
24+
share/python-wheels/
25+
*.egg-info/
26+
.installed.cfg
27+
*.egg
28+
MANIFEST
29+
30+
# PyInstaller
31+
# Usually these files are written by a python script from a template
32+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
33+
*.manifest
34+
*.spec
35+
36+
# Installer logs
37+
pip-log.txt
38+
pip-delete-this-directory.txt
39+
40+
# Unit test / coverage reports
41+
htmlcov/
42+
.tox/
43+
.nox/
44+
.coverage
45+
.coverage.*
46+
.cache
47+
nosetests.xml
48+
coverage.xml
49+
*.cover
50+
*.py,cover
51+
.hypothesis/
52+
.pytest_cache/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
target/
76+
77+
# Jupyter Notebook
78+
.ipynb_checkpoints
79+
80+
# IPython
81+
profile_default/
82+
ipython_config.py
83+
84+
# pyenv
85+
.python-version
86+
87+
# pipenv
88+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
89+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
90+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
91+
# install all needed dependencies.
92+
#Pipfile.lock
93+
94+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
95+
__pypackages__/
96+
97+
# Celery stuff
98+
celerybeat-schedule
99+
celerybeat.pid
100+
101+
# SageMath parsed files
102+
*.sage.py
103+
104+
# Environments
105+
.env
106+
.venv
107+
env/
108+
venv/
109+
ENV/
110+
env.bak/
111+
venv.bak/
112+
113+
# Spyder project settings
114+
.spyderproject
115+
.spyproject
116+
117+
# Rope project settings
118+
.ropeproject
119+
120+
# mkdocs documentation
121+
/site
122+
123+
# mypy
124+
.mypy_cache/
125+
.dmypy.json
126+
dmypy.json
127+
128+
# Pyre type checker
129+
.pyre/

.idea/FlaskCalculatorApp.iml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/profiles_settings.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

+31
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# FlaskCalculatorApp
2+
Simple app to demo Flask

__init__.py

Whitespace-only changes.

calculator.py

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
from flask import Flask, render_template, request
2+
3+
Flask_App = Flask(__name__) # Creating our Flask Instance
4+
5+
@Flask_App.route('/', methods=['GET'])
6+
def index():
7+
""" Displays the index page accessible at '/' """
8+
9+
return render_template('index.html')
10+
11+
@Flask_App.route('/calculator/', methods=['POST'])
12+
def calculator_operations():
13+
"""Route where we send calculator form input"""
14+
15+
error = None
16+
result = None
17+
18+
# request.form looks for:
19+
# html tags with matching "name= "
20+
first_input = request.form['Input1']
21+
second_input = request.form['Input2']
22+
operation = request.form['operation']
23+
24+
try:
25+
input1 = float(first_input)
26+
input2 = float(second_input)
27+
28+
# On default, the operation on webpage is addition
29+
if operation == "+":
30+
result = input1 + input2
31+
32+
elif operation == "-":
33+
result = input1 - input2
34+
35+
elif operation == "/":
36+
result = input1 / input2
37+
38+
elif operation == "*":
39+
result = input1 * input2
40+
41+
else:
42+
operation = "%"
43+
result = input1 % input2
44+
45+
# return render_template(
46+
# 'index.html',
47+
# input1=input1,
48+
# input2=input2,
49+
# operation=operation,
50+
# result=result,
51+
# calculation_success=True
52+
# )
53+
54+
return {
55+
"status": "Success",
56+
"result": result,
57+
"message": "None",
58+
"inputs": [input1, input2],
59+
"operation": operation
60+
}
61+
62+
except ZeroDivisionError:
63+
# return render_template(
64+
# 'index.html',
65+
# input1=input1,
66+
# input2=input2,
67+
# operation=operation,
68+
# result="Bad Input",
69+
# calculation_success=False,
70+
# error="You cannot divide by zero"
71+
# )
72+
73+
return {
74+
"status": "Failed",
75+
"result": "Bad Input",
76+
"message": "You cannot divide by zero",
77+
"inputs": [first_input, second_input],
78+
"operation": operation
79+
}
80+
81+
except ValueError:
82+
# return render_template(
83+
# 'index.html',
84+
# input1=first_input,
85+
# input2=second_input,
86+
# operation=operation,
87+
# result="Bad Input",
88+
# calculation_success=False,
89+
# error="Cannot perform numeric operations with provided input"
90+
# )
91+
92+
return {
93+
"status": "Failed",
94+
"result": "Bad Input",
95+
"message": "Cannot perform numeric operations with provided input",
96+
"inputs": [first_input, second_input],
97+
"operation": operation
98+
}
99+
100+
if __name__ == '__main__':
101+
Flask_App.debug = True
102+
Flask_App.run(host='0.0.0.0')

static/calculator.css

Whitespace-only changes.

templates/hello_world.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Basic Document</title>
6+
</head>
7+
<body>
8+
<h1>Hello World!</h1>
9+
10+
</body>
11+
</html>

templates/index.html

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Document</title>
7+
<link rel="stylesheet" type="text/css"
8+
href="{{ url_for('static', filename='calculator.css') }}"/>
9+
</head>
10+
<body>
11+
<h1>Python Flask Calculator</h1>
12+
<form method="post" action="{{ url_for('calculator_operations') }}" id="calculator_form">
13+
<label for="Input1">Input1:</label>
14+
<input type="text" name="Input1" id="Input1">
15+
<br/>
16+
<label for="Input2">Input2:</label>
17+
<input type="Input2" name="Input2" id="Input2">
18+
<br/>
19+
<label for="operation">Operation</label>
20+
<select id="operation" name="operation">
21+
<option value="+">Addition</option>
22+
<option value="-">Subtraction</option>
23+
<option value="/">Division</option>
24+
<option value="*">Multiplication</option>
25+
<option value="%">Modulus</option>
26+
</select>
27+
<br/>
28+
<input type="submit" value="Submit"/>
29+
<input type="reset" value="RESET" style="color: red;"/>
30+
</form>
31+
{% if calculation_success == True %}
32+
<br/>
33+
<p>The calculation for {{input1}} {{operation}} {{input2}} is: {{result}}</p>
34+
{% endif %}
35+
{% if calculation_success == False %}
36+
<br/>
37+
<p>{{error}}</p>
38+
<p>{{input1}} {{operation}} {{input2}} = "{{result}}"</p>
39+
{% endif %}
40+
</body>
41+
</html>

0 commit comments

Comments
 (0)