-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
48 lines (39 loc) · 1.46 KB
/
main.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from flask import Flask, request, jsonify, render_template
from calculator import Calculator
app = Flask(__name__)
class Main:
def __init__(self):
self.calculator = Calculator()
def run(self):
app.run(debug=True)
def calculate(self, operation: str, number1: float, number2: float) -> float:
if operation == 'add':
return self.calculator.add(number1, number2)
elif operation == 'subtract':
return self.calculator.subtract(number1, number2)
elif operation == 'multiply':
return self.calculator.multiply(number1, number2)
elif operation == 'divide':
return self.calculator.divide(number1, number2)
else:
raise ValueError(f"Unsupported operation: {operation}")
main_instance = Main()
@app.route('/calculate', methods=['POST'])
def calculate():
data = request.get_json()
operation = data.get('operation')
number1 = data.get('number1', 0.0)
number2 = data.get('number2', 0.0)
try:
result = main_instance.calculate(operation, number1, number2)
return jsonify(result=result), 200
except ValueError as e:
return jsonify(error=str(e)), 400
@app.route('/reset', methods=['POST'])
def reset():
main_instance.calculator.reset()
return jsonify(message="Calculator reset"), 200
@app.route('/')
def home():
return render_template('index.html')
main_instance.run()