Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

calculator_app_ilyina #25

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
Binary file added .DS_Store
Binary file not shown.
17 changes: 17 additions & 0 deletions circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## create functions for circle calculations

# import package
from math import pi

# define class for circle calculations
class Circle:
def __init__(self, radius):
self.radius = radius

def area(self):
result = pi * self.radius**2
return f"This circle's area is: {round(result, 4)}"

def perimeter(self):
result = 2 * pi * self.radius
return f"This circle's perimeter is: {round(result, 4)}"
45 changes: 41 additions & 4 deletions flask_app.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from flask import Flask, render_template, request
## structure and run flask app

from flask import Flask, render_template, request
from circle import Circle
from helper import perform_calculation, convert_to_float

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


@app.route('/')
@app.route('/home')
def home():
Expand All @@ -28,13 +29,49 @@ def calculate():
value1 = convert_to_float(value=value1)
value2 = convert_to_float(value=value2)
except ValueError:
return render_template('calculator.html', printed_result="Cannot perform operation with this input")
return render_template('calculator.html', printed_result="Cannot perform operation with this input.")

try:
result = perform_calculation(value1=value1, value2=value2, operation=operation)
return render_template('calculator.html', printed_result=str(result))

except ZeroDivisionError:
return render_template('calculator.html', printed_result="You cannot divide by zero")
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':
# use request method from flask
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 ['area', 'perimeter']:
return render_template('circle.html',
printed_result='Operation must be either "Area" or "Perimeter".')

# convert to float
try:
radius = convert_to_float(value=radius)
except ValueError:
return render_template('circle.html', printed_result="Cannot perform operation with this input.")


# make sure the radius is positive
if radius < 0:
return render_template('circle.html',
printed_result='Radius cannot be negative.')

# run circle function
try:
result = Circle(radius=radius, operation=operation)
return render_template('circle.html', printed_result=str(result))
except Exception:
return render_template('circle.html', printed_result=str("An error occurred."))

return render_template('circle.html')


3 changes: 1 addition & 2 deletions helper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# create helper functions for calculations

# create helper functions for general calculations

def perform_calculation(value1: float, value2: float, operation: str) -> float:
"""
Expand Down
Binary file added static/calculator.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/circle_calc.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions static/main.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Global Styles */
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f8f9fa;
background-color: #c3e0f7;
color: #333;
margin: 0;
padding: 0;
Expand All @@ -15,7 +15,7 @@ body {

/* Header Styles */
header {
background-color: #343a40; /* Dark neutral color for header */
background-color: #194e7a; /* Light blue neutral color for header */
color: #fff; /* Light text color for header */
padding: 10px 0;
}
Expand Down Expand Up @@ -51,15 +51,15 @@ header {
}

h1 a {
color: #ffc8dd;
color: #c3e0f7;
}

h1 a:hover {
color: #ffafcc;
color: #fff;
}

.menu li a:hover {
color: #bde0fe; /* Light pastel color on hover */
color: #c3e0f7; /* Light pastel color on hover */
}

/* Calculator Styles */
Expand Down
10 changes: 9 additions & 1 deletion templates/calculator.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
{% extends 'layout.html' %}
{% block content %}
<h1>Calculator</h1>
<h1>General Calculator</h1>
<br>
<h3>Let's crunch some numbers!</h3>
<ol>
<li>Enter two values</li>
<li>Select an operator you want to use for your calculation</li>
<li>Get your result!</li>
</ol>
<br>
<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" />
Expand Down
28 changes: 28 additions & 0 deletions templates/circle.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{% extends 'layout.html' %}
{% block content %}
<h1>Circle calculator</h1>
<br>
<h3>Let's find your circle's area or perimeter!</h3>
<ol>
<li>Enter a radius</li>
<li>Select an option you want to calculate</li>
<li>Get your result!</li>
</ol>
<br>
<form method="post">
<input type="text" name="radius" placeholder="Enter the radius" required="required" />

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

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

<br>

{{ printed_result }}

{% endblock %}
22 changes: 21 additions & 1 deletion templates/home.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
{% extends 'layout.html' %}
{% block content %}
<h1>Welcome!</h1>
<p>Use the navigation bar, to find the calculator you need.</p>
<p>This page was created to help you with your calculations.</p>
<p>Use the navigation bar above or the buttons below to find the calculator you need.</p>
<br>
<div style="display: flex; align-items: top;">
<div style="flex: 1;">
<strong><nav>
<ul class="menu">
<li><a href="{{ url_for('calculate') }}" style="color: #194e7a;">General Calculator</a></li>
</ul>
</nav></strong>
<img src="{{url_for('static', filename='calculator.jpeg')}}" style="width: 100%; max-width: 120px; height: auto;" align="middle" />
</div>
<div style="flex: 1;">
<strong><nav>
<ul class="menu">
<li><a href="{{ url_for('circle') }}" style="color: #194e7a;">Circle Calculator</a></li>
</ul>
</nav></strong>
<img src="{{url_for('static', filename='circle_calc.jpeg')}}" style="width: 100%; max-width: 150px; height: auto;" align="middle" />
</div>
</div>
{% endblock %}
5 changes: 5 additions & 0 deletions templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ <h1 class="logo"><a href="{{ url_for('home') }}">Home</a></h1>
<li><a href="{{ url_for('calculate') }}">General Calculator</a></li>
</ul>
</nav></strong>
<strong><nav>
<ul class="menu">
<li><a href="{{ url_for('circle') }}">Circle Calculator</a></li>
</ul>
</nav></strong>
</div>
</header>
<div class="container">
Expand Down
20 changes: 20 additions & 0 deletions test_circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## create helper functions for circle calculations

# import circle class from circle.py
from circle import Circle
from math import pi

# test area function
def test_area():
circle = calc_circles(radius = 1)
expected_res = f"This circle's area is: {round(pi * 1 ** 2, 4)}"
actual_res = circle.area("area")
assert actual_res == expected_res


# test area function
def test_perimeter():
circle = calc_circles(radius = 1)
expected_res = f"This circle's perimeter is: {round(result, 4)}"
actual_res = circle.perimeter("perimeter")
assert actual_res == expected_res