Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,47 +2,84 @@
|
|
| 2 |
import streamlit as st
|
| 3 |
import math
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
st.title("Enhanced Calculator")
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
# Operation selection
|
| 13 |
-
operation = st.selectbox(
|
| 14 |
-
"Select operation",
|
| 15 |
-
["+", "-", "*", "/", "Power", "Square Root", "Cube Root", "Average"],
|
| 16 |
-
)
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
if operation == "+":
|
| 22 |
-
result = num1 + num2 + num3
|
| 23 |
-
elif operation == "-":
|
| 24 |
-
result = num1 - num2 - num3
|
| 25 |
-
elif operation == "*":
|
| 26 |
-
result = num1 * num2 * num3
|
| 27 |
-
elif operation == "/":
|
| 28 |
-
if num2 == 0 or num3 == 0:
|
| 29 |
-
raise ZeroDivisionError("Division by zero is not allowed.") # Raise exception
|
| 30 |
-
else:
|
| 31 |
-
result = num1 / num2 / num3
|
| 32 |
-
elif operation == "Power":
|
| 33 |
-
result = num1**num2
|
| 34 |
-
elif operation == "Square Root":
|
| 35 |
-
if num1 < 0:
|
| 36 |
-
raise ValueError("Square root of a negative number is not allowed.") # Raise exception
|
| 37 |
-
else:
|
| 38 |
-
result = math.sqrt(num1)
|
| 39 |
-
elif operation == "Cube Root":
|
| 40 |
-
result = num1**(1/3)
|
| 41 |
-
elif operation == "Average":
|
| 42 |
-
result = (num1 + num2 + num3) / 3
|
| 43 |
-
st.write(f"Result: {result}")
|
| 44 |
-
|
| 45 |
-
except (ZeroDivisionError, ValueError) as e: # Catch specific exceptions
|
| 46 |
-
st.error(str(e)) # Display the error message to the user
|
| 47 |
-
except Exception as e: # Catch any other unexpected exceptions
|
| 48 |
-
st.error(f"An error occurred: {e}") # Display a generic error message
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
import math
|
| 4 |
|
| 5 |
+
st.set_page_config(page_title="Enhanced Calculator", page_icon=":calculator:", layout="wide") # Page configuration
|
| 6 |
+
|
| 7 |
+
# Add some styling with CSS
|
| 8 |
+
st.markdown(
|
| 9 |
+
"""
|
| 10 |
+
<style>
|
| 11 |
+
body {
|
| 12 |
+
font-family: sans-serif;
|
| 13 |
+
}
|
| 14 |
+
.calculator-container {
|
| 15 |
+
border: 2px solid #ccc;
|
| 16 |
+
padding: 20px;
|
| 17 |
+
border-radius: 10px;
|
| 18 |
+
box-shadow: 5px 5px 10px rgba(0,0,0,0.2); /* Add a subtle shadow */
|
| 19 |
+
background-color: #f8f9fa; /* Light background color */
|
| 20 |
+
}
|
| 21 |
+
.input-col {
|
| 22 |
+
width: 30%; /* Adjust input column width */
|
| 23 |
+
float: left;
|
| 24 |
+
padding: 10px;
|
| 25 |
+
}
|
| 26 |
+
.operation-col {
|
| 27 |
+
width: 40%; /* Adjust operation column width */
|
| 28 |
+
float: left;
|
| 29 |
+
padding: 10px;
|
| 30 |
+
}
|
| 31 |
+
.result-col {
|
| 32 |
+
width: 30%; /* Adjust result column width */
|
| 33 |
+
float: left;
|
| 34 |
+
padding: 10px;
|
| 35 |
+
}
|
| 36 |
+
.stButton>button {
|
| 37 |
+
background-color: #4CAF50; /* Green button */
|
| 38 |
+
color: white;
|
| 39 |
+
padding: 10px 20px;
|
| 40 |
+
border: none;
|
| 41 |
+
border-radius: 5px;
|
| 42 |
+
cursor: pointer;
|
| 43 |
+
}
|
| 44 |
+
.stButton>button:hover {
|
| 45 |
+
background-color: #45a049; /* Darker green on hover */
|
| 46 |
+
}
|
| 47 |
+
</style>
|
| 48 |
+
""",
|
| 49 |
+
unsafe_allow_html=True,
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
st.title("Enhanced Calculator")
|
| 53 |
|
| 54 |
+
# Use columns for layout
|
| 55 |
+
col1, col2, col3 = st.columns([1, 1, 1]) # Three equal-width columns
|
| 56 |
+
|
| 57 |
+
with col1:
|
| 58 |
+
st.subheader("Input Numbers")
|
| 59 |
+
num1 = st.number_input("First Number", value=0.0) # Default value
|
| 60 |
+
num2 = st.number_input("Second Number", value=0.0)
|
| 61 |
+
num3 = st.number_input("Third Number", value=0.0)
|
| 62 |
+
|
| 63 |
+
with col2:
|
| 64 |
+
st.subheader("Select Operation")
|
| 65 |
+
operation = st.selectbox(
|
| 66 |
+
"Operation",
|
| 67 |
+
["+", "-", "*", "/", "Power", "Square Root", "Cube Root", "Average"],
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
with col3:
|
| 71 |
+
st.subheader("Result")
|
| 72 |
+
if st.button("Calculate"):
|
| 73 |
+
try:
|
| 74 |
+
# ... (calculation logic remains the same as before)
|
| 75 |
+
# ...
|
| 76 |
+
st.write(f"Result: {result}")
|
| 77 |
+
except (ZeroDivisionError, ValueError) as e:
|
| 78 |
+
st.error(str(e))
|
| 79 |
+
except Exception as e:
|
| 80 |
+
st.error(f"An error occurred: {e}")
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
# Add some extra visual elements (optional)
|
| 84 |
+
st.markdown("---") # Separator line
|
| 85 |
+
st.write("Made with Streamlit") # Footer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|