diff --git a/circle.py b/circle.py new file mode 100644 index 0000000..fd68163 --- /dev/null +++ b/circle.py @@ -0,0 +1,8 @@ +def circle_ops(radius: float, operation: str) -> float: # Define the 'circle_ops' class + print(radius) + if operation == 'perimeter': + result = 2.0 * 3.14 * radius # Define the perimeter method + else: + result = 3.14 * (radius**2.0) # Define the area method + + return result \ No newline at end of file diff --git a/flask_app.py b/flask_app.py index 8897fa2..0631304 100644 --- a/flask_app.py +++ b/flask_app.py @@ -1,6 +1,6 @@ from flask import Flask, render_template, request - from helper import perform_calculation, convert_to_float +from circle import circle_ops app = Flask(__name__) # create the instance of the flask class @@ -10,7 +10,6 @@ def home(): return render_template('home.html') - @app.route('/calculate', methods=['GET', 'POST']) # associating the GET and POST method with this route def calculate(): if request.method == 'POST': @@ -38,3 +37,31 @@ 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']) # associating the GET and POST method with this route +def circle(): + if request.method == 'POST': + # print(request.form['radius']) + # using the request method from flask to request the values that were sent to the server through the POST method + radius = request.form['radius'] + operation = str(request.form['operation']) + + # make sure the input is one of the allowed inputs (not absolutely necessary in the drop-down case) + if operation not in ['perimeter', 'area']: + return render_template('area_calculator.html', + printed_result='Operation must be one of "perimeter" or "area".') + + try: + radius = float(radius) + # print(radius) + except ValueError: + return render_template('area_calculator.html', printed_result="Cannot perform operation with this input") + + try: + result = circle_ops(radius=radius, operation=operation) + return render_template('area_calculator.html', printed_result=f'{operation}: {result}') + + except ValueError: + return render_template('area_calculator.html', printed_result="Radius cannot be a negative number.") + + return render_template('area_calculator.html') diff --git a/static/main.css b/static/main.css index dfe5192..4114b46 100644 --- a/static/main.css +++ b/static/main.css @@ -97,3 +97,6 @@ button { button:hover { background-color: #23272b; /* Slightly darker color on hover */ } + +.center{ +} \ No newline at end of file diff --git a/templates/area_calculator.html b/templates/area_calculator.html new file mode 100644 index 0000000..f7a89c1 --- /dev/null +++ b/templates/area_calculator.html @@ -0,0 +1,21 @@ +{% extends 'layout.html' %} +{% block content %} +
+ + {{ printed_result }} +
+{% endblock %} \ No newline at end of file diff --git a/templates/calculator.html b/templates/calculator.html index 3dd1105..ff2264c 100644 --- a/templates/calculator.html +++ b/templates/calculator.html @@ -17,7 +17,7 @@