AQTestSpace / app.py
aneesqumar's picture
Create app.py
54c7cfe verified
Raw
History Blame Contribute Delete
2 kB
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!")