Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,15 +3,13 @@ import math
|
|
| 3 |
|
| 4 |
st.set_page_config(page_title="Calculator", layout="centered")
|
| 5 |
|
| 6 |
-
#
|
| 7 |
st.markdown("""
|
| 8 |
<style>
|
| 9 |
.calc {
|
| 10 |
max-width: 360px;
|
| 11 |
margin: auto;
|
| 12 |
}
|
| 13 |
-
|
| 14 |
-
/* Display */
|
| 15 |
.display {
|
| 16 |
background: #111;
|
| 17 |
color: white;
|
|
@@ -22,102 +20,65 @@ st.markdown("""
|
|
| 22 |
margin-bottom: 14px;
|
| 23 |
min-height: 60px;
|
| 24 |
}
|
| 25 |
-
|
| 26 |
-
/* ALL buttons */
|
| 27 |
-
.stButton > button {
|
| 28 |
height: 64px;
|
| 29 |
font-size: 22px;
|
| 30 |
border-radius: 16px;
|
| 31 |
-
border: none;
|
| 32 |
-
}
|
| 33 |
-
|
| 34 |
-
/* Operator buttons */
|
| 35 |
-
.stButton > button[data-op="op"] {
|
| 36 |
-
background-color: #ff9500;
|
| 37 |
-
color: white;
|
| 38 |
-
}
|
| 39 |
-
|
| 40 |
-
/* Equal button */
|
| 41 |
-
.stButton > button[data-op="eq"] {
|
| 42 |
-
background-color: #34c759;
|
| 43 |
-
color: white;
|
| 44 |
-
}
|
| 45 |
-
|
| 46 |
-
/* Clear button */
|
| 47 |
-
.stButton > button[data-op="clr"] {
|
| 48 |
-
background-color: #ff3b30;
|
| 49 |
-
color: white;
|
| 50 |
}
|
| 51 |
</style>
|
| 52 |
""", unsafe_allow_html=True)
|
| 53 |
|
| 54 |
-
#
|
| 55 |
if "expr" not in st.session_state:
|
| 56 |
st.session_state.expr = "0"
|
| 57 |
|
| 58 |
-
def press(
|
| 59 |
-
if
|
| 60 |
st.session_state.expr = "0"
|
| 61 |
-
|
| 62 |
-
elif val == "=":
|
| 63 |
try:
|
| 64 |
st.session_state.expr = str(eval(st.session_state.expr))
|
| 65 |
except:
|
| 66 |
st.session_state.expr = "Error"
|
| 67 |
-
|
| 68 |
-
elif val == "√":
|
| 69 |
try:
|
| 70 |
st.session_state.expr = str(math.sqrt(float(st.session_state.expr)))
|
| 71 |
except:
|
| 72 |
st.session_state.expr = "Error"
|
| 73 |
-
|
| 74 |
-
elif val == "x²":
|
| 75 |
try:
|
| 76 |
st.session_state.expr = str(float(st.session_state.expr) ** 2)
|
| 77 |
except:
|
| 78 |
st.session_state.expr = "Error"
|
| 79 |
-
|
| 80 |
-
elif val == "log":
|
| 81 |
try:
|
| 82 |
st.session_state.expr = str(math.log10(float(st.session_state.expr)))
|
| 83 |
except:
|
| 84 |
st.session_state.expr = "Error"
|
| 85 |
-
|
| 86 |
else:
|
| 87 |
if st.session_state.expr == "0":
|
| 88 |
-
st.session_state.expr =
|
| 89 |
else:
|
| 90 |
-
st.session_state.expr +=
|
| 91 |
|
| 92 |
st.rerun()
|
| 93 |
|
| 94 |
-
#
|
| 95 |
st.markdown('<div class="calc">', unsafe_allow_html=True)
|
| 96 |
st.markdown(f'<div class="display">{st.session_state.expr}</div>', unsafe_allow_html=True)
|
| 97 |
|
| 98 |
-
|
| 99 |
-
[
|
| 100 |
-
[
|
| 101 |
-
[
|
| 102 |
-
[
|
| 103 |
-
[
|
| 104 |
]
|
| 105 |
|
| 106 |
-
for row in
|
| 107 |
cols = st.columns(4)
|
| 108 |
-
for i,
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
st.markdown(
|
| 112 |
-
f"""
|
| 113 |
-
<script>
|
| 114 |
-
const btns = window.parent.document.querySelectorAll("button");
|
| 115 |
-
btns[btns.length-1].setAttribute("data-op", "{role}");
|
| 116 |
-
</script>
|
| 117 |
-
""",
|
| 118 |
-
unsafe_allow_html=True
|
| 119 |
-
)
|
| 120 |
-
if btn:
|
| 121 |
-
press(label)
|
| 122 |
|
| 123 |
st.markdown("</div>", unsafe_allow_html=True)
|
|
|
|
| 3 |
|
| 4 |
st.set_page_config(page_title="Calculator", layout="centered")
|
| 5 |
|
| 6 |
+
# ---------- Minimal SAFE CSS ---------- #
|
| 7 |
st.markdown("""
|
| 8 |
<style>
|
| 9 |
.calc {
|
| 10 |
max-width: 360px;
|
| 11 |
margin: auto;
|
| 12 |
}
|
|
|
|
|
|
|
| 13 |
.display {
|
| 14 |
background: #111;
|
| 15 |
color: white;
|
|
|
|
| 20 |
margin-bottom: 14px;
|
| 21 |
min-height: 60px;
|
| 22 |
}
|
| 23 |
+
.stButton button {
|
|
|
|
|
|
|
| 24 |
height: 64px;
|
| 25 |
font-size: 22px;
|
| 26 |
border-radius: 16px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
}
|
| 28 |
</style>
|
| 29 |
""", unsafe_allow_html=True)
|
| 30 |
|
| 31 |
+
# ---------- State ---------- #
|
| 32 |
if "expr" not in st.session_state:
|
| 33 |
st.session_state.expr = "0"
|
| 34 |
|
| 35 |
+
def press(v):
|
| 36 |
+
if v == "C":
|
| 37 |
st.session_state.expr = "0"
|
| 38 |
+
elif v == "=":
|
|
|
|
| 39 |
try:
|
| 40 |
st.session_state.expr = str(eval(st.session_state.expr))
|
| 41 |
except:
|
| 42 |
st.session_state.expr = "Error"
|
| 43 |
+
elif v == "√":
|
|
|
|
| 44 |
try:
|
| 45 |
st.session_state.expr = str(math.sqrt(float(st.session_state.expr)))
|
| 46 |
except:
|
| 47 |
st.session_state.expr = "Error"
|
| 48 |
+
elif v == "x²":
|
|
|
|
| 49 |
try:
|
| 50 |
st.session_state.expr = str(float(st.session_state.expr) ** 2)
|
| 51 |
except:
|
| 52 |
st.session_state.expr = "Error"
|
| 53 |
+
elif v == "log":
|
|
|
|
| 54 |
try:
|
| 55 |
st.session_state.expr = str(math.log10(float(st.session_state.expr)))
|
| 56 |
except:
|
| 57 |
st.session_state.expr = "Error"
|
|
|
|
| 58 |
else:
|
| 59 |
if st.session_state.expr == "0":
|
| 60 |
+
st.session_state.expr = v
|
| 61 |
else:
|
| 62 |
+
st.session_state.expr += v
|
| 63 |
|
| 64 |
st.rerun()
|
| 65 |
|
| 66 |
+
# ---------- UI ---------- #
|
| 67 |
st.markdown('<div class="calc">', unsafe_allow_html=True)
|
| 68 |
st.markdown(f'<div class="display">{st.session_state.expr}</div>', unsafe_allow_html=True)
|
| 69 |
|
| 70 |
+
keys = [
|
| 71 |
+
["C", "√", "x²", "/"],
|
| 72 |
+
["7", "8", "9", "*"],
|
| 73 |
+
["4", "5", "6", "-"],
|
| 74 |
+
["1", "2", "3", "+"],
|
| 75 |
+
["0", ".", "log", "="]
|
| 76 |
]
|
| 77 |
|
| 78 |
+
for row in keys:
|
| 79 |
cols = st.columns(4)
|
| 80 |
+
for i, k in enumerate(row):
|
| 81 |
+
if cols[i].button(k, use_container_width=True):
|
| 82 |
+
press(k)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
st.markdown("</div>", unsafe_allow_html=True)
|