muhammadrazapathan commited on
Commit
a8efbf3
·
verified ·
1 Parent(s): 3509b02

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +159 -0
app.py ADDED
@@ -0,0 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 ----------------
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:10px;
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
+ # ---------------- MOBILE GRID ----------------
76
+
77
+ layout = [
78
+ ["C", "DEL", "(", ")"],
79
+ ["7", "8", "9", "/"],
80
+ ["4", "5", "6", "*"],
81
+ ["1", "2", "3", "-"],
82
+ ["0", ".", "+", "="],
83
+ ]
84
+
85
+ for row in layout:
86
+ cols = st.columns(4)
87
+ for i, button in enumerate(row):
88
+ if button == "C":
89
+ cols[i].button(button, on_click=clear)
90
+ elif button == "DEL":
91
+ cols[i].button(button, on_click=delete)
92
+ elif button == "=":
93
+ cols[i].button(button, on_click=calculate)
94
+ else:
95
+ cols[i].button(button, on_click=add, args=(button,))
96
+
97
+ st.markdown("---")
98
+
99
+ # ---------------- SCIENTIFIC SECTION ----------------
100
+ st.subheader("Scientific Functions")
101
+
102
+ sci_cols = st.columns(4)
103
+ sci_cols[0].button("sqrt(", on_click=add, args=("sqrt(",))
104
+ sci_cols[1].button("sin(", on_click=add, args=("sin(",))
105
+ sci_cols[2].button("cos(", on_click=add, args=("cos(",))
106
+ sci_cols[3].button("log(", on_click=add, args=("log(",))
107
+
108
+ st.markdown("---")
109
+
110
+ # ---------------- GRAPH PLOT ----------------
111
+ st.subheader("📊 Plot Function")
112
+
113
+ plot_expr = st.text_input("Enter function in x (example: sin(x), x**2)")
114
+
115
+ if st.button("Plot"):
116
+ try:
117
+ x_vals = [i/10 for i in range(-100, 100)]
118
+ y_vals = []
119
+
120
+ for x in x_vals:
121
+ y = eval(
122
+ plot_expr,
123
+ {"__builtins__": None},
124
+ {
125
+ "x": x,
126
+ "sin": math.sin,
127
+ "cos": math.cos,
128
+ "tan": math.tan,
129
+ "sqrt": math.sqrt,
130
+ "log": math.log10,
131
+ "pi": math.pi,
132
+ "e": math.e
133
+ }
134
+ )
135
+ y_vals.append(y)
136
+
137
+ df = pd.DataFrame({"x": x_vals, "y": y_vals})
138
+ st.line_chart(df.set_index("x"))
139
+
140
+ except:
141
+ st.error("Invalid Function")
142
+
143
+ st.markdown("---")
144
+
145
+ # ---------------- HISTORY ----------------
146
+ st.subheader("📜 History")
147
+
148
+ if st.session_state.history:
149
+ for item in reversed(st.session_state.history):
150
+ st.write(item)
151
+
152
+ df = pd.DataFrame(st.session_state.history, columns=["Calculations"])
153
+ st.download_button(
154
+ "Download History",
155
+ df.to_csv(index=False),
156
+ file_name="history.csv"
157
+ )
158
+ else:
159
+ st.info("No calculations yet.")