diff --git a/circle.py b/circle.py new file mode 100644 index 0000000..15c34b0 --- /dev/null +++ b/circle.py @@ -0,0 +1,14 @@ + +import math + +class Circle: + def __init__(self, radius): + self.radius = radius + + def calculate_perimeter(self): + perimeter = 2 * math.pi * self.radius + return round(perimeter, 2) + + def calculate_area(self): + area = math.pi * self.radius**2 + return round(area, 2) \ No newline at end of file diff --git a/flask_app.py b/flask_app.py index 8897fa2..866488f 100644 --- a/flask_app.py +++ b/flask_app.py @@ -1,25 +1,23 @@ -from flask import Flask, render_template, request +# flask_app.py +from flask import Flask, render_template, request +from circle import Circle # Import Circle from the circle module from helper import perform_calculation, convert_to_float -app = Flask(__name__) # create the instance of the flask class - +app = Flask(__name__) @app.route('/') @app.route('/home') def home(): return render_template('home.html') - -@app.route('/calculate', methods=['GET', 'POST']) # 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 value1 = request.form['value1'] value2 = request.form['value2'] operation = str(request.form['operation']) - # make sure the input is one of the allowed inputs (not absolutely necessary in the drop-down case) if operation not in ['add', 'subtract', 'divide', 'multiply']: return render_template('calculator.html', printed_result='Operation must be one of "add", "subtract", "divide", or "multiply".') @@ -38,3 +36,32 @@ def calculate(): return render_template('calculator.html', printed_result="You cannot divide by zero") return render_template('calculator.html') + + +@app.route('/calculate_perimeter_area', methods=['GET', 'POST']) +def calculate_perimeter_area(): + perimeter_result = None + area_result = None + + if request.method == 'POST': + radius = request.form['radius'] + + try: + radius = convert_to_float(radius) + except ValueError: + return render_template('perimeter_calculator.html', printed_result="Please enter a valid number for the radius.") + + if radius <= 0: + return render_template('perimeter_calculator.html', printed_result="Radius must be a positive number.") + + circle = Circle(radius) + perimeter_result = circle.calculate_perimeter() + area_result = circle.calculate_area() + + print("Perimeter Result:", perimeter_result) + print("Area Result:", area_result) + + return render_template('perimeter_calculator.html', perimeter_result=perimeter_result, area_result=area_result) + +if __name__ == '__main__': + app.run(debug=True) diff --git a/helper.py b/helper.py index 31e82d6..ac9214f 100644 --- a/helper.py +++ b/helper.py @@ -1,4 +1,4 @@ -# create helper functions for calculations + def perform_calculation(value1: float, value2: float, operation: str) -> float: @@ -45,3 +45,6 @@ def convert_to_float(value: str) -> float: float_value = float(value) return float_value + + +# helper.py diff --git a/templates/layout.html b/templates/layout.html index 8de2e62..56f213e 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -12,6 +12,7 @@
Perimeter: {{ perimeter_result }} units
+ {% endif %} + {% if area_result is not none %} +Area: {{ area_result }} square units
+ {% endif %} +