Spaces:
Sleeping

Calculator / application.py
Mansoor12300's picture
Rename calculator.py to application.py
55981d5 verified
Raw History Blame Contribute Delete
1.71 kB
import streamlit as st
import math
from datetime import datetime
# Page config
st.set_page_config(
page_title="Advanced Calculator",
page_icon="๐Ÿงฎ",
layout="centered"
)
st.title("๐Ÿงฎ Advanced Calculator App")
st.caption("Built with Streamlit | Deployable on Hugging Face ๐Ÿš€")
# Initialize history
if "history" not in st.session_state:
st.session_state.history = []
# Allowed math functions
safe_dict = {
"sqrt": math.sqrt,
"log": math.log,
"sin": math.sin,
"cos": math.cos,
"tan": math.tan,
"pow": pow,
"pi": math.pi,
"e": math.e
}
st.markdown("### โœ๏ธ Enter Mathematical Expression")
expression = st.text_input(
"Example: 2 + 3 * sqrt(16) - sin(0)",
placeholder="Type your expression here..."
)
if st.button("๐Ÿ” Calculate"):
try:
result = eval(expression, {"__builtins__": None}, safe_dict)
time = datetime.now().strftime("%H:%M:%S")
st.success(f"โœ… Result: {result}")
st.session_state.history.append(
f"[{time}] {expression} = {result}"
)
except Exception as e:
st.error("โŒ Invalid expression. Please check your input.")
# Scientific shortcuts
st.markdown("### ๐Ÿงช Scientific Functions")
st.markdown("""
- `sqrt(x)` โ†’ Square Root
- `log(x)` โ†’ Natural Log
- `sin(x)` `cos(x)` `tan(x)`
- `pow(x, y)` โ†’ Power
- Constants: `pi`, `e`
""")
# History section
st.markdown("### ๐Ÿ“œ Calculation History")
if st.session_state.history:
for item in reversed(st.session_state.history):
st.write(item)
else:
st.info("No calculations yet.")
# Footer
st.markdown("---")
st.caption("๐Ÿ‘จโ€๐Ÿ’ป Created by Mansoor Ahmed | Streamlit + Hugging Face")