From 826e240d0b83dd3d286dfa05e6de0a3122076679 Mon Sep 17 00:00:00 2001 From: Jackson Luckey Date: Fri, 8 Mar 2024 15:25:49 +0100 Subject: [PATCH 1/3] circle.py is for the circle class for the problem set --- circle.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 circle.py diff --git a/circle.py b/circle.py new file mode 100644 index 0000000..e69de29 From aea781ed552a5536bbb8271f5975c78dbe330d4a Mon Sep 17 00:00:00 2001 From: Jackson Luckey Date: Fri, 8 Mar 2024 16:38:49 +0100 Subject: [PATCH 2/3] basically finished the calculator. renamed flask_app to make it more convenient for testing. --- flask_app.py => app.py | 23 +++++++++++++++++++++++ circle.py | 9 +++++++++ static/main.css | 4 ++++ templates/circle.html | 27 +++++++++++++++++++++++++++ test_circle.py | 1 + 5 files changed, 64 insertions(+) rename flask_app.py => app.py (60%) create mode 100644 templates/circle.html create mode 100644 test_circle.py diff --git a/flask_app.py b/app.py similarity index 60% rename from flask_app.py rename to app.py index 8897fa2..980ca84 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', msg = f'The radius must be a number, not {type(radius)}.', + radius = '', area='', perimeter='') + if radius <= 0: + return render_template('circle.html', msg = f'The radius of the circle must be greater than zero. {radius} is less than zero.', + radius = '', area='', perimeter='') + 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 index e69de29..a509de3 100644 --- a/circle.py +++ 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/test_circle.py b/test_circle.py new file mode 100644 index 0000000..9f6050c --- /dev/null +++ b/test_circle.py @@ -0,0 +1 @@ +from circle import Circle From 1000f5cec3dba2d789a3d220547d2b83dbb45a99 Mon Sep 17 00:00:00 2001 From: Jackson Luckey Date: Tue, 12 Mar 2024 19:46:10 +0100 Subject: [PATCH 3/3] updated project --- app.py | 8 ++++---- templates/layout.html | 1 + test_circle.py | 24 ++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index 980ca84..157e0a8 100644 --- a/app.py +++ b/app.py @@ -49,11 +49,11 @@ def calculate_circle(): try: radius = float(radius) except: - return render_template('circle.html', msg = f'The radius must be a number, not {type(radius)}.', - radius = '', area='', perimeter='') + 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', msg = f'The radius of the circle must be greater than zero. {radius} is less than zero.', - radius = '', area='', perimeter='') + 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 = '', 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 index 9f6050c..ce56624 100644 --- a/test_circle.py +++ b/test_circle.py @@ -1 +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