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
16 changes: 16 additions & 0 deletions circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import math


class Circle:
def __init__(self, radius):
if radius < 0:
raise ValueError('Radius cannot be negative')
self.radius = radius

def perimeter(self):
# Calculate the perimeter of a circle
return 2 * math.pi * self.radius

def area(self):
# Calculate the area of a circle
return math.pi * (self.radius ** 2)
33 changes: 31 additions & 2 deletions flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -11,7 +13,8 @@ def home():
return render_template('home.html')


@app.route('/calculate', methods=['GET', 'POST']) # associating the GET and POST method with this route
# associating the GET and POST method with this route
@app.route('/calculate', methods=['GET', 'POST'])
def calculate():
if request.method == 'POST':
# using the request method from flask to request the values that were sent to the server through the POST method
Expand All @@ -31,10 +34,36 @@ def calculate():
return render_template('calculator.html', printed_result="Cannot perform operation with this input")

try:
result = perform_calculation(value1=value1, value2=value2, operation=operation)
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')

# Below is the part that I added.
# It's the homepage to calculat the circie's perimeter and area.


@app.route('/circle', methods=['GET', 'POST'])
def circle():
if request.method == 'POST':
radius = request.form['radius']
try:
radius = convert_to_float(value=radius)
except ValueError:
# I already made circlecal.html in the templates folder.
return render_template('circlecal.html', printed_result="Cannot perform operation with this input")
try:
# Below is the process calculating perimeter and area
circle = Circle(radius)
# For simplicity, I applied the 'round' function.
perimeter = round(circle.perimeter(), 2)
area = round(circle.area(), 2)
return render_template('circlecal.html', printed_result=f"Perimeter: {perimeter}, Area: {area}")
except ValueError:
return render_template('circlecal.html', printed_result="You cannot input negative value")

return render_template('circlecal.html')
13 changes: 13 additions & 0 deletions templates/circlecal.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% extends 'layout.html' %}
{% block content %}
<h1>Circle Calculator</h1>
<form method="post">
<input type="text" name="radius" placeholder="Enter the radius" required="required" />
<button type="submit">Calculate</button>
</form>

<br>

{{ printed_result }}

{% 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 Calculator</a></li>
</ul>
</nav></strong>
</div>
Expand Down
9 changes: 9 additions & 0 deletions test_circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from circle import Circle
import math

radius = Circle(5)


def test_circle():
assert radius.perimeter() == 2 * math.pi * 5, 'The perimeter method is wrong'
assert radius.area() == math.pi * (5 ** 2), 'The area method is wrong'