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
19 changes: 19 additions & 0 deletions circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from math import pi
class Circle:
def __init__(self, radius):
self.radius = radius

def perimeter(self):
return 2 * 3.14 * self.radius

def area(self):
return 3.14 * self.radius * self.radius

def circle_calc(radius, operation):
c = Circle(float(radius))
if operation == 'perimeter':
return c.perimeter()
elif operation == 'area':
return c.area()
else:
return "Invalid operation"
18 changes: 18 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_calc

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


Expand Down Expand Up @@ -38,3 +40,19 @@ 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_calculation():
if request.method == 'POST':
radius = request.form['radius']
operation = request.form['operation']
result = circle_calc(radius, operation)
return render_template('circle.html', printed_result=result)
else:
return render_template('circle.html')



if __name__ == '__main__':
app.run(debug=True) # run the application
24 changes: 24 additions & 0 deletions templates/circle.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{% extends 'layout.html' %}
{% block content %}
<head>
<title>Circle Zone</title>
</head>

<body>
<h1>Circle</h1>

<form action="{{ url_for('circle_calculation')}}" method="post">
<input type="text" name="radius" placeholder="Enter the radius" required="required" />
<select name="operation" required="required">
<option value="perimeter">perimeter</option>
<option value="area">area</option>
</select>
<button type="submit">Calculate</button>
</form>

<br>

{{ printed_result }}

</body>
{% 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_calculation') }}">Circle Zone</a></li>
</ul>
</nav></strong>
</div>
Expand Down
16 changes: 16 additions & 0 deletions test_circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from circle import Circle, circle_calc
import pytest
import numpy as np
from math import pi

# Testing area calculation
radius = 3
def area_test():
circle = Circle(radius)
expected = np.pi * radius ** 2
assert Circle(radius).area() == expected

def perimeter_test():
circle = Circle(radius)
expected = 2 * np.pi * radius
assert Circle(radius).perimeter() == expected