Spaces:
Paused
Paused
Habib U Rehman commited on
Update app.py
Browse files
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.
|
| 8 |
-
|
| 9 |
-
# Initialize
|
| 10 |
-
if "
|
| 11 |
-
st.session_state.
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
"Multiplication",
|
| 19 |
-
"Division",
|
| 20 |
-
"Power",
|
| 21 |
-
"Modulus",
|
| 22 |
-
"Square Root",
|
| 23 |
-
"Sin",
|
| 24 |
-
"Cos",
|
| 25 |
-
"Tan",
|
| 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 |
-
st.
|
| 68 |
-
st.session_state.
|
| 69 |
-
|
| 70 |
-
|
| 71 |
|
| 72 |
st.markdown("---")
|
| 73 |
-
st.subheader("📜 Calculation History")
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|