Spaces:
Sleeping
Sleeping
import streamlit as st | |
def add(a, b): | |
if not isinstance(a, (int, float)) or not isinstance(b, (int, float)): | |
return "Invalid input: Please enter only numbers." | |
return a + b | |
def subtract(a, b): | |
if not isinstance(a, (int, float)) or not isinstance(b, (int, float)): | |
return "Invalid input: Please enter only numbers." | |
return a - b | |
def multiply(a, b): | |
if not isinstance(a, (int, float)) or not isinstance(b, (int, float)): | |
return "Invalid input: Please enter only numbers." | |
return a * b | |
def divide(a, b): | |
if not isinstance(a, (int, float)) or not isinstance(b, (int, float)): | |
return "Invalid input: Please enter only numbers." | |
if b == 0: | |
return "Error: Division by zero" | |
return a / b | |
def exponentiation(a, b): | |
if not isinstance(a, (int, float)) or not isinstance(b, (int, float)): | |
return "Invalid input: Please enter only numbers." | |
return a ** b | |
def square_root(a): | |
if not isinstance(a, (int, float)): | |
return "Invalid input: Please enter only numbers." | |
if a < 0: | |
return "Error: Square root of negative number not possible." | |
return a ** 0.5 | |
st.title("Advanced Functional Calculator") | |
# Clear layout using columns | |
col1, col2 = st.columns(2) | |
# Input fields | |
first_number = col1.number_input("Enter first number", key="first_number") | |
second_number = col2.number_input("Enter second number (optional)", key="second_number") | |
# Buttons with descriptions | |
add_button = st.button("Add", key="add_button") | |
subtract_button = st.button("Subtract", key="subtract_button") | |
multiply_button = st.button("Multiply", key="multiply_button") | |
divide_button = st.button("Divide", key="divide_button") | |
exp_button = st.button("Exponentiation", key="exp_button") | |
sqrt_button = st.button("Square Root", key="sqrt_button") | |
# Robust input validation and calculation based on buttons | |
if add_button: | |
result = add(first_number, second_number) | |
if isinstance(result, str): # Display error message if validation failed | |
st.error(result) | |
else: | |
st.write("Result:", result) | |
elif subtract_button: | |
result = subtract(first_number, second_number) | |
if isinstance(result, str): | |
st.error(result) | |
else: | |
st.write("Result:", result) | |
elif multiply_button: | |
result = multiply(first_number, second_number) | |
if isinstance(result, str): | |
st.error(result) | |
else: | |
st.write("Result:", result) | |
elif divide_button: | |
result = divide(first_number, second_number) | |
if isinstance(result, str): | |
st.error(result) | |
else: | |
st.write("Result:", result) | |
elif exp_button: | |
result = exponentiation(first_number, second_number) | |
if isinstance(result, str): | |
st.error(result) | |
else: | |
st.write("Result:", result) | |
elif sqrt_button: | |
result = square_root(first_number) | |
if isinstance(result, str): | |
st.error(result) | |
else: | |
st.write("Result:", result) | |
# History using SessionState | |
session_state = st.session_state | |
if "calc_history" not in session_state: | |
session_state.calc_history = [] | |
# Update history on each calculation | |
if result and not isinstance(result, str): | |
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}") | |
# Display history | |
if session_state.calc_history: | |
st.subheader("Calculation History:") | |
for entry in session_state.calc_history: | |
st.write(entry) | |
# Customization options | |
st.subheader("Customization:") | |
font_size = st.number_input("Font size", min_value=10, max_value=20, value=16, key="font_size") | |
st.write("Current font size:", font_size) | |
primary_color = st.color_picker("Primary color", key="primary_color") | |
st.write("Current primary color:", primary_color) | |
# Update layout based on customization | |
st.markdown(f"<style>.stApp {{ font-size: {font_size}px; color: {primary_color}; }}</style>", unsafe_allow_html=True) | |