ZeroTech commited on
Commit
3df1378
·
1 Parent(s): 739f282

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -8
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
 
 
3
  def calculator(num1, num2, operation):
4
  if operation == "+":
5
  return num1 + num2
@@ -13,12 +14,82 @@ def calculator(num1, num2, operation):
13
  else:
14
  return "Error: Division by zero"
15
 
16
- iface = gr.Interface(fn=calculator,
17
- inputs=[gr.inputs.Number(default=0, label="Num1"),
18
- gr.inputs.Number(default=0, label="Num2"),
19
- gr.inputs.Radio(["+", "-", "*", "/"], label="Operation")],
20
- outputs=gr.outputs.Textbox(type="text", label="Result"),
21
- title="Calculator with Buttons",
22
- theme="compact")
23
 
24
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
 
3
+ # Define the calculator function
4
  def calculator(num1, num2, operation):
5
  if operation == "+":
6
  return num1 + num2
 
14
  else:
15
  return "Error: Division by zero"
16
 
17
+ # Define the button labels and values
18
+ button_labels = ["7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"]
 
 
 
 
 
19
 
20
+ button_values = [
21
+ [7, 8, 9, "/"],
22
+ [4, 5, 6, "*"],
23
+ [1, 2, 3, "-"],
24
+ [0, ".", "=", "+"]
25
+ ]
26
+
27
+ # Define the interface
28
+ iface = gr.Interface(
29
+ fn=calculator,
30
+ inputs=[
31
+ gr.inputs.Number(default=0, label="Num1", key="num1"),
32
+ gr.inputs.Number(default=0, label="Num2", key="num2"),
33
+ gradio.inputs.InputGroup(button_labels, button_values, label="Operation", key="operation")
34
+ ],
35
+ outputs=gr.outputs.Textbox(type="text", label="Result"),
36
+ title="Calculator with Buttons",
37
+ theme="default",
38
+ layout="unaligned",
39
+ allow_flagging=False,
40
+ allow_screenshot=False,
41
+ allow_share=False
42
+ )
43
+
44
+ # Define the custom HTML and CSS for the interface
45
+ html = """
46
+ <style>
47
+ .button-group {
48
+ display: grid;
49
+ grid-template-columns: repeat(4, 1fr);
50
+ grid-gap: 10px;
51
+ margin-top: 10px;
52
+ margin-bottom: 20px;
53
+ }
54
+ .button-group button {
55
+ font-size: 24px;
56
+ font-weight: bold;
57
+ color: white;
58
+ background-color: #444444;
59
+ border: none;
60
+ border-radius: 5px;
61
+ padding: 10px;
62
+ cursor: pointer;
63
+ }
64
+ .button-group button:hover {
65
+ background-color: #555555;
66
+ }
67
+ .button-group button:active {
68
+ background-color: #666666;
69
+ }
70
+ .button-group button[data-value="="] {
71
+ background-color: #009900;
72
+ }
73
+ .button-group button[data-value="+"],
74
+ .button-group button[data-value="-"],
75
+ .button-group button[data-value="*"],
76
+ .button-group button[data-value="/"] {
77
+ background-color: #ff6600;
78
+ }
79
+ .button-group button[data-value="."],
80
+ .button-group button[data-value="0"] {
81
+ grid-column: span 2;
82
+ }
83
+ .output-container {
84
+ margin-top: 20px;
85
+ }
86
+ </style>
87
+ <div class="input-container">
88
+ <label for="num1">Num1:</label>
89
+ <input id="num1" type="number" name="num1" value="{{ num1 }}" step="any" required>
90
+ </div>
91
+ <div class="input-container">
92
+ <label for="num2">Num2:</label>
93
+ <input id="num2" type="number" name="num2" value="{{ num2 }}" step="any" required>
94
+ </div>
95
+ <div class