diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..d15f3cf Binary files /dev/null and b/.DS_Store differ diff --git a/circle.py b/circle.py new file mode 100644 index 0000000..721ee6f --- /dev/null +++ b/circle.py @@ -0,0 +1,17 @@ +## create functions for circle calculations + +# import package +from math import pi + +# define class for circle calculations +class Circle: + def __init__(self, radius): + self.radius = radius + + def area(self): + result = pi * self.radius**2 + return f"This circle's area is: {round(result, 4)}" + + def perimeter(self): + result = 2 * pi * self.radius + return f"This circle's perimeter is: {round(result, 4)}" \ No newline at end of file diff --git a/flask_app.py b/flask_app.py index 8897fa2..f02b0be 100644 --- a/flask_app.py +++ b/flask_app.py @@ -1,10 +1,11 @@ -from flask import Flask, render_template, request +## structure and run flask app +from flask import Flask, render_template, request +from circle import Circle from helper import perform_calculation, convert_to_float app = Flask(__name__) # create the instance of the flask class - @app.route('/') @app.route('/home') def home(): @@ -28,13 +29,49 @@ def calculate(): value1 = convert_to_float(value=value1) value2 = convert_to_float(value=value2) except ValueError: - return render_template('calculator.html', printed_result="Cannot perform operation with this input") + return render_template('calculator.html', printed_result="Cannot perform operation with this input.") try: result = perform_calculation(value1=value1, value2=value2, operation=operation) return render_template('calculator.html', printed_result=str(result)) except ZeroDivisionError: - return render_template('calculator.html', printed_result="You cannot divide by zero") + 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': + # use request method from flask + 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 ['area', 'perimeter']: + return render_template('circle.html', + printed_result='Operation must be either "Area" or "Perimeter".') + + # convert to float + try: + radius = convert_to_float(value=radius) + except ValueError: + return render_template('circle.html', printed_result="Cannot perform operation with this input.") + + + # make sure the radius is positive + if radius < 0: + return render_template('circle.html', + printed_result='Radius cannot be negative.') + + # run circle function + try: + result = Circle(radius=radius, operation=operation) + return render_template('circle.html', printed_result=str(result)) + except Exception: + return render_template('circle.html', printed_result=str("An error occurred.")) + + return render_template('circle.html') + + diff --git a/helper.py b/helper.py index 31e82d6..5b5f982 100644 --- a/helper.py +++ b/helper.py @@ -1,5 +1,4 @@ -# create helper functions for calculations - +# create helper functions for general calculations def perform_calculation(value1: float, value2: float, operation: str) -> float: """ diff --git a/static/calculator.jpeg b/static/calculator.jpeg new file mode 100644 index 0000000..4f5f59e Binary files /dev/null and b/static/calculator.jpeg differ diff --git a/static/circle_calc.jpeg b/static/circle_calc.jpeg new file mode 100644 index 0000000..5db8242 Binary files /dev/null and b/static/circle_calc.jpeg differ diff --git a/static/main.css b/static/main.css index dfe5192..6081a9d 100644 --- a/static/main.css +++ b/static/main.css @@ -1,7 +1,7 @@ /* Global Styles */ body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; - background-color: #f8f9fa; + background-color: #c3e0f7; color: #333; margin: 0; padding: 0; @@ -15,7 +15,7 @@ body { /* Header Styles */ header { - background-color: #343a40; /* Dark neutral color for header */ + background-color: #194e7a; /* Light blue neutral color for header */ color: #fff; /* Light text color for header */ padding: 10px 0; } @@ -51,15 +51,15 @@ header { } h1 a { - color: #ffc8dd; + color: #c3e0f7; } h1 a:hover { - color: #ffafcc; + color: #fff; } .menu li a:hover { - color: #bde0fe; /* Light pastel color on hover */ + color: #c3e0f7; /* Light pastel color on hover */ } /* Calculator Styles */ diff --git a/templates/calculator.html b/templates/calculator.html index 3dd1105..ef2a43a 100644 --- a/templates/calculator.html +++ b/templates/calculator.html @@ -1,6 +1,14 @@ {% extends 'layout.html' %} {% block content %} -

Calculator

+

General Calculator

+
+

Let's crunch some numbers!

+
    +
  1. Enter two values
  2. +
  3. Select an operator you want to use for your calculation
  4. +
  5. Get your result!
  6. +
+
diff --git a/templates/circle.html b/templates/circle.html new file mode 100644 index 0000000..fc0bbef --- /dev/null +++ b/templates/circle.html @@ -0,0 +1,28 @@ +{% extends 'layout.html' %} +{% block content %} +

Circle calculator

+
+

Let's find your circle's area or perimeter!

+
    +
  1. Enter a radius
  2. +
  3. Select an option you want to calculate
  4. +
  5. Get your result!
  6. +
+
+ + + + + + + +
+ +
+ + {{ printed_result }} + +{% endblock %} diff --git a/templates/home.html b/templates/home.html index 6516181..4b25f1d 100644 --- a/templates/home.html +++ b/templates/home.html @@ -1,5 +1,25 @@ {% extends 'layout.html' %} {% block content %}

Welcome!

-

Use the navigation bar, to find the calculator you need.

+

This page was created to help you with your calculations.

+

Use the navigation bar above or the buttons below to find the calculator you need.

+
+
+
+ + +
+
+ + +
+
{% endblock %} \ No newline at end of file diff --git a/templates/layout.html b/templates/layout.html index 8de2e62..b9b6f4a 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -14,6 +14,11 @@

Home

  • General Calculator
  • +
    diff --git a/test_circle.py b/test_circle.py new file mode 100644 index 0000000..269d6f3 --- /dev/null +++ b/test_circle.py @@ -0,0 +1,20 @@ +## create helper functions for circle calculations + +# import circle class from circle.py +from circle import Circle +from math import pi + +# test area function +def test_area(): + circle = calc_circles(radius = 1) + expected_res = f"This circle's area is: {round(pi * 1 ** 2, 4)}" + actual_res = circle.area("area") + assert actual_res == expected_res + + +# test area function +def test_perimeter(): + circle = calc_circles(radius = 1) + expected_res = f"This circle's perimeter is: {round(result, 4)}" + actual_res = circle.perimeter("perimeter") + assert actual_res == expected_res