Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
def add(a, b):
|
| 4 |
+
if not isinstance(a, (int, float)) or not isinstance(b, (int, float)):
|
| 5 |
+
return "Invalid input: Please enter only numbers."
|
| 6 |
+
return a + b
|
| 7 |
+
|
| 8 |
+
def subtract(a, b):
|
| 9 |
+
if not isinstance(a, (int, float)) or not isinstance(b, (int, float)):
|
| 10 |
+
return "Invalid input: Please enter only numbers."
|
| 11 |
+
return a - b
|
| 12 |
+
|
| 13 |
+
def multiply(a, b):
|
| 14 |
+
if not isinstance(a, (int, float)) or not isinstance(b, (int, float)):
|
| 15 |
+
return "Invalid input: Please enter only numbers."
|
| 16 |
+
return a * b
|
| 17 |
+
|
| 18 |
+
def divide(a, b):
|
| 19 |
+
if not isinstance(a, (int, float)) or not isinstance(b, (int, float)):
|
| 20 |
+
return "Invalid input: Please enter only numbers."
|
| 21 |
+
if b == 0:
|
| 22 |
+
return "Error: Division by zero"
|
| 23 |
+
return a / b
|
| 24 |
+
|
| 25 |
+
def exponentiation(a, b):
|
| 26 |
+
if not isinstance(a, (int, float)) or not isinstance(b, (int, float)):
|
| 27 |
+
return "Invalid input: Please enter only numbers."
|
| 28 |
+
return a ** b
|
| 29 |
+
|
| 30 |
+
def square_root(a):
|
| 31 |
+
if not isinstance(a, (int, float)):
|
| 32 |
+
return "Invalid input: Please enter only numbers."
|
| 33 |
+
if a < 0:
|
| 34 |
+
return "Error: Square root of negative number not possible."
|
| 35 |
+
return a ** 0.5
|
| 36 |
+
|
| 37 |
+
st.title("Advanced Functional Calculator")
|
| 38 |
+
|
| 39 |
+
# Clear layout using columns
|
| 40 |
+
col1, col2 = st.columns(2)
|
| 41 |
+
|
| 42 |
+
# Input fields
|
| 43 |
+
first_number = col1.number_input("Enter first number", key="first_number")
|
| 44 |
+
second_number = col2.number_input("Enter second number (optional)", key="second_number")
|
| 45 |
+
|
| 46 |
+
# Buttons with descriptions
|
| 47 |
+
add_button = st.button("Add", key="add_button")
|
| 48 |
+
subtract_button = st.button("Subtract", key="subtract_button")
|
| 49 |
+
multiply_button = st.button("Multiply", key="multiply_button")
|
| 50 |
+
divide_button = st.button("Divide", key="divide_button")
|
| 51 |
+
exp_button = st.button("Exponentiation", key="exp_button")
|
| 52 |
+
sqrt_button = st.button("Square Root", key="sqrt_button")
|
| 53 |
+
|
| 54 |
+
# Robust input validation and calculation based on buttons
|
| 55 |
+
if add_button:
|
| 56 |
+
result = add(first_number, second_number)
|
| 57 |
+
if isinstance(result, str): # Display error message if validation failed
|
| 58 |
+
st.error(result)
|
| 59 |
+
else:
|
| 60 |
+
st.write("Result:", result)
|
| 61 |
+
elif subtract_button:
|
| 62 |
+
result = subtract(first_number, second_number)
|
| 63 |
+
if isinstance(result, str):
|
| 64 |
+
st.error(result)
|
| 65 |
+
else:
|
| 66 |
+
st.write("Result:", result)
|
| 67 |
+
elif multiply_button:
|
| 68 |
+
result = multiply(first_number, second_number)
|
| 69 |
+
if isinstance(result, str):
|
| 70 |
+
st.error(result)
|
| 71 |
+
else:
|
| 72 |
+
st.write("Result:", result)
|
| 73 |
+
elif divide_button:
|
| 74 |
+
result = divide(first_number, second_number)
|
| 75 |
+
if isinstance(result, str):
|
| 76 |
+
st.error(result)
|
| 77 |
+
else:
|
| 78 |
+
st.write("Result:", result)
|
| 79 |
+
elif exp_button:
|
| 80 |
+
result = exponentiation(first_number, second_number)
|
| 81 |
+
if isinstance(result, str):
|
| 82 |
+
st.error(result)
|
| 83 |
+
else:
|
| 84 |
+
st.write("Result:", result)
|
| 85 |
+
elif sqrt_button:
|
| 86 |
+
result = square_root(first_number)
|
| 87 |
+
if isinstance(result, str):
|
| 88 |
+
st.error(result)
|
| 89 |
+
else:
|
| 90 |
+
st.write("Result:", result)
|
| 91 |
+
|
| 92 |
+
# History using SessionState
|
| 93 |
+
session_state = st.session_state
|
| 94 |
+
if "calc_history" not in session_state:
|
| 95 |
+
session_state.calc_history = []
|
| 96 |
+
|
| 97 |
+
# Update history on each calculation
|
| 98 |
+
if result and not isinstance(result, str):
|
| 99 |
+
session_state.calc_history.append(f"{first_number} {add_button.text if add_button else '', subtract_button.text if subtract_button else '', multiply_button.text if multiply_button else '', divide_button.text if divide_button else '', exp_button.text if exp_button else '', sqrt_button.text if sqrt_button else ''} {second_number} = {result}")
|
| 100 |
+
|
| 101 |
+
# Display history
|
| 102 |
+
if session_state.calc_history:
|
| 103 |
+
st.subheader("Calculation History:")
|
| 104 |
+
for entry in session_state.calc_history:
|
| 105 |
+
st.write(entry)
|
| 106 |
+
|
| 107 |
+
# Customization options
|
| 108 |
+
st.subheader("Customization:")
|
| 109 |
+
font_size = st.number_input("Font size", min_value=10, max_value=20, value=16, key="font_size")
|
| 110 |
+
st.write("Current font size:", font_size)
|
| 111 |
+
primary_color = st.color_picker("Primary color", key="primary_color")
|
| 112 |
+
st.write("Current primary color:", primary_color)
|
| 113 |
+
|
| 114 |
+
# Update layout based on customization
|
| 115 |
+
st.markdown(f"<style>.stApp {{ font-size: {font_size}px; color: {primary_color}; }}</style>", unsafe_allow_html=True)
|
| 116 |
+
|