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
23 changes: 23 additions & 0 deletions flask_app.py → 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 Down Expand Up @@ -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='')
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 perimeter(self):
return pi * 2 * self.radius
def area(self):
return pi * self.radius**2
4 changes: 4 additions & 0 deletions static/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,7 @@ button {
button:hover {
background-color: #23272b; /* Slightly darker color on hover */
}

.center {
text-align: center;
}
27 changes: 27 additions & 0 deletions templates/circle.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{% extends 'layout.html' %}
{% block content %}
<h1>Circle Calculator</h1>
<form method="post">
<input type="text" name="radius" placeholder="Radius of the circle" required="required" />
<button type="submit">Calculate</button>
</form>

<br>

<div class='center'>
{{ msg }}
</div>
<div class='center'>
Radius: {{ radius }}
</div>
<br>
<div class='center'>
Area: {{ area }}
</div>
<br>
<div class='center'>
Perimeter: {{ perimeter }}
</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('calculate_circle')}}">Circle Calculator</a></li>
</ul>
</nav></strong>
</div>
Expand Down
25 changes: 25 additions & 0 deletions test_circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from circle import Circle
from math import pi
import random

'''
These tests are circular, but we're testing a toy example.
I could have also hardcoded some precalculated perimeters and areas and tested it that way.
I am not testing invalid inputs (e.g. radius = "five") here because I handle that via input validation in Flask.
I would normally test the Flask app itself, but that is out of scope for this problem set.

I run these tests via pytest test_circle.py
'''



def test_circle_perimeter():
radius = random.random() * 100
circle = Circle(radius)
assert circle.perimeter() == pi * 2 * radius


def test_circle_area():
radius = random.random() * 100
circle = Circle(radius)
assert circle.area() == pi * radius**2