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
32 changes: 29 additions & 3 deletions flask_app.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# Import Flask class
from flask import Flask, render_template, request

from helper import perform_calculation, convert_to_float
from helper_circle import circle_perform

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


@app.route('/')
@app.route('/home')
def home():
return render_template('home.html')


#Creating home route and returning home HTML
@app.route('/calculate', methods=['GET', 'POST']) # associating the GET and POST method with this route
def calculate():
if request.method == 'POST':
Expand Down Expand Up @@ -38,3 +39,28 @@ def calculate():
return render_template('calculator.html', printed_result="You cannot divide by zero")

return render_template('calculator.html')

#Creating circle route and returning circle HTML
@app.route('/circle', methods=['GET', 'POST']) # associating the GET and POST method with this route
def circle():
printed_result = None

if request.method == 'POST':
radius = float(request.form['radius'])
operation = request.form['operation']

# Create an instance
helper_circle = circle_perform(radius)

if radius < 0:
printed_result = 'Radius must not be defined as negative.'
elif operation not in ['Perimeter', 'Area']:
printed_result = 'Operation must be one of "Perimeter", or "Area".'
elif operation == 'Perimeter':
result = helper_circle.perimeter()
printed_result = f"Perimeter: {result}"
else:
result = helper_circle.area()
printed_result = f"Area: {result}"

return render_template('circle.html', printed_result=printed_result)
9 changes: 9 additions & 0 deletions helper_circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# create helper_circle functions for circle calculations
import math
class circle_perform:
def __init__(self, radius: float):
self.radius=radius
def perimeter(self) -> float:
return 2*math.pi*self.radius
def area(self) -> float:
return math.pi*(self.radius**2)
20 changes: 20 additions & 0 deletions templates/circle.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% extends 'layout.html' %}
{% block content %}
<h1>Perimeter and Area Calculator</h1>
<form method="post" action="circle">
<input type="text" name="radius" placeholder="Enter the radius" required="required" />

<label for="operation">Operation</label>
<select id="operation" name="operation">
<option value="Perimeter">Perimeter</option>
<option value="Area">Area</option>
</select>

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

<br>

{{ printed_result }}

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

def perimeter_test():
#Create an instance radius=1
helper_circle = circle_perform(1)

perimeter = helper_circle.perimeter()
assert round(perimeter, 8) == round(2 * math.pi, 8)

def area_test():
#Create an instance radius=1
helper_circle = circle_perform(1)

area = helper_circle.area
assert round(area, 8) == round(math.pi, 8)