From 7486a900fa620c16b3579f471eb6c0bbcdb5ab2e Mon Sep 17 00:00:00 2001 From: isabella-ut Date: Mon, 11 Mar 2024 20:36:01 +0100 Subject: [PATCH] pull request task 1 homework 2 Isabella Urbano-Trujillo --- circle.py | 11 +++++++++ flask_app.py | 38 +++++++++++++++++++++++++++++++- templates/calculator.html | 2 +- templates/calculator_circle.html | 29 ++++++++++++++++++++++++ templates/layout.html | 10 +++++---- test_circle.py | 9 ++++++++ 6 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 circle.py create mode 100644 templates/calculator_circle.html create mode 100644 test_circle.py 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 @@

Calculator

{{ printed_result }} -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/templates/calculator_circle.html b/templates/calculator_circle.html new file mode 100644 index 0000000..93a04f7 --- /dev/null +++ b/templates/calculator_circle.html @@ -0,0 +1,29 @@ +{% extends 'layout.html' %} +{% block content %} +

Circle Calculator

+
+ + +
+ + +
+
+ + +
+ +
+ +
+ + + {% if error %} +

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 @@ - +
-
+
{% block content %} {% endblock %}
- - \ No newline at end of file + + diff --git a/test_circle.py b/test_circle.py new file mode 100644 index 0000000..f8e22a1 --- /dev/null +++ b/test_circle.py @@ -0,0 +1,9 @@ +from circle import Circle + +def test_circle(radius : float, operation : str) -> float: + circle_instance = Circle(radius) + if operation == "area": + result = circle_instance.calculate_area() + else: + result = circle_instance.calculate_perimeter() + return result