diff --git a/calculator.py b/calculator.py new file mode 100644 index 0000000..636b0d8 --- /dev/null +++ b/calculator.py @@ -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) diff --git a/static/calculator.css b/static/calculator.css new file mode 100644 index 0000000..caa4e98 --- /dev/null +++ b/static/calculator.css @@ -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; + } diff --git a/templates/calculator.html b/templates/calculator.html new file mode 100644 index 0000000..6b4b2f9 --- /dev/null +++ b/templates/calculator.html @@ -0,0 +1,32 @@ + + + + + Web Calculator with Flask + + + {% block body_content %} +
+

Calculating with Flask!

+ {% if number %} +
The result of your previous calculation is: {{number}}
+ {% else %} +
Please enter two numbers to evaluate: + {% endif %} + {% if error %} +
Invalid input!
+ {% endif %} +
+ + + + +
+ {% endblock %} +