diff --git a/circle.py b/circle.py new file mode 100644 index 0000000..3f96b7d --- /dev/null +++ b/circle.py @@ -0,0 +1,11 @@ +import math + +class Circle: + def __init__(self, radius): + self.radius = radius + + def calculate_perimeter(self): + return 2 * math.pi * self.radius + + def calculate_area(self): + return math.pi * (self.radius ** 2) diff --git a/flask_app.py b/flask_app.py index 8897fa2..400fcb2 100644 --- a/flask_app.py +++ b/flask_app.py @@ -1,6 +1,7 @@ from flask import Flask, render_template, request - from helper import perform_calculation, convert_to_float +from circle import Circle +from test_circle import test_circle app = Flask(__name__) # create the instance of the flask class @@ -38,3 +39,38 @@ def calculate(): return render_template('calculator.html', printed_result="You cannot divide by zero") return render_template('calculator.html') + + + +# new route for circle calculations + +@app.route('/circle', methods=['GET', 'POST']) +def calculate_circle(): + if request.method == 'POST': + radius = request.form['radius'] + operation = request.form['operation'] + + try: + radius = float(radius) + circle = Circle(radius) + if operation == 'area': + result = circle.calculate_area() + result_message = "The area of the circle" + elif operation == 'perimeter': + result = circle.calculate_perimeter() + result_message = "The perimeter of the circle" + else: + raise ValueError("Invalid operation.") + except ValueError as e: + return render_template('calculator_circle.html', error=str(e)) + except Exception: + return render_template('calculator_circle.html', error="Error encountered. Please try again.") + + return render_template('calculator_circle.html', result=result, result_message=result_message) + else: + return render_template('calculator_circle.html') + + + +if __name__ == '__main__': + app.run(debug=True) \ No newline at end of file diff --git a/templates/calculator.html b/templates/calculator.html index 3dd1105..07063e5 100644 --- a/templates/calculator.html +++ b/templates/calculator.html @@ -20,4 +20,4 @@
Error: {{ error }}
+ {% else %} + {% if result %} +{{ result_message }}: {{ result }}
+ {% endif %} + {% endif %} +{% endblock %} + diff --git a/templates/layout.html b/templates/layout.html index 8de2e62..8835037 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -5,20 +5,22 @@ - +