Mavhas's picture
Update app.py
d0b1c0e verified
# app.py
import streamlit as st
import math
st.set_page_config(page_title="Enhanced Calculator", page_icon=":calculator:", layout="wide")
# Custom CSS for a more professional look
st.markdown(
"""
<style>
body {
font-family: 'Arial', sans-serif; /* Modern font */
background-color: #f4f4f4; /* Light background */
}
.calculator-container {
border: 1px solid #ddd;
padding: 20px;
border-radius: 10px;
box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.1);
background-color: white;
}
.input-group {
margin-bottom: 15px;
}
.input-group label {
font-weight: bold;
margin-bottom: 5px;
}
.stButton>button {
background-color: #007bff; /* Blue button */
color: white;
padding: 12px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
width: 100%; /* Full width button */
}
.stButton>button:hover {
background-color: #0056b3; /* Darker blue on hover */
}
.result-area {
margin-top: 20px;
padding: 15px;
border: 1px solid #ddd;
border-radius: 5px;
background-color: #f9f9f9;
font-size: 1.2em;
font-weight: bold;
}
.error-message {
color: red;
margin-top: 10px;
}
</style>
""",
unsafe_allow_html=True,
)
st.title("Enhanced Calculator")
with st.container() as calculator_container: # Enclose in a container for styling
col1, col2 = st.columns([1, 1]) # Two columns for inputs and operations
with col1:
st.subheader("Input Numbers")
num1 = st.number_input("First Number", value=0.0)
num2 = st.number_input("Second Number", value=0.0)
num3 = st.number_input("Third Number", value=0.0)
with col2:
st.subheader("Select Operation")
operation = st.selectbox(
"Operation",
[
"+", "-", "*", "/", "Power", "Square Root", "Cube Root", "Average",
"Logarithm", # Added Logarithm
"Sin", "Cos", "Tan", # Added Trigonometric functions
"Absolute Value", #Added Absolute Value function
"Factorial" #Added Factorial function
],
)
if st.button("Calculate"):
try:
if operation == "+":
result = num1 + num2 + num3
elif operation == "-":
result = num1 - num2 - num3
elif operation == "*":
result = num1 * num2 * num3
elif operation == "/":
if num2 == 0 or num3 == 0:
raise ZeroDivisionError("Division by zero is not allowed.")
else:
result = num1 / num2 / num3
elif operation == "Power":
result = num1**num2
elif operation == "Square Root":
if num1 < 0:
raise ValueError("Square root of a negative number is not allowed.")
else:
result = math.sqrt(num1)
elif operation == "Cube Root":
result = num1**(1/3)
elif operation == "Average":
result = (num1 + num2 + num3) / 3
elif operation == "Logarithm":
if num1 <= 0:
raise ValueError("Logarithm of a non-positive number is not allowed.")
else:
result = math.log(num1)
elif operation == "Sin":
result = math.sin(math.radians(num1)) # Convert to radians
elif operation == "Cos":
result = math.cos(math.radians(num1)) # Convert to radians
elif operation == "Tan":
result = math.tan(math.radians(num1)) # Convert to radians
elif operation == "Absolute Value":
result = abs(num1)
elif operation == "Factorial":
if num1 < 0 or not num1.is_integer():
raise ValueError("Factorial is only defined for non-negative integers.")
else:
result = math.factorial(int(num1))
st.markdown(f"<div class='result-area'>Result: {result}</div>", unsafe_allow_html=True)
except (ZeroDivisionError, ValueError) as e:
st.markdown(f"<div class='error-message'>{str(e)}</div>", unsafe_allow_html=True)
except Exception as e:
st.markdown(f"<div class='error-message'>An error occurred: {e}</div>", unsafe_allow_html=True)
st.markdown("---")
st.write("Made with Streamlit")