diff --git a/circle.py b/circle.py new file mode 100644 index 0000000..12b9c2d --- /dev/null +++ b/circle.py @@ -0,0 +1,19 @@ +from math import pi +class Circle: + def __init__(self, radius): + self.radius = radius + + def perimeter(self): + return 2 * 3.14 * self.radius + + def area(self): + return 3.14 * self.radius * self.radius + +def circle_calc(radius, operation): + c = Circle(float(radius)) + if operation == 'perimeter': + return c.perimeter() + elif operation == 'area': + return c.area() + else: + return "Invalid operation" \ No newline at end of file diff --git a/flask_app.py b/flask_app.py index 8897fa2..8cf6d75 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_calc + app = Flask(__name__) # create the instance of the flask class @@ -38,3 +40,19 @@ 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_calculation(): + if request.method == 'POST': + radius = request.form['radius'] + operation = request.form['operation'] + result = circle_calc(radius, operation) + return render_template('circle.html', printed_result=result) + else: + return render_template('circle.html') + + + +if __name__ == '__main__': + app.run(debug=True) # run the application diff --git a/templates/circle.html b/templates/circle.html new file mode 100644 index 0000000..0386ddf --- /dev/null +++ b/templates/circle.html @@ -0,0 +1,24 @@ +{% extends 'layout.html' %} +{% block content %} + + Circle Zone + + + +

Circle

+ +
+ + + +
+ +
+ + {{ printed_result }} + + +{% endblock %} \ No newline at end of file diff --git a/templates/layout.html b/templates/layout.html index 8de2e62..4ca373a 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -12,6 +12,7 @@

Home

diff --git a/test_circle.py b/test_circle.py new file mode 100644 index 0000000..7dc54f6 --- /dev/null +++ b/test_circle.py @@ -0,0 +1,16 @@ +from circle import Circle, circle_calc +import pytest +import numpy as np +from math import pi + +# Testing area calculation +radius = 3 +def area_test(): + circle = Circle(radius) + expected = np.pi * radius ** 2 + assert Circle(radius).area() == expected + +def perimeter_test(): + circle = Circle(radius) + expected = 2 * np.pi * radius + assert Circle(radius).perimeter() == expected \ No newline at end of file