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"