diff --git a/circle.py b/circle.py new file mode 100644 index 0000000..edb1a88 --- /dev/null +++ b/circle.py @@ -0,0 +1,12 @@ +# circle.py +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..9de527f 100644 --- a/flask_app.py +++ b/flask_app.py @@ -2,6 +2,8 @@ from helper import perform_calculation, convert_to_float +from circle import Circle + app = Flask(__name__) # create the instance of the flask class @@ -38,3 +40,27 @@ def calculate(): return render_template('calculator.html', printed_result="You cannot divide by zero") return render_template('calculator.html') + + +@app.route('/calculate_circle', methods=['GET', 'POST']) +def calculate_circle(): + if request.method == 'POST': + try: + radius = convert_to_float(request.form.get('radius', '')) + circle = Circle(radius) + calc_method = request.form.get('calc_method') + + if calc_method == 'area': + result = circle.calculate_area() + result_text = f"Area: {result}" + elif calc_method == 'perimeter': + result = circle.calculate_perimeter() + result_text = f"Perimeter: {result}" + else: + return render_template('circle_calculator.html', error="Invalid calculation method selected.") + + return render_template('circle_calculator.html', result=result_text) + except ValueError as e: + return render_template('circle_calculator.html', error=str(e)) + else: + return render_template('circle_calculator.html') \ No newline at end of file diff --git a/templates/calculator.html b/templates/calculator.html index 3dd1105..6bbeb24 100644 --- a/templates/calculator.html +++ b/templates/calculator.html @@ -1,23 +1,25 @@ {% extends 'layout.html' %} {% block content %} -
Result: {{ printed_result }}
+{% endif %} -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/templates/circle_calculator.html b/templates/circle_calculator.html new file mode 100644 index 0000000..6f161af --- /dev/null +++ b/templates/circle_calculator.html @@ -0,0 +1,33 @@ +{% extends 'layout.html' %} +{% block content %} + +Perimeter: {{ result.perimeter }}
+Area: {{ result.area }}
+ {% else %} +Result: {{ result }}
+ {% endif %} +{% endif %} + +{% if error %} +{{ error }}
+{% endif %} + +{% endblock %} diff --git a/templates/layout.html b/templates/layout.html index 8de2e62..474c1ce 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -5,20 +5,21 @@ - +