umarcui commited on
Commit
136eb56
·
verified ·
1 Parent(s): 0954443

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -25
app.py CHANGED
@@ -1,27 +1,39 @@
1
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- # Title
4
- st.title("🧮 Simple Calculator")
5
-
6
- # User inputs
7
- num1 = st.number_input("Enter first number", value=0.0)
8
- num2 = st.number_input("Enter second number", value=0.0)
9
-
10
- # Operation selection
11
- operation = st.selectbox("Select operation", ["Add", "Subtract", "Multiply", "Divide"])
12
-
13
- # Perform calculation
14
- if st.button("Calculate"):
15
- if operation == "Add":
16
- result = num1 + num2
17
- elif operation == "Subtract":
18
- result = num1 - num2
19
- elif operation == "Multiply":
20
- result = num1 * num2
21
- elif operation == "Divide":
22
- if num2 == 0:
23
- st.error("❌ Cannot divide by zero.")
24
- else:
25
- result = num1 / num2
26
- if operation != "Divide" or num2 != 0:
27
- st.success(f"Result: {result}")
 
1
  import streamlit as st
2
+ import numpy as np
3
+ import math
4
+
5
+ st.set_page_config(page_title="🧮 Advanced Calculator", layout="centered")
6
+
7
+ st.title("🧠 Advanced Calculator")
8
+ st.markdown("Perform advanced mathematical calculations securely and interactively.")
9
+
10
+ expression = st.text_input("Enter your expression (e.g., sin(30) + log(10) + 2^3):")
11
+
12
+ # Safe math function map
13
+ allowed_names = {
14
+ k: v for k, v in math.__dict__.items() if not k.startswith("__")
15
+ }
16
+ allowed_names.update({
17
+ 'np': np,
18
+ 'sqrt': np.sqrt,
19
+ 'pow': pow,
20
+ 'abs': abs
21
+ })
22
+
23
+ def evaluate_expression(expr):
24
+ try:
25
+ # Replace ^ with ** for Python syntax
26
+ expr = expr.replace("^", "**")
27
+ result = eval(expr, {"__builtins__": {}}, allowed_names)
28
+ return result
29
+ except Exception as e:
30
+ return f"❌ Error: {e}"
31
+
32
+ if expression:
33
+ result = evaluate_expression(expression)
34
+ st.subheader("🧾 Result:")
35
+ st.code(result, language='text')
36
+
37
+ st.markdown("---")
38
+ st.caption("Built using Streamlit | © 2025 AdvancedCalc")
39