Spaces:
Sleeping
Sleeping
Download application.py from Mansoor12300/Calculator: direct link, hf CLI and curl.
- Browser
- Download file 1.71 kB
-
https://huggingface.co/spaces/Mansoor12300/Calculator/resolve/main/application.py
- Command line
-
hf download hf://spaces/Mansoor12300/Calculator/application.py
-
curl -L -o application.py https://huggingface.co/spaces/Mansoor12300/Calculator/resolve/main/application.py
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") | |