diff --git a/circle.py b/circle.py new file mode 100644 index 0000000..87c9f52 --- /dev/null +++ b/circle.py @@ -0,0 +1,9 @@ +from math import pi +class Circle: + def __init__(self, radius): + self.radius = radius + def area(self): + return pow(self.radius,2) * pi + + def perimeter(self): + return 2 * pi * self.radius diff --git a/flask_app.py b/flask_app.py index 8897fa2..fa2e8ac 100644 --- a/flask_app.py +++ b/flask_app.py @@ -2,6 +2,10 @@ from helper import perform_calculation, convert_to_float +from circle import Circle + +from test_circle import test_circle_area, test_circle_perimeter + app = Flask(__name__) # create the instance of the flask class @@ -38,3 +42,34 @@ def calculate(): return render_template('calculator.html', printed_result="You cannot divide by zero") return render_template('calculator.html') + +@app.route('/circle', methods=['GET', 'POST']) +def circle(): + area = None # default + perimeter = None # default + radius = None # default + printed_result = "" + if request.method == 'POST': + radius_str = request.form.get('radius') + if radius_str: + try: + radius = float(radius_str) + if radius > 0: + circle_obj = Circle(radius) + area = "{:.2f}".format(circle_obj.area()) + perimeter = "{:.2f}".format(circle_obj.perimeter()) + else: + # If radius is not greater than 0, reset it to None and set the error message + radius = None + printed_result = "Radius must be a positive number." + except ValueError: + # If there's a ValueError, reset radius to None and set the error message + radius = None + printed_result = "Please enter a valid number for the radius." + return render_template('circle.html', radius=radius, area=area, perimeter=perimeter, printed_result=printed_result) + +# Optional: Run app in debug Mode including the tests +# if __name__ == '__main__': +# test_circle_area() +# test_circle_perimeter() +# app.run(debug=True) \ No newline at end of file diff --git a/static/main.css b/static/main.css index dfe5192..829e5a0 100644 --- a/static/main.css +++ b/static/main.css @@ -7,10 +7,15 @@ body { padding: 0; } +/* Centered Text */ +p { + text-align: center; +} + .container { max-width: 800px; margin: 0 auto; - padding: 20px; + padding: 10px; } /* Header Styles */ diff --git a/templates/circle.html b/templates/circle.html new file mode 100644 index 0000000..bab1931 --- /dev/null +++ b/templates/circle.html @@ -0,0 +1,25 @@ +{% extends 'layout.html' %} +{% block content %} +
Input radius of circle here to find it's area and perimeter.
+ + + +For a radius of {{ radius }} the area and perimeter are the following:
+Area: {{ area }}
+Perimeter: {{ perimeter }}
+ {% elif printed_result %} +{{ printed_result }}
+ {% endif %} + +