muhammadrazapathan commited on
Commit
d7ed6ed
·
verified ·
1 Parent(s): 3e457f2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +140 -0
app.py ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import math
3
+ import pandas as pd
4
+
5
+ st.set_page_config(page_title="Mobile Calculator", page_icon="🧮", layout="centered")
6
+
7
+ # ---------------- SESSION STATE ----------------
8
+ if "expression" not in st.session_state:
9
+ st.session_state.expression = ""
10
+
11
+ if "history" not in st.session_state:
12
+ st.session_state.history = []
13
+
14
+ # ---------------- SAFE EVAL ----------------
15
+ def safe_eval(expr):
16
+ allowed = {
17
+ "sqrt": math.sqrt,
18
+ "sin": math.sin,
19
+ "cos": math.cos,
20
+ "tan": math.tan,
21
+ "log": math.log10,
22
+ "pi": math.pi,
23
+ "e": math.e
24
+ }
25
+ try:
26
+ return eval(expr, {"__builtins__": None}, allowed)
27
+ except:
28
+ return "Error"
29
+
30
+ # ---------------- STYLE ----------------
31
+ st.markdown("""
32
+ <style>
33
+ body {background-color:#0f172a;}
34
+ .display {
35
+ background:#1e293b;
36
+ color:white;
37
+ padding:20px;
38
+ border-radius:20px;
39
+ font-size:32px;
40
+ text-align:right;
41
+ margin-bottom:15px;
42
+ }
43
+ .stButton>button {
44
+ height:65px;
45
+ border-radius:20px;
46
+ font-size:22px;
47
+ font-weight:bold;
48
+ }
49
+ </style>
50
+ """, unsafe_allow_html=True)
51
+
52
+ st.title("🧮 Advanced Mobile Calculator")
53
+
54
+ # ---------------- DISPLAY ----------------
55
+ st.markdown(f"<div class='display'>{st.session_state.expression or '0'}</div>", unsafe_allow_html=True)
56
+
57
+ if st.session_state.expression:
58
+ st.write("Result:", safe_eval(st.session_state.expression))
59
+
60
+ # ---------------- BUTTON FUNCTIONS ----------------
61
+ def add(val):
62
+ st.session_state.expression += str(val)
63
+
64
+ def clear():
65
+ st.session_state.expression = ""
66
+
67
+ def delete():
68
+ st.session_state.expression = st.session_state.expression[:-1]
69
+
70
+ def calculate():
71
+ result = safe_eval(st.session_state.expression)
72
+ st.session_state.history.append(f"{st.session_state.expression} = {result}")
73
+ st.session_state.expression = str(result)
74
+
75
+ # ---------------- FIXED MOBILE GRID ----------------
76
+ layout = [
77
+ ["C", "DEL", "(", ")"],
78
+ ["7", "8", "9", "/"],
79
+ ["4", "5", "6", "*"],
80
+ ["1", "2", "3", "-"],
81
+ ["0", ".", "+", "="],
82
+ ]
83
+
84
+ for r, row in enumerate(layout):
85
+ cols = st.columns(4)
86
+ for c, button in enumerate(row):
87
+
88
+ key_name = f"btn_{r}_{c}"
89
+
90
+ if button == "C":
91
+ cols[c].button(button, key=key_name, on_click=clear)
92
+
93
+ elif button == "DEL":
94
+ cols[c].button(button, key=key_name, on_click=delete)
95
+
96
+ elif button == "=":
97
+ cols[c].button(button, key=key_name, on_click=calculate)
98
+
99
+ else:
100
+ cols[c].button(
101
+ button,
102
+ key=key_name,
103
+ on_click=add,
104
+ args=(button,)
105
+ )
106
+
107
+ st.markdown("---")
108
+
109
+ # ---------------- SCIENTIFIC BUTTONS ----------------
110
+ st.subheader("Scientific")
111
+
112
+ sci_buttons = ["sqrt(", "sin(", "cos(", "tan(", "log(", "pi", "e"]
113
+
114
+ cols = st.columns(len(sci_buttons))
115
+
116
+ for i, btn in enumerate(sci_buttons):
117
+ cols[i].button(
118
+ btn,
119
+ key=f"sci_{i}",
120
+ on_click=add,
121
+ args=(btn,)
122
+ )
123
+
124
+ st.markdown("---")
125
+
126
+ # ---------------- HISTORY ----------------
127
+ st.subheader("History")
128
+
129
+ if st.session_state.history:
130
+ for item in reversed(st.session_state.history):
131
+ st.write(item)
132
+
133
+ df = pd.DataFrame(st.session_state.history, columns=["Calculations"])
134
+ st.download_button(
135
+ "Download History",
136
+ df.to_csv(index=False),
137
+ file_name="history.csv"
138
+ )
139
+ else:
140
+ st.info("No calculations yet.")