0Hojoon0 commited on
Commit
d827080
·
verified ·
1 Parent(s): 5599b58

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -88
app.py CHANGED
@@ -1,91 +1,39 @@
1
  import gradio as gr
2
 
3
  # Create a layout for the calculator
4
- layout = gr.Layout(
5
- gr.InputBox(id='num1', label='Number 1'),
6
- gr.InputBox(id='num2', label='Number 2'),
7
- gr.Dropdown(id='operator', options=['+', '-', '*', '/', '^', 'sqrt', 'exp']),
8
- gr.Button(id='equal', label='='),
9
- gr.TextOutput(id='result')
10
- )
11
-
12
- # Define a function for each operation
13
- def add():
14
- num1 = float(input_box.get_value('num1'))
15
- num2 = float(input_box.get_value('num2'))
16
- return num1 + num2
17
-
18
- def subtract():
19
- num1 = float(input_box.get_value('num1'))
20
- num2 = float(input_box.get_value('num2'))
21
- return num1 - num2
22
-
23
- def multiply():
24
- num1 = float(input_box.get_value('num1'))
25
- num2 = float(input_box.get_value('num2'))
26
- return num1 * num2
27
-
28
- def divide():
29
- num1 = float(input_box.get_value('num1'))
30
- num2 = float(input_box.get_value('num2'))
31
- return num1 / num2
32
-
33
- def power():
34
- num1 = float(input_box.get_value('num1'))
35
- num2 = float(input_box.get_value('num2'))
36
- return num1 ** num2
37
-
38
- def sqrt():
39
- num1 = float(input_box.get_value('num1'))
40
- return num1 ** 0.5
41
-
42
- def exp():
43
- num1 = float(input_box.get_value('num1'))
44
- return num1 ** Math.E
45
-
46
- # Set up the buttons and outputs
47
- button_equal = gr.Button(id='equal', label='=')
48
- button_add = gr.Button(id='add', label='+')
49
- button_subtract = gr.Button(id='subtract', label='-')
50
- button_multiply = gr.Button(id='multiply', label='*')
51
- button_divide = gr.Button(id='divide', label='/')
52
- button_power = gr.Button(id='power', label='^')
53
- button_sqrt = gr.Button(id='sqrt', label='√')
54
- button_exp = gr.Button(id='exp', label='e^x')
55
-
56
- output = gr.TextOutput(id='result', text='')
57
-
58
- # Set up the event handlers
59
- def update_output(sender):
60
- global output
61
- if sender == button_equal:
62
- num1 = float(input_box.get_value('num1'))
63
- num2 = float(input_box.get_value('num2'))
64
- output.text = f'{num1} {sender.label} {num2} = {num1} {sender.label} {num2}'
65
- elif sender == button_add or sender == button_subtract or sender == button_multiply or sender == button_divide:
66
- num1 = float(input_box.get_value('num1'))
67
- num2 = float(input_ box.get_value('num2'))
68
- result = None
69
- if sender == button_add:
70
- result = add()
71
- elif sender == button_subtract:
72
- result = subtract()
73
- elif sender == button_multiply:
74
- result = multiply()
75
- else:
76
- result = divide()
77
- output.text = f'{num1} {sender.label} {num2} = {result}'
78
- elif sender == button_power or sender == button_sqrt or sender == button_exp:
79
- num1 = float(input_box.get_value('num1'))
80
- num2 = float(input_box.get_value('num2'))
81
- result = None
82
- if sender == button_power:
83
- result = power()
84
- elif sender == button_sqrt:
85
- result = sqrt()
86
- else:
87
- result = exp()
88
- output.text = f'{num1} {sender.label} {num2} = {result}'
89
-
90
- # Run the application
91
- gr.run_application(layout)
 
1
  import gradio as gr
2
 
3
  # Create a layout for the calculator
4
+ layout = gr.GridLayout(cols=3, rows=4)
5
+
6
+ # Add labels and entries for the numbers and operators
7
+ layout.add(gr.Label('Number 1:'))
8
+ layout.add(gr.Entry())
9
+ layout.add(gr. Label('Number 2:'))
10
+ layout.add(gr.Entry())
11
+ layout.add(gr.Label('Operator:'))
12
+ layout.add(gr.Choice(options=['+', '-', '*', '/']))
13
+
14
+ # Add a button to submit the calculation
15
+ layout.add(gr.Button('Calculate'))
16
+
17
+ # Define a function to perform the calculation when the button is clicked
18
+ def calculate():
19
+ num1 = float(layout.get_value(0))
20
+ num2 = float(layout.get_value(2))
21
+ operator = layout.get_value(4)
22
+
23
+ if operator == '+':
24
+ result = num1 + num2
25
+ elif operator == '-':
26
+ result = num1 - num2
27
+ elif operator == '*':
28
+ result = num1 * num2
29
+ else:
30
+ result = num1 / num2
31
+
32
+ # Display the result in the final column
33
+ layout.set_value(6, str(result))
34
+
35
+ # Attach the calculate function to the button
36
+ layout.get_widget(5).on_click(calculate)
37
+
38
+ # Start the GUI event loop
39
+ gradio.run(layout)