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 %} +