Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,129 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import math
|
| 3 |
-
import pandas as pd
|
| 4 |
-
|
| 5 |
-
st.set_page_config(page_title="Ultimate Calculator", page_icon="🧮", layout="centered")
|
| 6 |
-
|
| 7 |
-
st.title("🧮 Ultimate Scientific Calculator")
|
| 8 |
-
|
| 9 |
-
# ---------------- SESSION STATE ----------------
|
| 10 |
-
if "expression" not in st.session_state:
|
| 11 |
-
st.session_state.expression = ""
|
| 12 |
-
|
| 13 |
-
if "history" not in st.session_state:
|
| 14 |
-
st.session_state.history = []
|
| 15 |
-
|
| 16 |
-
# ---------------- SAFE EVAL ----------------
|
| 17 |
-
def safe_eval(expr):
|
| 18 |
-
allowed = {
|
| 19 |
-
"sqrt": math.sqrt,
|
| 20 |
-
"sin": math.sin,
|
| 21 |
-
"cos": math.cos,
|
| 22 |
-
"tan": math.tan,
|
| 23 |
-
"log": math.log10,
|
| 24 |
-
"pi": math.pi,
|
| 25 |
-
"e": math.e
|
| 26 |
-
}
|
| 27 |
-
try:
|
| 28 |
-
return eval(expr, {"__builtins__": None}, allowed)
|
| 29 |
-
except:
|
| 30 |
-
return "Error"
|
| 31 |
-
|
| 32 |
-
# ---------------- DISPLAY ----------------
|
| 33 |
-
st.markdown(f"### {st.session_state.expression or 0}")
|
| 34 |
-
|
| 35 |
-
if st.session_state.expression:
|
| 36 |
-
st.write("🔎 Live Result:", safe_eval(st.session_state.expression))
|
| 37 |
-
|
| 38 |
-
# ---------------- BUTTON FUNCTIONS ----------------
|
| 39 |
-
def add(val):
|
| 40 |
-
st.session_state.expression += str(val)
|
| 41 |
-
|
| 42 |
-
def clear():
|
| 43 |
-
st.session_state.expression = ""
|
| 44 |
-
|
| 45 |
-
def delete():
|
| 46 |
-
st.session_state.expression = st.session_state.expression[:-1]
|
| 47 |
-
|
| 48 |
-
def calculate():
|
| 49 |
-
result = safe_eval(st.session_state.expression)
|
| 50 |
-
st.session_state.history.append(f"{st.session_state.expression} = {result}")
|
| 51 |
-
st.session_state.expression = str(result)
|
| 52 |
-
|
| 53 |
-
# ---------------- BUTTON GRID ----------------
|
| 54 |
-
buttons = [
|
| 55 |
-
["7","8","9","/"],
|
| 56 |
-
["4","5","6","*"],
|
| 57 |
-
["1","2","3","-"],
|
| 58 |
-
["0",".","(",")"],
|
| 59 |
-
]
|
| 60 |
-
|
| 61 |
-
for row in buttons:
|
| 62 |
-
cols = st.columns(4)
|
| 63 |
-
for i, btn in enumerate(row):
|
| 64 |
-
cols[i].button(btn, on_click=add, args=(btn,))
|
| 65 |
-
|
| 66 |
-
cols = st.columns(4)
|
| 67 |
-
cols[0].button("+", on_click=add, args=("+",))
|
| 68 |
-
cols[1].button("sqrt(", on_click=add, args=("sqrt(",))
|
| 69 |
-
cols[2].button("sin(", on_click=add, args=("sin(",))
|
| 70 |
-
cols[3].button("log(", on_click=add, args=("log(",))
|
| 71 |
-
|
| 72 |
-
cols = st.columns(3)
|
| 73 |
-
cols[0].button("C", on_click=clear)
|
| 74 |
-
cols[1].button("DEL", on_click=delete)
|
| 75 |
-
cols[2].button("=", on_click=calculate)
|
| 76 |
-
|
| 77 |
-
st.markdown("---")
|
| 78 |
-
|
| 79 |
-
# ---------------- FUNCTION PLOTTER ----------------
|
| 80 |
-
st.subheader("📊 Function Plotter")
|
| 81 |
-
|
| 82 |
-
plot_expr = st.text_input("Enter function in x (example: sin(x), x**2)")
|
| 83 |
-
|
| 84 |
-
if st.button("Plot Graph"):
|
| 85 |
-
try:
|
| 86 |
-
x_vals = [i/10 for i in range(-100, 100)]
|
| 87 |
-
y_vals = []
|
| 88 |
-
|
| 89 |
-
for x in x_vals:
|
| 90 |
-
y = eval(
|
| 91 |
-
plot_expr,
|
| 92 |
-
{"__builtins__": None},
|
| 93 |
-
{
|
| 94 |
-
"x": x,
|
| 95 |
-
"sin": math.sin,
|
| 96 |
-
"cos": math.cos,
|
| 97 |
-
"tan": math.tan,
|
| 98 |
-
"sqrt": math.sqrt,
|
| 99 |
-
"log": math.log10,
|
| 100 |
-
"pi": math.pi,
|
| 101 |
-
"e": math.e
|
| 102 |
-
}
|
| 103 |
-
)
|
| 104 |
-
y_vals.append(y)
|
| 105 |
-
|
| 106 |
-
df = pd.DataFrame({"x": x_vals, "y": y_vals})
|
| 107 |
-
st.line_chart(df.set_index("x"))
|
| 108 |
-
|
| 109 |
-
except:
|
| 110 |
-
st.error("Invalid Function")
|
| 111 |
-
|
| 112 |
-
st.markdown("---")
|
| 113 |
-
|
| 114 |
-
# ---------------- HISTORY ----------------
|
| 115 |
-
st.subheader("📜 History")
|
| 116 |
-
|
| 117 |
-
if st.session_state.history:
|
| 118 |
-
for item in reversed(st.session_state.history):
|
| 119 |
-
st.write(item)
|
| 120 |
-
|
| 121 |
-
df = pd.DataFrame(st.session_state.history, columns=["Calculations"])
|
| 122 |
-
st.download_button(
|
| 123 |
-
"📥 Download History",
|
| 124 |
-
df.to_csv(index=False),
|
| 125 |
-
file_name="history.csv",
|
| 126 |
-
mime="text/csv"
|
| 127 |
-
)
|
| 128 |
-
else:
|
| 129 |
-
st.info("No calculations yet.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|