Spaces:
Paused
Paused
File size: 2,105 Bytes
1ce565d 9668d97 1ce565d 9668d97 1ce565d 9668d97 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | import streamlit as st
import math
st.set_page_config(page_title="Advanced Calculator", page_icon="🧮")
st.title("🧮 Advanced Calculator")
st.caption("Streamlit App for Hugging Face Spaces")
# Initialize expression
if "expression" not in st.session_state:
st.session_state.expression = ""
# Display
st.text_input(
"Calculator",
st.session_state.expression,
disabled=True,
)
# Button handler
def press(key):
st.session_state.expression += str(key)
def clear():
st.session_state.expression = ""
def calculate():
try:
result = eval(st.session_state.expression)
st.session_state.expression = str(result)
except:
st.session_state.expression = "Error"
# Layout
buttons = [
["7", "8", "9", "/"],
["4", "5", "6", "*"],
["1", "2", "3", "-"],
["0", ".", "+", "="],
]
for row in buttons:
cols = st.columns(4)
for i, btn in enumerate(row):
if cols[i].button(btn):
if btn == "=":
calculate()
else:
press(btn)
# Extra controls
col1, col2 = st.columns(2)
if col1.button("C"):
clear()
if col2.button("√"):
try:
value = float(st.session_state.expression)
st.session_state.expression = str(math.sqrt(value))
except:
st.session_state.expression = "Error"
st.markdown("---")
# Scientific buttons
st.subheader("Scientific Functions")
sci_cols = st.columns(3)
if sci_cols[0].button("sin"):
try:
value = float(st.session_state.expression)
st.session_state.expression = str(math.sin(math.radians(value)))
except:
st.session_state.expression = "Error"
if sci_cols[1].button("cos"):
try:
value = float(st.session_state.expression)
st.session_state.expression = str(math.cos(math.radians(value)))
except:
st.session_state.expression = "Error"
if sci_cols[2].button("tan"):
try:
value = float(st.session_state.expression)
st.session_state.expression = str(math.tan(math.radians(value)))
except:
st.session_state.expression = "Error"
|