Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from flask import Flask, render_template, request

app = Flask(__name__)

@app.route("/", methods=["GET", "POST"])
def calculate():
number = 0
if request.method == "GET":
return render_template("calculator.html", number="")
elif request.method == "POST":
try:
num = float(request.form["first_number"])
num2 = float(request.form["second_number"])
except ValueError:
return render_template("calculator.html", number="", error=True)

if request.form["operator"] == "+":
return render_template("calculator.html", number=(num+num2))
elif request.form["operator"] == "-":
return render_template("calculator.html", number=(num-num2))
elif request.form["operator"] == "*":
return render_template("calculator.html", number=(num*num2))
elif request.form["operator"] == "/":
return render_template("calculator.html", number=(num/num2))


def get_resource_as_string(name, charset='utf-8'):
with app.open_resource(name) as f:
return f.read().decode(charset)


if __name__ == "__main__":
app.run(debug=True)
58 changes: 58 additions & 0 deletions static/calculator.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
body { text-align: center;}

form {
background: -webkit-gradient(linear, bottom, left 175px, from(#CCCCCC), to(#EEEEEE));
background: -moz-linear-gradient(bottom, #CCCCCC, #EEEEEE 175px);
margin: auto;
position:relative;
width:400px;
height:175px;
font-family: Tahoma, Geneva, sans-serif;
font-size: 14px;
font-style: italic;
line-height: 24px;
font-weight: bold;
color: #000;
text-decoration: none;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
padding:10px;
border: 1px solid #999;
border: inset 1px solid #333;
-webkit-box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);
-moz-box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);
box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);
}

input {
width:100px;
margin:5px;
bottom: 50px;
display:line;
border: 2px solid #999;
height: 25px;
-webkit-box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);
-moz-box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);
box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.3);
}


div.ui-btn, input[type="submit"] {
width:100px;
position:absolute;
bottom:20px;
left:150px;
background:#09C;
color:#fff;
font-family: Tahoma, Geneva, sans-serif;
height:30px;
-webkit-border-radius: 15px;
-moz-border-radius: 15px;
border-radius: 15px;
border: 1p solid #999;
}
div.ui-btn, input[type="submit"]:hover{
background:#fff;
color:#09C;
}
32 changes: 32 additions & 0 deletions templates/calculator.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE html>

<html>
<head>
<title>Web Calculator with Flask</title>
<link rel="stylesheet" href="{{url_for('static', filename='calculator.css')}}">
</head>
{% block body_content %}
<form action="/" method="POST">
<h1>Calculating with Flask!</h1>
{% if number %}
<div>The result of your previous calculation is: {{number}}<div>
{% else %}
<div>Please enter two numbers to evaluate:
{% endif %}
{% if error %}
<div>Invalid input!</div>
{% endif %}
<div>
<input type="text" id="first_number" name="first_number" value={{number}}>
<select name="operator">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</menu>
<input type="text" id="second_number" name="second_number" value="">
<input type="submit" value="Calculate!">
</form>
</div>
{% endblock %}
</html>