Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,45 +1,40 @@
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
def calculator(
|
4 |
try:
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
elif operation == "Subtract":
|
9 |
-
return num1 - num2
|
10 |
-
elif operation == "Multiply":
|
11 |
-
return num1 * num2
|
12 |
-
elif operation == "Divide":
|
13 |
-
if num2 == 0:
|
14 |
-
return "Error: Division by zero!"
|
15 |
-
return num1 / num2
|
16 |
-
else:
|
17 |
-
return "Invalid operation"
|
18 |
-
except ValueError:
|
19 |
-
return "Please enter valid numbers"
|
20 |
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
gr.Markdown("# Simple Calculator")
|
24 |
|
25 |
with gr.Row():
|
26 |
-
|
27 |
-
num2_input = gr.Number(label="Second Number")
|
28 |
-
|
29 |
-
operation_dropdown = gr.Dropdown(
|
30 |
-
choices=["Add", "Subtract", "Multiply", "Divide"],
|
31 |
-
label="Operation",
|
32 |
-
value="Add"
|
33 |
-
)
|
34 |
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
|
44 |
-
|
45 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
def calculator(input_text):
|
4 |
try:
|
5 |
+
return str(eval(input_text))
|
6 |
+
except:
|
7 |
+
return "Error"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
def update_input(input_text, new_value):
|
10 |
+
if new_value == "C":
|
11 |
+
return ""
|
12 |
+
elif new_value == "=":
|
13 |
+
try:
|
14 |
+
return str(eval(input_text))
|
15 |
+
except:
|
16 |
+
return "Error"
|
17 |
+
else:
|
18 |
+
return input_text + new_value
|
19 |
+
|
20 |
+
with gr.Blocks() as iface:
|
21 |
gr.Markdown("# Simple Calculator")
|
22 |
|
23 |
with gr.Row():
|
24 |
+
input_display = gr.Textbox(value="", label="Display")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
+
button_layout = [
|
27 |
+
["7", "8", "9", "/"],
|
28 |
+
["4", "5", "6", "*"],
|
29 |
+
["1", "2", "3", "-"],
|
30 |
+
["0", ".", "C", "+"],
|
31 |
+
["="]
|
32 |
+
]
|
33 |
|
34 |
+
for row in button_layout:
|
35 |
+
with gr.Row():
|
36 |
+
for button in row:
|
37 |
+
btn = gr.Button(button)
|
38 |
+
btn.click(update_input, inputs=[input_display, btn], outputs=input_display)
|
39 |
|
40 |
+
iface.launch()
|
|