diff --git a/1.py b/1.py new file mode 100644 index 0000000..a703e84 --- /dev/null +++ b/1.py @@ -0,0 +1,8 @@ +from circle import Circle +import math +r = 2 +ins = Circle(r) +print(ins.circle_perimeter()) +print(round((math.pi * 2 * r), 2)) +print(round((math.pi * r**2), 2)) +print(ins.circle_area()) \ No newline at end of file diff --git a/circle.py b/circle.py new file mode 100644 index 0000000..21e0204 --- /dev/null +++ b/circle.py @@ -0,0 +1,18 @@ +import math + +class Circle: + def __init__(self, radius): + self.radius = radius + + def circle_area(self): + return round((math.pi * self.radius**2), 2) + + def circle_perimeter(self): + return round((math.pi * self.radius * 2), 2) + +circle_instance = Circle(1) +check = circle_instance.circle_area() +check2 = circle_instance.circle_perimeter() + +print(check) +print(check2) \ No newline at end of file diff --git a/flask_app.py b/flask_app.py index 8897fa2..653e1a1 100644 --- a/flask_app.py +++ b/flask_app.py @@ -1,6 +1,7 @@ from flask import Flask, render_template, request from helper import perform_calculation, convert_to_float +from circle import Circle app = Flask(__name__) # create the instance of the flask class @@ -38,3 +39,24 @@ 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 calculate_circle(): + radius = '' # Set a default value for radius as empty string + if request.method == 'POST': + # using the request method from flask to request the values that were sent to the server through the POST method + radius = request.form.get('radius') + + try: + radius = convert_to_float(value=radius) + except ValueError: + return render_template('circle.html', printed_result_area="Please type digits") + + + if radius is not None: + create_circle = Circle(float(radius)) # Convert radius to float + calc_area = create_circle.circle_area() + calc_per = create_circle.circle_perimeter() + return render_template('circle.html', printed_result_area=str(calc_area), printed_result_per=str(calc_per)) + else: + return render_template('circle.html', printed_result="Radius not provided") diff --git a/templates/circle.html b/templates/circle.html new file mode 100644 index 0000000..356bd26 --- /dev/null +++ b/templates/circle.html @@ -0,0 +1,19 @@ +{% extends 'layout.html' %} +{% block content %} +

Circle

+
+ + + +
+ +
+ + {% if printed_result_area is defined %} +

Area: {{ printed_result_area }}

+ {% endif %} + {% if printed_result_per is defined %} +

Perimeter: {{ printed_result_per }}

+ {% endif %} +{% endblock %} + diff --git a/test_circle.py b/test_circle.py new file mode 100644 index 0000000..e971ab5 --- /dev/null +++ b/test_circle.py @@ -0,0 +1,14 @@ +from circle import Circle +import math +r = 2 +ins = Circle(r) +print(ins.circle_perimeter()) + +# Testing Perimeter function +assert ins.circle_perimeter() == round((math.pi * 2 * r), 2) + +# Testing Area function +assert ins.circle_area() == round((math.pi * r**2), 2) + + +