diff --git a/flask_app.py b/app.py similarity index 60% rename from flask_app.py rename to app.py index 8897fa2..157e0a8 100644 --- a/flask_app.py +++ b/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 @@ -38,3 +40,24 @@ def calculate(): return render_template('calculator.html', printed_result="You cannot divide by zero") return render_template('calculator.html') + + +@app.route('/calculate-circle', methods=['GET', 'POST']) +def calculate_circle(): + if request.method == 'POST': + radius = request.form['radius'] + try: + radius = float(radius) + except: + return render_template('circle.html', radius = '', area='', perimeter='', + msg = f'The radius must be a number, not {type(radius)}.') + if radius <= 0: + return render_template('circle.html', radius = '', area='', perimeter='', + msg = f'The radius of the circle must be greater than zero. {radius} is less than zero.') + circle = Circle(radius) + return render_template('circle.html', + msg = '', + radius = radius, + area = format(circle.area(), '.2f'), + perimeter = format(circle.perimeter(), '.2f')) + return render_template('circle.html', radius = '', msg = '', area='', perimeter='') \ No newline at end of file diff --git a/circle.py b/circle.py new file mode 100644 index 0000000..a509de3 --- /dev/null +++ b/circle.py @@ -0,0 +1,9 @@ +from math import pi + +class Circle: + def __init__(self, radius): + self.radius = radius + def perimeter(self): + return pi * 2 * self.radius + def area(self): + return pi * self.radius**2 \ No newline at end of file diff --git a/static/main.css b/static/main.css index dfe5192..13037df 100644 --- a/static/main.css +++ b/static/main.css @@ -97,3 +97,7 @@ button { button:hover { background-color: #23272b; /* Slightly darker color on hover */ } + +.center { + text-align: center; +} \ No newline at end of file diff --git a/templates/circle.html b/templates/circle.html new file mode 100644 index 0000000..3144bb8 --- /dev/null +++ b/templates/circle.html @@ -0,0 +1,27 @@ +{% extends 'layout.html' %} +{% block content %} +

Circle Calculator

+
+ + +
+ +
+ +
+ {{ msg }} +
+
+ Radius: {{ radius }} +
+
+
+ Area: {{ area }} +
+
+
+ Perimeter: {{ perimeter }} +
+ + +{% endblock %} \ No newline at end of file diff --git a/templates/layout.html b/templates/layout.html index 8de2e62..ede7ca4 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..ce56624 --- /dev/null +++ b/test_circle.py @@ -0,0 +1,25 @@ +from circle import Circle +from math import pi +import random + +''' +These tests are circular, but we're testing a toy example. +I could have also hardcoded some precalculated perimeters and areas and tested it that way. +I am not testing invalid inputs (e.g. radius = "five") here because I handle that via input validation in Flask. +I would normally test the Flask app itself, but that is out of scope for this problem set. + +I run these tests via pytest test_circle.py +''' + + + +def test_circle_perimeter(): + radius = random.random() * 100 + circle = Circle(radius) + assert circle.perimeter() == pi * 2 * radius + + +def test_circle_area(): + radius = random.random() * 100 + circle = Circle(radius) + assert circle.area() == pi * radius**2 \ No newline at end of file