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
12 changes: 12 additions & 0 deletions circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# circle.py
import math

class Circle:
def __init__(self, radius):
self.radius = radius

def calculate_perimeter(self):
return 2 * math.pi * self.radius

def calculate_area(self):
return math.pi * (self.radius ** 2)
26 changes: 26 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 Down Expand Up @@ -38,3 +40,27 @@ 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':
try:
radius = convert_to_float(request.form.get('radius', ''))
circle = Circle(radius)
calc_method = request.form.get('calc_method')

if calc_method == 'area':
result = circle.calculate_area()
result_text = f"Area: {result}"
elif calc_method == 'perimeter':
result = circle.calculate_perimeter()
result_text = f"Perimeter: {result}"
else:
return render_template('circle_calculator.html', error="Invalid calculation method selected.")

return render_template('circle_calculator.html', result=result_text)
except ValueError as e:
return render_template('circle_calculator.html', error=str(e))
else:
return render_template('circle_calculator.html')
34 changes: 18 additions & 16 deletions templates/calculator.html
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
{% extends 'layout.html' %}
{% block content %}
<h1>Calculator</h1>
<form method="post">
<input type="text" name="value1" placeholder="Enter the first number" required="required" />
<input type="text" name="value2" placeholder="Enter the second number" required="required" />
<h1>Calculator</h1>
<form action="{{ url_for('calculate') }}" method="post">
<input type="text" name="value1" placeholder="Enter the first number" required="required" />
<input type="text" name="value2" placeholder="Enter the second number" required="required" />

<label for="operation">Operation</label>
<select id="operation" name="operation">
<option value="add">Add</option>
<option value="subtract">Subtract</option>
<option value="divide">Divide</option>
<option value="multiply">Multiply</option>
</select>
<label for="operation">Operation</label>
<select id="operation" name="operation">
<option value="add">Add</option>
<option value="subtract">Subtract</option>
<option value="divide">Divide</option>
<option value="multiply">Multiply</option>
</select>

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

<br>
<br>

{{ printed_result }}
{% if printed_result %}
<p>Result: {{ printed_result }}</p>
{% endif %}

{% endblock %}
{% endblock %}
33 changes: 33 additions & 0 deletions templates/circle_calculator.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{% extends 'layout.html' %}
{% block content %}
<!-- Circle Calculator Form -->
<h2>Circle Calculator</h2>
<form action="{{ url_for('calculate_circle') }}" method="post">
<input type="text" name="radius" placeholder="Enter the radius" required="required" />

<label for="calc_method">Calculation Method</label>
<select id="calc_method" name="calc_method">
<option value="area">Area</option>
<option value="perimeter">Perimeter</option>
</select>

<button type="submit">Calculate Circle Properties</button>

</form>

<br>

{% if result %}
{% if result.perimeter %}
<p>Perimeter: {{ result.perimeter }}</p>
<p>Area: {{ result.area }}</p>
{% else %}
<p>Result: {{ result }}</p>
{% endif %}
{% endif %}

{% if error %}
<p class="error">{{ error }}</p>
{% endif %}

{% endblock %}
11 changes: 6 additions & 5 deletions templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='main.css') }}">
</head>
<body>
<body>
<header>
<div id="navbar">
<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> <!-- Added URL for Circle Calculator -->
</ul>
</nav></strong>
</div>
</header>
<div class="container">
{% block content %}
{% endblock %}
<div class="container">
{% block content %}
{% endblock %}
</div>
</body>
</body>
</html>
20 changes: 20 additions & 0 deletions test_circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

import unittest
from circle import Circle


class TestCircle(unittest.TestCase):

def test_calculate_perimeter(self):
circle = Circle(5)
# Adjusted expected value to match the exact output
self.assertAlmostEqual(circle.calculate_perimeter(), 31.41592653589793, places=5)

def test_calculate_area(self):
circle = Circle(5)
# Adjusted expected value to match the exact output
self.assertAlmostEqual(circle.calculate_area(), 78.53981633974483, places=5)


if __name__ == '__main__':
unittest.main()