From d8e903dd264107f42e5ca92509a34e6253c34cfc Mon Sep 17 00:00:00 2001 From: lino-zurmuehl <66783355+lino-zurmuehl@users.noreply.github.com> Date: Tue, 12 Mar 2024 18:37:19 +0100 Subject: [PATCH] Implemented changes for exercice 1 --- circle.py | 9 +++++++++ flask_app.py | 20 ++++++++++++++++++++ templates/circle.html | 12 ++++++++++++ templates/layout.html | 1 + test_circle.py | 12 ++++++++++++ 5 files changed, 54 insertions(+) create mode 100644 circle.py create mode 100644 templates/circle.html create mode 100644 test_circle.py diff --git a/circle.py b/circle.py new file mode 100644 index 0000000..8cf0fe8 --- /dev/null +++ b/circle.py @@ -0,0 +1,9 @@ +#create circle class +import numpy as np +class Circle: + def __init__(self, radius): + self.radius = radius + def area(self): + return np.pi * self.radius ** 2 + def perimeter(self): + return 2 * np.pi * self.radius \ No newline at end of file diff --git a/flask_app.py b/flask_app.py index 8897fa2..fb0a9e5 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 @@ -10,6 +12,24 @@ def home(): return render_template('home.html') +@app.route('/circle', methods=['GET', 'POST']) +def circle(): + if request.method == 'POST': + radius = request.form['radius'] + + try: + radius = convert_to_float(radius) + if radius <= 0: + return render_template('circle.html', printed_result = "Must be a positive Number.") + + circle = Circle(radius=radius) + area = circle.area() + perimeter = circle.perimeter() + return render_template('circle.html', printed_result = f'The area of you circle is {str(area)} and the perimeter is {str(perimeter)}') + except ValueError: + return render_template('circle.html', printed_result = "Invalid input!") + + return render_template('circle.html') @app.route('/calculate', methods=['GET', 'POST']) # associating the GET and POST method with this route def calculate(): diff --git a/templates/circle.html b/templates/circle.html new file mode 100644 index 0000000..432f9b0 --- /dev/null +++ b/templates/circle.html @@ -0,0 +1,12 @@ +{% extends 'layout.html' %} +{% block content %} +

Circle area and perimeter calculations

+
+ + +
+
+ + {{ printed_result }} + +{% endblock %} \ No newline at end of file diff --git a/templates/layout.html b/templates/layout.html index 8de2e62..23b0193 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -12,6 +12,7 @@

Home

diff --git a/test_circle.py b/test_circle.py new file mode 100644 index 0000000..40d88c6 --- /dev/null +++ b/test_circle.py @@ -0,0 +1,12 @@ +import numpy as np +from circle import Circle + +def test_perimeter(): + circle = Circle(1) + expected_result = 2 * np.pi * 1 + assert circle.perimeter() == expected_result + +def test_area(): + circle = Circle(1) + expected_result = np.pi * 1 ** 2 + assert circle.area() == expected_result \ No newline at end of file