diff --git a/circle.py b/circle.py new file mode 100644 index 0000000..5f330ce --- /dev/null +++ b/circle.py @@ -0,0 +1,16 @@ +import math + + +class Circle: + def __init__(self, radius): + if radius < 0: + raise ValueError('Radius cannot be negative') + self.radius = radius + + def perimeter(self): + # Calculate the perimeter of a circle + return 2 * math.pi * self.radius + + def area(self): + # Calculate the area of a circle + return math.pi * (self.radius ** 2) diff --git a/flask_app.py b/flask_app.py index 8897fa2..9c8764d 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 @@ -11,7 +13,8 @@ def home(): return render_template('home.html') -@app.route('/calculate', methods=['GET', 'POST']) # associating the GET and POST method with this route +# associating the GET and POST method with this route +@app.route('/calculate', methods=['GET', 'POST']) def calculate(): if request.method == 'POST': # using the request method from flask to request the values that were sent to the server through the POST method @@ -31,10 +34,36 @@ def calculate(): return render_template('calculator.html', printed_result="Cannot perform operation with this input") try: - result = perform_calculation(value1=value1, value2=value2, operation=operation) + result = perform_calculation( + value1=value1, value2=value2, operation=operation) return render_template('calculator.html', printed_result=str(result)) except ZeroDivisionError: return render_template('calculator.html', printed_result="You cannot divide by zero") return render_template('calculator.html') + +# Below is the part that I added. +# It's the homepage to calculat the circie's perimeter and area. + + +@app.route('/circle', methods=['GET', 'POST']) +def circle(): + if request.method == 'POST': + radius = request.form['radius'] + try: + radius = convert_to_float(value=radius) + except ValueError: + # I already made circlecal.html in the templates folder. + return render_template('circlecal.html', printed_result="Cannot perform operation with this input") + try: + # Below is the process calculating perimeter and area + circle = Circle(radius) + # For simplicity, I applied the 'round' function. + perimeter = round(circle.perimeter(), 2) + area = round(circle.area(), 2) + return render_template('circlecal.html', printed_result=f"Perimeter: {perimeter}, Area: {area}") + except ValueError: + return render_template('circlecal.html', printed_result="You cannot input negative value") + + return render_template('circlecal.html') diff --git a/templates/circlecal.html b/templates/circlecal.html new file mode 100644 index 0000000..aa87e36 --- /dev/null +++ b/templates/circlecal.html @@ -0,0 +1,13 @@ +{% extends 'layout.html' %} +{% block content %} +