File size: 4,151 Bytes
fda41b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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)