0Learn commited on
Commit
1d643da
·
verified ·
1 Parent(s): b0f2fe4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import math
3
+
4
+ history = []
5
+
6
+ def calculate(expression):
7
+ try:
8
+ expression = expression.replace('^', '**')
9
+ safe_dict = {
10
+ 'sin': math.sin, 'cos': math.cos, 'tan': math.tan,
11
+ 'log': math.log10, 'ln': math.log, 'sqrt': math.sqrt,
12
+ 'pi': math.pi, 'e': math.e
13
+ }
14
+ result = eval(expression, {"__builtins__": None}, safe_dict)
15
+ result_str = f"{result:.8f}".rstrip('0').rstrip('.')
16
+ global history
17
+ history.append(f"{expression} = {result_str}")
18
+ history = history[-5:]
19
+ return result_str, "\n".join(reversed(history))
20
+ except Exception as e:
21
+ return f"Error: {str(e)}", "\n".join(reversed(history))
22
+
23
+ def update_expression(expression, value):
24
+ if expression == "0" and value not in ['.', '(', ')']:
25
+ return value
26
+ return expression + value
27
+
28
+ def backspace(expression):
29
+ return expression[:-1] if len(expression) > 0 else "0"
30
+
31
+ def clear(expression):
32
+ return "0"
33
+
34
+ css = """
35
+ .calculator-display { font-family: 'Digital-7', 'Courier New', monospace; font-size: 24px; }
36
+ .calculator-button { font-family: 'Arial', sans-serif; font-size: 18px; }
37
+ .number-button { background-color: #e0e0e0; }
38
+ .basic-op-button { background-color: #ffffff; }
39
+ .advanced-op-button { background-color: #ffa500; }
40
+ """
41
+
42
+ with gr.Blocks(css=css) as iface:
43
+ gr.Markdown("# Scientific Calculator")
44
+
45
+ with gr.Row():
46
+ history_display = gr.Textbox(label="History (Last 5 Calculations)", lines=5)
47
+
48
+ with gr.Row():
49
+ display = gr.Textbox(value="0", label="Display", elem_classes=["calculator-display"])
50
+
51
+ with gr.Row():
52
+ with gr.Column(scale=3):
53
+ # Number pad
54
+ for i in range(3):
55
+ with gr.Row():
56
+ for j in range(3):
57
+ num = 7 - 3*i + j
58
+ globals()[f"btn_{num}"] = gr.Button(str(num), elem_classes=["calculator-button", "number-button"])
59
+ with gr.Row():
60
+ btn_0 = gr.Button("0", elem_classes=["calculator-button", "number-button"])
61
+ btn_dot = gr.Button(".", elem_classes=["calculator-button", "number-button"])
62
+ btn_equals = gr.Button("=", elem_classes=["calculator-button", "basic-op-button"])
63
+
64
+ with gr.Column(scale=2):
65
+ # Basic operations
66
+ for op, symbol in [("add", "+"), ("sub", "-"), ("mul", "*"), ("div", "/")]:
67
+ globals()[f"btn_{op}"] = gr.Button(symbol, elem_classes=["calculator-button", "basic-op-button"])
68
+ btn_left_paren = gr.Button("(", elem_classes=["calculator-button", "basic-op-button"])
69
+ btn_right_paren = gr.Button(")", elem_classes=["calculator-button", "basic-op-button"])
70
+ btn_clear = gr.Button("C", elem_classes=["calculator-button", "basic-op-button"])
71
+ btn_backspace = gr.Button("⌫", elem_classes=["calculator-button", "basic-op-button"])
72
+
73
+ with gr.Column(scale=2):
74
+ # Advanced operations
75
+ for func in ["sin", "cos", "tan", "log", "ln", "sqrt"]:
76
+ globals()[f"btn_{func}"] = gr.Button(func, elem_classes=["calculator-button", "advanced-op-button"])
77
+ btn_power = gr.Button("^", elem_classes=["calculator-button", "advanced-op-button"])
78
+ btn_pi = gr.Button("π", elem_classes=["calculator-button", "advanced-op-button"])
79
+ btn_e = gr.Button("e", elem_classes=["calculator-button", "advanced-op-button"])
80
+
81
+ # Define button click events
82
+ for btn in [btn_0, btn_1, btn_2, btn_3, btn_4, btn_5, btn_6, btn_7, btn_8, btn_9,
83
+ btn_dot, btn_add, btn_sub, btn_mul, btn_div, btn_left_paren, btn_right_paren,
84
+ btn_power, btn_sin, btn_cos, btn_tan, btn_log, btn_ln, btn_sqrt, btn_pi, btn_e]:
85
+ btn.click(update_expression, inputs=[display, btn], outputs=display)
86
+
87
+ btn_equals.click(calculate, inputs=display, outputs=[display, history_display])
88
+ btn_clear.click(clear, inputs=display, outputs=display)
89
+ btn_backspace.click(backspace, inputs=display, outputs=display)
90
+
91
+ iface.launch()