Habib U Rehman commited on
Commit
9668d97
·
verified ·
1 Parent(s): 1ce565d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -68
app.py CHANGED
@@ -4,76 +4,87 @@ import math
4
  st.set_page_config(page_title="Advanced Calculator", page_icon="🧮")
5
 
6
  st.title("🧮 Advanced Calculator")
7
- st.write("Built with Streamlit for Hugging Face Spaces 🚀")
8
-
9
- # Initialize history
10
- if "history" not in st.session_state:
11
- st.session_state.history = []
12
-
13
- operation = st.selectbox(
14
- "Choose an operation",
15
- [
16
- "Addition",
17
- "Subtraction",
18
- "Multiplication",
19
- "Division",
20
- "Power",
21
- "Modulus",
22
- "Square Root",
23
- "Sin",
24
- "Cos",
25
- "Tan",
26
- ],
27
  )
28
 
29
- def calculate(op, a=None, b=None):
30
- if op == "Addition":
31
- return a + b
32
- elif op == "Subtraction":
33
- return a - b
34
- elif op == "Multiplication":
35
- return a * b
36
- elif op == "Division":
37
- if b == 0:
38
- return "❌ Division by zero"
39
- return a / b
40
- elif op == "Power":
41
- return a ** b
42
- elif op == "Modulus":
43
- return a % b
44
- elif op == "Square Root":
45
- if a < 0:
46
- return " Negative number"
47
- return math.sqrt(a)
48
- elif op == "Sin":
49
- return math.sin(math.radians(a))
50
- elif op == "Cos":
51
- return math.cos(math.radians(a))
52
- elif op == "Tan":
53
- return math.tan(math.radians(a))
54
-
55
- # Input fields
56
- if operation in ["Square Root", "Sin", "Cos", "Tan"]:
57
- num1 = st.number_input("Enter number / angle (degrees)", value=0.0)
58
- if st.button("Calculate"):
59
- result = calculate(operation, num1)
60
- st.success(f"Result: {result}")
61
- st.session_state.history.append(f"{operation}({num1}) = {result}")
62
- else:
63
- num1 = st.number_input("Enter first number", value=0.0)
64
- num2 = st.number_input("Enter second number", value=0.0)
65
- if st.button("Calculate"):
66
- result = calculate(operation, num1, num2)
67
- st.success(f"Result: {result}")
68
- st.session_state.history.append(
69
- f"{operation}({num1}, {num2}) = {result}"
70
- )
71
 
72
  st.markdown("---")
73
- st.subheader("📜 Calculation History")
74
 
75
- if st.session_state.history:
76
- for item in reversed(st.session_state.history):
77
- st.write(item)
78
- else:
79
- st.write("No calculations yet.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  st.set_page_config(page_title="Advanced Calculator", page_icon="🧮")
5
 
6
  st.title("🧮 Advanced Calculator")
7
+ st.caption("Streamlit App for Hugging Face Spaces")
8
+
9
+ # Initialize expression
10
+ if "expression" not in st.session_state:
11
+ st.session_state.expression = ""
12
+
13
+ # Display
14
+ st.text_input(
15
+ "Calculator",
16
+ st.session_state.expression,
17
+ disabled=True,
 
 
 
 
 
 
 
 
 
18
  )
19
 
20
+ # Button handler
21
+ def press(key):
22
+ st.session_state.expression += str(key)
23
+
24
+ def clear():
25
+ st.session_state.expression = ""
26
+
27
+ def calculate():
28
+ try:
29
+ result = eval(st.session_state.expression)
30
+ st.session_state.expression = str(result)
31
+ except:
32
+ st.session_state.expression = "Error"
33
+
34
+ # Layout
35
+ buttons = [
36
+ ["7", "8", "9", "/"],
37
+ ["4", "5", "6", "*"],
38
+ ["1", "2", "3", "-"],
39
+ ["0", ".", "+", "="],
40
+ ]
41
+
42
+ for row in buttons:
43
+ cols = st.columns(4)
44
+ for i, btn in enumerate(row):
45
+ if cols[i].button(btn):
46
+ if btn == "=":
47
+ calculate()
48
+ else:
49
+ press(btn)
50
+
51
+ # Extra controls
52
+ col1, col2 = st.columns(2)
53
+ if col1.button("C"):
54
+ clear()
55
+
56
+ if col2.button(""):
57
+ try:
58
+ value = float(st.session_state.expression)
59
+ st.session_state.expression = str(math.sqrt(value))
60
+ except:
61
+ st.session_state.expression = "Error"
62
 
63
  st.markdown("---")
 
64
 
65
+ # Scientific buttons
66
+ st.subheader("Scientific Functions")
67
+
68
+ sci_cols = st.columns(3)
69
+
70
+ if sci_cols[0].button("sin"):
71
+ try:
72
+ value = float(st.session_state.expression)
73
+ st.session_state.expression = str(math.sin(math.radians(value)))
74
+ except:
75
+ st.session_state.expression = "Error"
76
+
77
+ if sci_cols[1].button("cos"):
78
+ try:
79
+ value = float(st.session_state.expression)
80
+ st.session_state.expression = str(math.cos(math.radians(value)))
81
+ except:
82
+ st.session_state.expression = "Error"
83
+
84
+ if sci_cols[2].button("tan"):
85
+ try:
86
+ value = float(st.session_state.expression)
87
+ st.session_state.expression = str(math.tan(math.radians(value)))
88
+ except:
89
+ st.session_state.expression = "Error"
90
+