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 1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from circle import Circle
import math
r = 2
ins = Circle(r)
print(ins.circle_perimeter())
print(round((math.pi * 2 * r), 2))
print(round((math.pi * r**2), 2))
print(ins.circle_area())
18 changes: 18 additions & 0 deletions circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import math

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

def circle_area(self):
return round((math.pi * self.radius**2), 2)

def circle_perimeter(self):
return round((math.pi * self.radius * 2), 2)

circle_instance = Circle(1)
check = circle_instance.circle_area()
check2 = circle_instance.circle_perimeter()

print(check)
print(check2)
22 changes: 22 additions & 0 deletions flask_app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from flask import Flask, render_template, request

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 +39,24 @@ 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 calculate_circle():
radius = '' # Set a default value for radius as empty string
if request.method == 'POST':
# using the request method from flask to request the values that were sent to the server through the POST method
radius = request.form.get('radius')

try:
radius = convert_to_float(value=radius)
except ValueError:
return render_template('circle.html', printed_result_area="Please type digits")


if radius is not None:
create_circle = Circle(float(radius)) # Convert radius to float
calc_area = create_circle.circle_area()
calc_per = create_circle.circle_perimeter()
return render_template('circle.html', printed_result_area=str(calc_area), printed_result_per=str(calc_per))
else:
return render_template('circle.html', printed_result="Radius not provided")
19 changes: 19 additions & 0 deletions templates/circle.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% extends 'layout.html' %}
{% block content %}
<h1>Circle</h1>
<form method="post">
<input type="text" name="radius" placeholder="Enter the radius" required="required" />

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

<br>

{% if printed_result_area is defined %}
<p>Area: {{ printed_result_area }}</p>
{% endif %}
{% if printed_result_per is defined %}
<p>Perimeter: {{ printed_result_per }}</p>
{% endif %}
{% endblock %}

14 changes: 14 additions & 0 deletions test_circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from circle import Circle
import math
r = 2
ins = Circle(r)
print(ins.circle_perimeter())

# Testing Perimeter function
assert ins.circle_perimeter() == round((math.pi * 2 * r), 2)

# Testing Area function
assert ins.circle_area() == round((math.pi * r**2), 2)