Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,11 +4,53 @@ import math
|
|
| 4 |
|
| 5 |
st.set_page_config(page_title="Enhanced Calculator", page_icon=":calculator:", layout="wide")
|
| 6 |
|
| 7 |
-
# CSS
|
| 8 |
st.markdown(
|
| 9 |
"""
|
| 10 |
<style>
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
</style>
|
| 13 |
""",
|
| 14 |
unsafe_allow_html=True,
|
|
@@ -16,24 +58,29 @@ st.markdown(
|
|
| 16 |
|
| 17 |
st.title("Enhanced Calculator")
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
col1, col2, col3 = st.columns([1, 1, 1])
|
| 21 |
|
| 22 |
-
|
| 23 |
-
st.subheader("Input Numbers")
|
| 24 |
-
num1 = st.number_input("First Number", value=0.0)
|
| 25 |
-
num2 = st.number_input("Second Number", value=0.0)
|
| 26 |
-
num3 = st.number_input("Third Number", value=0.0)
|
| 27 |
|
| 28 |
-
with
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
"
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
with col3:
|
| 36 |
-
st.subheader("Result")
|
| 37 |
if st.button("Calculate"):
|
| 38 |
try:
|
| 39 |
if operation == "+":
|
|
@@ -46,7 +93,7 @@ with col3:
|
|
| 46 |
if num2 == 0 or num3 == 0:
|
| 47 |
raise ZeroDivisionError("Division by zero is not allowed.")
|
| 48 |
else:
|
| 49 |
-
result = num1 / num2 / num3
|
| 50 |
elif operation == "Power":
|
| 51 |
result = num1**num2
|
| 52 |
elif operation == "Square Root":
|
|
@@ -58,13 +105,32 @@ with col3:
|
|
| 58 |
result = num1**(1/3)
|
| 59 |
elif operation == "Average":
|
| 60 |
result = (num1 + num2 + num3) / 3
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
except (ZeroDivisionError, ValueError) as e:
|
| 64 |
-
st.error
|
| 65 |
except Exception as e:
|
| 66 |
-
st.
|
|
|
|
| 67 |
|
| 68 |
-
# Footer (same as before)
|
| 69 |
st.markdown("---")
|
| 70 |
st.write("Made with Streamlit")
|
|
|
|
| 4 |
|
| 5 |
st.set_page_config(page_title="Enhanced Calculator", page_icon=":calculator:", layout="wide")
|
| 6 |
|
| 7 |
+
# Custom CSS for a more professional look
|
| 8 |
st.markdown(
|
| 9 |
"""
|
| 10 |
<style>
|
| 11 |
+
body {
|
| 12 |
+
font-family: 'Arial', sans-serif; /* Modern font */
|
| 13 |
+
background-color: #f4f4f4; /* Light background */
|
| 14 |
+
}
|
| 15 |
+
.calculator-container {
|
| 16 |
+
border: 1px solid #ddd;
|
| 17 |
+
padding: 20px;
|
| 18 |
+
border-radius: 10px;
|
| 19 |
+
box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.1);
|
| 20 |
+
background-color: white;
|
| 21 |
+
}
|
| 22 |
+
.input-group {
|
| 23 |
+
margin-bottom: 15px;
|
| 24 |
+
}
|
| 25 |
+
.input-group label {
|
| 26 |
+
font-weight: bold;
|
| 27 |
+
margin-bottom: 5px;
|
| 28 |
+
}
|
| 29 |
+
.stButton>button {
|
| 30 |
+
background-color: #007bff; /* Blue button */
|
| 31 |
+
color: white;
|
| 32 |
+
padding: 12px 20px;
|
| 33 |
+
border: none;
|
| 34 |
+
border-radius: 5px;
|
| 35 |
+
cursor: pointer;
|
| 36 |
+
width: 100%; /* Full width button */
|
| 37 |
+
}
|
| 38 |
+
.stButton>button:hover {
|
| 39 |
+
background-color: #0056b3; /* Darker blue on hover */
|
| 40 |
+
}
|
| 41 |
+
.result-area {
|
| 42 |
+
margin-top: 20px;
|
| 43 |
+
padding: 15px;
|
| 44 |
+
border: 1px solid #ddd;
|
| 45 |
+
border-radius: 5px;
|
| 46 |
+
background-color: #f9f9f9;
|
| 47 |
+
font-size: 1.2em;
|
| 48 |
+
font-weight: bold;
|
| 49 |
+
}
|
| 50 |
+
.error-message {
|
| 51 |
+
color: red;
|
| 52 |
+
margin-top: 10px;
|
| 53 |
+
}
|
| 54 |
</style>
|
| 55 |
""",
|
| 56 |
unsafe_allow_html=True,
|
|
|
|
| 58 |
|
| 59 |
st.title("Enhanced Calculator")
|
| 60 |
|
| 61 |
+
with st.container() as calculator_container: # Enclose in a container for styling
|
|
|
|
| 62 |
|
| 63 |
+
col1, col2 = st.columns([1, 1]) # Two columns for inputs and operations
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
with col1:
|
| 66 |
+
st.subheader("Input Numbers")
|
| 67 |
+
num1 = st.number_input("First Number", value=0.0)
|
| 68 |
+
num2 = st.number_input("Second Number", value=0.0)
|
| 69 |
+
num3 = st.number_input("Third Number", value=0.0)
|
| 70 |
+
|
| 71 |
+
with col2:
|
| 72 |
+
st.subheader("Select Operation")
|
| 73 |
+
operation = st.selectbox(
|
| 74 |
+
"Operation",
|
| 75 |
+
[
|
| 76 |
+
"+", "-", "*", "/", "Power", "Square Root", "Cube Root", "Average",
|
| 77 |
+
"Logarithm", # Added Logarithm
|
| 78 |
+
"Sin", "Cos", "Tan", # Added Trigonometric functions
|
| 79 |
+
"Absolute Value", #Added Absolute Value function
|
| 80 |
+
"Factorial" #Added Factorial function
|
| 81 |
+
],
|
| 82 |
+
)
|
| 83 |
|
|
|
|
|
|
|
| 84 |
if st.button("Calculate"):
|
| 85 |
try:
|
| 86 |
if operation == "+":
|
|
|
|
| 93 |
if num2 == 0 or num3 == 0:
|
| 94 |
raise ZeroDivisionError("Division by zero is not allowed.")
|
| 95 |
else:
|
| 96 |
+
result = num1 / num2 / num3
|
| 97 |
elif operation == "Power":
|
| 98 |
result = num1**num2
|
| 99 |
elif operation == "Square Root":
|
|
|
|
| 105 |
result = num1**(1/3)
|
| 106 |
elif operation == "Average":
|
| 107 |
result = (num1 + num2 + num3) / 3
|
| 108 |
+
elif operation == "Logarithm":
|
| 109 |
+
if num1 <= 0:
|
| 110 |
+
raise ValueError("Logarithm of a non-positive number is not allowed.")
|
| 111 |
+
else:
|
| 112 |
+
result = math.log(num1)
|
| 113 |
+
elif operation == "Sin":
|
| 114 |
+
result = math.sin(math.radians(num1)) # Convert to radians
|
| 115 |
+
elif operation == "Cos":
|
| 116 |
+
result = math.cos(math.radians(num1)) # Convert to radians
|
| 117 |
+
elif operation == "Tan":
|
| 118 |
+
result = math.tan(math.radians(num1)) # Convert to radians
|
| 119 |
+
elif operation == "Absolute Value":
|
| 120 |
+
result = abs(num1)
|
| 121 |
+
elif operation == "Factorial":
|
| 122 |
+
if num1 < 0 or not num1.is_integer():
|
| 123 |
+
raise ValueError("Factorial is only defined for non-negative integers.")
|
| 124 |
+
else:
|
| 125 |
+
result = math.factorial(int(num1))
|
| 126 |
+
|
| 127 |
+
st.markdown(f"<div class='result-area'>Result: {result}</div>", unsafe_allow_html=True)
|
| 128 |
|
| 129 |
except (ZeroDivisionError, ValueError) as e:
|
| 130 |
+
st.markdown(f"<div class='error-message'>{str(e)}</div>", unsafe_allow_html=True)
|
| 131 |
except Exception as e:
|
| 132 |
+
st.markdown(f"<div class='error-message'>An error occurred: {e}</div>", unsafe_allow_html=True)
|
| 133 |
+
|
| 134 |
|
|
|
|
| 135 |
st.markdown("---")
|
| 136 |
st.write("Made with Streamlit")
|