Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from math import pi
class Circle:
def __init__(self, radius):
self.radius = radius
def area(self):
return pow(self.radius,2) * pi

def perimeter(self):
return 2 * pi * self.radius
35 changes: 35 additions & 0 deletions flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

from helper import perform_calculation, convert_to_float

from circle import Circle

from test_circle import test_circle_area, test_circle_perimeter

app = Flask(__name__) # create the instance of the flask class


Expand Down Expand Up @@ -38,3 +42,34 @@ 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'])
def circle():
area = None # default
perimeter = None # default
radius = None # default
printed_result = ""
if request.method == 'POST':
radius_str = request.form.get('radius')
if radius_str:
try:
radius = float(radius_str)
if radius > 0:
circle_obj = Circle(radius)
area = "{:.2f}".format(circle_obj.area())
perimeter = "{:.2f}".format(circle_obj.perimeter())
else:
# If radius is not greater than 0, reset it to None and set the error message
radius = None
printed_result = "Radius must be a positive number."
except ValueError:
# If there's a ValueError, reset radius to None and set the error message
radius = None
printed_result = "Please enter a valid number for the radius."
return render_template('circle.html', radius=radius, area=area, perimeter=perimeter, printed_result=printed_result)

# Optional: Run app in debug Mode including the tests
# if __name__ == '__main__':
# test_circle_area()
# test_circle_perimeter()
# app.run(debug=True)
7 changes: 6 additions & 1 deletion static/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ body {
padding: 0;
}

/* Centered Text */
p {
text-align: center;
}

.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
padding: 10px;
}

/* Header Styles */
Expand Down
25 changes: 25 additions & 0 deletions templates/circle.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{% extends 'layout.html' %}
{% block content %}
<h1>Circle Operations</h1>
<div class="container">
<p>Input radius of circle here to find it's area and perimeter. </p>

<form action="{{ url_for('circle')}}" method="post">
<input type="text" name="radius" placeholder="Enter radius" required="required" />

<button type="submit">Calculate</button>
</form>

<br>

{% if radius %}
<p>For a radius of {{ radius }} the area and perimeter are the following:</p>
<p><strong>Area:</strong> {{ area }}</p>
<p><strong>Perimeter:</strong> {{ perimeter }}</p>
{% elif printed_result %}
<p>{{ printed_result }}</p>
{% endif %}

</div>

{% endblock %}
1 change: 1 addition & 0 deletions templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ <h1 class="logo"><a href="{{ url_for('home') }}">Home</a></h1>
<strong><nav>
<ul class="menu">
<li><a href="{{ url_for('calculate') }}">General Calculator</a></li>
<li><a href="{{ url_for('circle') }}">Circle Operations</a></li>
</ul>
</nav></strong>
</div>
Expand Down
21 changes: 21 additions & 0 deletions test_circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from circle import Circle
from math import pi

# Test case for the area method
def test_circle_area():
radius = 3
test_circle = Circle(radius)
expected_area = pi * radius ** 2
assert test_circle.area() == expected_area

# Test case for the perimeter method
def test_circle_perimeter():
radius = 3
test_circle = Circle(radius)
expected_perimeter = 2 * pi * radius
assert test_circle.perimeter() == expected_perimeter

if __name__ == "__main__":
test_circle_area()
test_circle_perimeter()
print("All tests passed!")