Spaces:
Sleeping
Sleeping
File size: 2,001 Bytes
54c7cfe | 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 | import streamlit as st
import math
# -----------------------------
# Page Configuration
# -----------------------------
st.set_page_config(
page_title="Advanced Calculator",
page_icon="🧮",
layout="centered"
)
st.title("🧮 Advanced Calculator")
st.write("Supports basic, power, modulus & scientific operations")
# -----------------------------
# Safe math functions
# -----------------------------
safe_dict = {
"sqrt": math.sqrt,
"log": math.log,
"log10": math.log10,
"sin": math.sin,
"cos": math.cos,
"tan": math.tan,
"pi": math.pi,
"e": math.e,
"pow": pow,
"abs": abs,
"round": round
}
# -----------------------------
# Session state for history
# -----------------------------
if "history" not in st.session_state:
st.session_state.history = []
# -----------------------------
# Calculator UI
# -----------------------------
expression = st.text_input(
"Enter mathematical expression:",
placeholder="Example: sqrt(25) + 2**3 or sin(pi/2)"
)
st.markdown("""
**Allowed operations:**
- `+ - * / % **`
- `sqrt(x), log(x), log10(x)`
- `sin(x), cos(x), tan(x)`
- `pi, e`
""")
# -----------------------------
# Calculate Button
# -----------------------------
if st.button("Calculate"):
try:
result = eval(expression, {"__builtins__": None}, safe_dict)
st.success(f"Result: {result}")
st.session_state.history.append(f"{expression} = {result}")
except ZeroDivisionError:
st.error("Error: Division by zero!")
except Exception:
st.error("Invalid expression!")
# -----------------------------
# History Section
# -----------------------------
if st.session_state.history:
st.subheader("📜 Calculation History")
for item in reversed(st.session_state.history):
st.write(item)
# -----------------------------
# Clear History
# -----------------------------
if st.button("Clear History"):
st.session_state.history = []
st.info("History cleared!")
|