-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
33 lines (22 loc) · 914 Bytes
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from flask import Flask, jsonify, request, render_template
from calculator.calculator import Calculator
from calculator.operator_registry import OperatorRegistry, OPERATORS_MAP
from calculator.shunting_yard import ShuntingYardAlgorithm
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/calculate", methods=["POST"])
def calculate():
data = request.get_json()
operands = map(float, data["operands"].split(","))
operators = data["operators"]
operators_registry = OperatorRegistry(OPERATORS_MAP)
calculation_strategy = ShuntingYardAlgorithm(operators_registry)
calculator = Calculator(calculation_strategy)
result = calculator.calculate(operands, operators)
color = "red" if result % 2 else "green"
response = {"result": result, "color": color}
return jsonify(response)
if __name__ == "__main__":
app.run()