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 @@
#create circle class
import numpy as np
class Circle:
def __init__(self, radius):
self.radius = radius
def area(self):
return np.pi * self.radius ** 2
def perimeter(self):
return 2 * np.pi * self.radius
20 changes: 20 additions & 0 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 @@ -10,6 +12,24 @@
def home():
return render_template('home.html')

@app.route('/circle', methods=['GET', 'POST'])
def circle():
if request.method == 'POST':
radius = request.form['radius']

try:
radius = convert_to_float(radius)
if radius <= 0:
return render_template('circle.html', printed_result = "Must be a positive Number.")

circle = Circle(radius=radius)
area = circle.area()
perimeter = circle.perimeter()
return render_template('circle.html', printed_result = f'The area of you circle is {str(area)} and the perimeter is {str(perimeter)}')
except ValueError:
return render_template('circle.html', printed_result = "Invalid input!")

return render_template('circle.html')

@app.route('/calculate', methods=['GET', 'POST']) # associating the GET and POST method with this route
def calculate():
Expand Down
12 changes: 12 additions & 0 deletions templates/circle.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends 'layout.html' %}
{% block content %}
<h1>Circle area and perimeter calculations</h1>
<form method="post">
<input type="text" name="radius" placeholder="Enter the radius of the circle here" 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
12 changes: 12 additions & 0 deletions test_circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import numpy as np
from circle import Circle

def test_perimeter():
circle = Circle(1)
expected_result = 2 * np.pi * 1
assert circle.perimeter() == expected_result

def test_area():
circle = Circle(1)
expected_result = np.pi * 1 ** 2
assert circle.area() == expected_result