hassan773 commited on
Commit
a31b831
·
verified ·
1 Parent(s): f2975f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -72
app.py CHANGED
@@ -1,82 +1,50 @@
1
  import streamlit as st
2
- import math
3
 
4
- # ---------------- Functions ---------------- #
5
- def add(a, b):
6
- return a + b
7
 
8
- def subtract(a, b):
9
- return a - b
10
 
11
- def multiply(a, b):
12
- return a * b
 
13
 
14
- def divide(a, b):
15
- if b == 0:
16
- return "❌ Division by zero"
17
- return a / b
18
-
19
- def power(a, b):
20
- return a ** b
21
-
22
- def modulus(a, b):
23
- return a % b
24
-
25
- # ---------------- UI ---------------- #
26
- st.set_page_config(page_title="Advanced Calculator", page_icon="🧮")
27
-
28
- st.title("🧮 Advanced Calculator App")
29
- st.write("Built with **Streamlit** | Ready for **Hugging Face Spaces** 🚀")
30
-
31
- # Operation selection
32
- operation = st.selectbox(
33
- "Select Operation",
34
- [
35
- "Addition (+)",
36
- "Subtraction (-)",
37
- "Multiplication (*)",
38
- "Division (/)",
39
- "Power (**)",
40
- "Modulus (%)",
41
- "Square Root (√)",
42
- "Logarithm (log)"
43
- ]
44
  )
45
 
46
- # Inputs
47
- num1 = st.number_input("Enter first number", value=0.0)
48
-
49
- num2 = None
50
- if operation not in ["Square Root (√)", "Logarithm (log)"]:
51
- num2 = st.number_input("Enter second number", value=0.0)
52
-
53
- # Calculate button
54
- if st.button("Calculate"):
55
- if operation == "Addition (+)":
56
- result = add(num1, num2)
57
- elif operation == "Subtraction (-)":
58
- result = subtract(num1, num2)
59
- elif operation == "Multiplication (*)":
60
- result = multiply(num1, num2)
61
- elif operation == "Division (/)":
62
- result = divide(num1, num2)
63
- elif operation == "Power (**)":
64
- result = power(num1, num2)
65
- elif operation == "Modulus (%)":
66
- result = modulus(num1, num2)
67
- elif operation == "Square Root (√)":
68
- if num1 < 0:
69
- result = "❌ Cannot take square root of negative number"
70
- else:
71
- result = math.sqrt(num1)
72
- elif operation == "Logarithm (log)":
73
- if num1 <= 0:
74
- result = "❌ Logarithm undefined for ≤ 0"
75
- else:
76
- result = math.log10(num1)
77
-
78
- st.success(f"Result: {result}")
79
 
80
  # Footer
81
  st.markdown("---")
82
- st.caption(" Made with Python & Streamlit")
 
1
  import streamlit as st
 
2
 
3
+ st.set_page_config(page_title="Button Calculator", page_icon="🧮")
 
 
4
 
5
+ st.title("🧮 Smart Calculator")
 
6
 
7
+ # Initialize session state
8
+ if "expression" not in st.session_state:
9
+ st.session_state.expression = ""
10
 
11
+ # Display
12
+ st.text_input(
13
+ "Display",
14
+ st.session_state.expression,
15
+ disabled=True,
16
+ label_visibility="collapsed"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  )
18
 
19
+ # Button handler
20
+ def press(key):
21
+ if key == "C":
22
+ st.session_state.expression = ""
23
+ elif key == "=":
24
+ try:
25
+ st.session_state.expression = str(
26
+ eval(st.session_state.expression)
27
+ )
28
+ except:
29
+ st.session_state.expression = "Error"
30
+ else:
31
+ st.session_state.expression += key
32
+
33
+ # Layout buttons
34
+ buttons = [
35
+ ["7", "8", "9", "/"],
36
+ ["4", "5", "6", "*"],
37
+ ["1", "2", "3", "-"],
38
+ ["0", ".", "=", "+"],
39
+ ["C"]
40
+ ]
41
+
42
+ for row in buttons:
43
+ cols = st.columns(len(row))
44
+ for i, btn in enumerate(row):
45
+ if cols[i].button(btn, use_container_width=True):
46
+ press(btn)
 
 
 
 
 
47
 
48
  # Footer
49
  st.markdown("---")
50
+ st.caption("📱 Smartphone-style calculator using Streamlit")