Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,82 +1,50 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import math
|
| 3 |
|
| 4 |
-
|
| 5 |
-
def add(a, b):
|
| 6 |
-
return a + b
|
| 7 |
|
| 8 |
-
|
| 9 |
-
return a - b
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 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 |
-
#
|
| 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 |
-
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("
|
|
|
|
| 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")
|