From c072a918a5ac0d1d0e3d2f44ba56e1d08da25c4d Mon Sep 17 00:00:00 2001 From: Milton0215 Date: Thu, 14 Mar 2024 20:57:08 +0100 Subject: [PATCH] final --- flask_app.py | 32 +++++++++++++++++++++++++++++--- helper_circle.py | 9 +++++++++ templates/circle.html | 20 ++++++++++++++++++++ templates/layout.html | 3 +++ test.py | 20 ++++++++++++++++++++ 5 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 helper_circle.py create mode 100644 templates/circle.html create mode 100644 test.py diff --git a/flask_app.py b/flask_app.py index 8897fa2..b37700b 100644 --- a/flask_app.py +++ b/flask_app.py @@ -1,16 +1,17 @@ +# Import Flask class from flask import Flask, render_template, request - from helper import perform_calculation, convert_to_float +from helper_circle import circle_perform +# Create Flask object app = Flask(__name__) # create the instance of the flask class - @app.route('/') @app.route('/home') def home(): return render_template('home.html') - +#Creating home route and returning home HTML @app.route('/calculate', methods=['GET', 'POST']) # associating the GET and POST method with this route def calculate(): if request.method == 'POST': @@ -38,3 +39,28 @@ def calculate(): return render_template('calculator.html', printed_result="You cannot divide by zero") return render_template('calculator.html') + +#Creating circle route and returning circle HTML +@app.route('/circle', methods=['GET', 'POST']) # associating the GET and POST method with this route +def circle(): + printed_result = None + + if request.method == 'POST': + radius = float(request.form['radius']) + operation = request.form['operation'] + + # Create an instance + helper_circle = circle_perform(radius) + + if radius < 0: + printed_result = 'Radius must not be defined as negative.' + elif operation not in ['Perimeter', 'Area']: + printed_result = 'Operation must be one of "Perimeter", or "Area".' + elif operation == 'Perimeter': + result = helper_circle.perimeter() + printed_result = f"Perimeter: {result}" + else: + result = helper_circle.area() + printed_result = f"Area: {result}" + + return render_template('circle.html', printed_result=printed_result) \ No newline at end of file diff --git a/helper_circle.py b/helper_circle.py new file mode 100644 index 0000000..774d496 --- /dev/null +++ b/helper_circle.py @@ -0,0 +1,9 @@ +# create helper_circle functions for circle calculations +import math +class circle_perform: + def __init__(self, radius: float): + self.radius=radius + def perimeter(self) -> float: + return 2*math.pi*self.radius + def area(self) -> float: + return math.pi*(self.radius**2) \ No newline at end of file diff --git a/templates/circle.html b/templates/circle.html new file mode 100644 index 0000000..ae98756 --- /dev/null +++ b/templates/circle.html @@ -0,0 +1,20 @@ +{% extends 'layout.html' %} +{% block content %} +

Perimeter and Area Calculator

+
+ + + + + + +
+ +
+ + {{ printed_result }} + +{% endblock %} \ No newline at end of file diff --git a/templates/layout.html b/templates/layout.html index 8de2e62..210c984 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -13,6 +13,9 @@

Home

+ diff --git a/test.py b/test.py new file mode 100644 index 0000000..f527ebd --- /dev/null +++ b/test.py @@ -0,0 +1,20 @@ +from helper_circle import circle_perform +import math + +def perimeter_test(): + #Create an instance radius=1 + helper_circle = circle_perform(1) + + perimeter = helper_circle.perimeter() + assert round(perimeter, 8) == round(2 * math.pi, 8) + +def area_test(): + #Create an instance radius=1 + helper_circle = circle_perform(1) + + area = helper_circle.area + assert round(area, 8) == round(math.pi, 8) + + + +