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
8 changes: 8 additions & 0 deletions circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def circle_ops(radius: float, operation: str) -> float: # Define the 'circle_ops' class
print(radius)
if operation == 'perimeter':
result = 2.0 * 3.14 * radius # Define the perimeter method
else:
result = 3.14 * (radius**2.0) # Define the area method

return result
31 changes: 29 additions & 2 deletions flask_app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from flask import Flask, render_template, request

from helper import perform_calculation, convert_to_float
from circle import circle_ops

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

Expand All @@ -10,7 +10,6 @@
def home():
return render_template('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 +37,31 @@ 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']) # associating the GET and POST method with this route
def circle():
if request.method == 'POST':
# print(request.form['radius'])
# using the request method from flask to request the values that were sent to the server through the POST method
radius = request.form['radius']
operation = str(request.form['operation'])

# make sure the input is one of the allowed inputs (not absolutely necessary in the drop-down case)
if operation not in ['perimeter', 'area']:
return render_template('area_calculator.html',
printed_result='Operation must be one of "perimeter" or "area".')

try:
radius = float(radius)
# print(radius)
except ValueError:
return render_template('area_calculator.html', printed_result="Cannot perform operation with this input")

try:
result = circle_ops(radius=radius, operation=operation)
return render_template('area_calculator.html', printed_result=f'{operation}: {result}')

except ValueError:
return render_template('area_calculator.html', printed_result="Radius cannot be a negative number.")

return render_template('area_calculator.html')
3 changes: 3 additions & 0 deletions static/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,6 @@ button {
button:hover {
background-color: #23272b; /* Slightly darker color on hover */
}

.center{
}
21 changes: 21 additions & 0 deletions templates/area_calculator.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% extends 'layout.html' %}
{% block content %}
<h1>Area and Perimeter Calculator for a Circle</h1>
<form method="post">
<input type="text" name="radius" placeholder="Enter radius of the circle" required="required" />

<label for="operation">Enter quantity to calculate:</label>
<select id="operation" name="operation">
<option value="perimeter">Perimeter</option>
<option value="area">Area</option>
</select>

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

<br>
<p style="text-align: center;">

{{ printed_result }}
</p>
{% endblock %}
2 changes: 1 addition & 1 deletion templates/calculator.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ <h1>Calculator</h1>
</form>

<br>

<div class="center">
{{ 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
10 changes: 10 additions & 0 deletions test_circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pytest
from circle import circle_ops

def test_circle_perimeter():
perimeter = circle_ops(radius=5, operation='perimeter')
assert perimeter == 2 * 3.14 * 5

def test_circle_area():
area = circle_ops(radius=5, operation='area')
assert area == 3.14 * (5 ** 2)