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2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
from typing import Literal
|
5 |
+
import re
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
Operation = Literal["+", "-", "x", "/"]
|
10 |
+
|
11 |
+
currOp: Operation = None
|
12 |
+
currInput = 0
|
13 |
+
currOutput = 0
|
14 |
+
|
15 |
+
|
16 |
+
def __cleanedOutput(output: str) -> str:
|
17 |
+
if output == "0":
|
18 |
+
return ""
|
19 |
+
|
20 |
+
print(f"==={output=}")
|
21 |
+
# output = str(round(float(output), 3))
|
22 |
+
return output.rstrip('0').rstrip('.') if '.' in output else output
|
23 |
+
|
24 |
+
|
25 |
+
def calc(input1: str, input2: str, operation: Operation) -> float:
|
26 |
+
input1 = float(input1)
|
27 |
+
input2 = float(input2)
|
28 |
+
if operation == "+":
|
29 |
+
return input1 + input2
|
30 |
+
if operation == "-":
|
31 |
+
return input1 - input2
|
32 |
+
if operation == "x":
|
33 |
+
return input1 * input2
|
34 |
+
if operation == "/":
|
35 |
+
return input1 / input2
|
36 |
+
|
37 |
+
|
38 |
+
def calcAndSetOutput(outputBoxValue: str, isFinalCalc: bool = True):
|
39 |
+
global currOp
|
40 |
+
global currInput
|
41 |
+
global currOutput
|
42 |
+
|
43 |
+
isOutputSingleNumber = len(re.sub(r"[0-9\.]", "", outputBoxValue)) == 0
|
44 |
+
print(f"{isOutputSingleNumber=}")
|
45 |
+
if isOutputSingleNumber:
|
46 |
+
currOutput = float(outputBoxValue)
|
47 |
+
else:
|
48 |
+
currOutput = calc(currOutput, currInput, currOp)
|
49 |
+
|
50 |
+
if isFinalCalc:
|
51 |
+
newOutputBoxValue = __cleanedOutput(str(currOutput))
|
52 |
+
print(f"{newOutputBoxValue=}")
|
53 |
+
currOp = None
|
54 |
+
else:
|
55 |
+
newOutputBoxValue = __cleanedOutput(outputBoxValue) + currOp
|
56 |
+
|
57 |
+
currInput = 0
|
58 |
+
return newOutputBoxValue
|
59 |
+
|
60 |
+
|
61 |
+
def reset():
|
62 |
+
global currOp
|
63 |
+
global currInput
|
64 |
+
global currOutput
|
65 |
+
currOp = None
|
66 |
+
currInput = 0
|
67 |
+
currOutput = 0
|
68 |
+
return currOutput
|
69 |
+
|
70 |
+
|
71 |
+
def setOperation(opsBtnValue: str, outputBoxValue: str) -> None:
|
72 |
+
global currOp
|
73 |
+
currOp = opsBtnValue
|
74 |
+
return calcAndSetOutput(outputBoxValue, False)
|
75 |
+
|
76 |
+
|
77 |
+
def setInput(input: str, outputBoxValue: str) -> None:
|
78 |
+
global currInput
|
79 |
+
currInput = currInput * 10 + int(input)
|
80 |
+
print(f"{currInput=}", f"{outputBoxValue=}")
|
81 |
+
return __cleanedOutput(outputBoxValue) + str(input)
|
82 |
+
|
83 |
+
|
84 |
+
with gr.Blocks() as demo:
|
85 |
+
with gr.Row(variant="default", elem_classes=["body-container"], equal_height=False):
|
86 |
+
|
87 |
+
with gr.Row(variant="compact", elem_classes=["main-container"]):
|
88 |
+
outputBox = gr.Textbox(
|
89 |
+
label="Output",
|
90 |
+
value=currOutput,
|
91 |
+
text_align="right",
|
92 |
+
max_lines=1,
|
93 |
+
elem_classes=["output-box"],
|
94 |
+
show_label=False
|
95 |
+
)
|
96 |
+
calcBtn = gr.Button("=", variant="primary", elem_classes=["calc-btn"]).click(
|
97 |
+
fn=calcAndSetOutput,
|
98 |
+
inputs=[outputBox],
|
99 |
+
outputs=outputBox,
|
100 |
+
api_name="calc"
|
101 |
+
)
|
102 |
+
|
103 |
+
with gr.Row(variant="compact", elem_classes=["input-container"], equal_height=True):
|
104 |
+
|
105 |
+
with gr.Row(variant="compact", elem_classes=["btn-container"]) as numBtnRow:
|
106 |
+
numbersBtn: list[gr.Button] = [
|
107 |
+
gr.Button(str(i), min_width=60, elem_classes=["btn"])
|
108 |
+
for i in [*range(1, 10), 0]
|
109 |
+
]
|
110 |
+
for btn in numBtnRow.children:
|
111 |
+
btn: gr.Button = btn
|
112 |
+
btn.click(fn=setInput, inputs=[btn, outputBox], outputs=outputBox, api_name="setInput")
|
113 |
+
|
114 |
+
with gr.Row(variant="panel", elem_classes=["ops-container"]) as opsBtnRow:
|
115 |
+
plus_btn = gr.Button("+", min_width=10, elem_classes=["ops-btn"])
|
116 |
+
minus_btn = gr.Button("-", min_width=10, elem_classes=["ops-btn"])
|
117 |
+
multiply_btn = gr.Button("x", min_width=10, elem_classes=["ops-btn"])
|
118 |
+
divide_btn = gr.Button("/", min_width=10, elem_classes=["ops-btn"])
|
119 |
+
for btn in opsBtnRow.children:
|
120 |
+
btn.click(fn=setOperation, inputs=[btn, outputBox], outputs=outputBox, api_name="setOperation")
|
121 |
+
|
122 |
+
resetBtn = gr.Button("Reset", variant="stop").click(fn=reset, inputs=None, outputs=outputBox, api_name="reset")
|
123 |
+
|
124 |
+
# with gr.Row(variant="panel", elem_classes=["debug-container"]):
|
125 |
+
# gr.Markdown("## Debugging")
|
126 |
+
# gr.Textbox(label="currInput", value=(lambda: currInput), every=1)
|
127 |
+
# gr.Textbox(label="currOutput", value=(lambda: currOutput), every=1)
|
128 |
+
# gr.Textbox(label="currOp", value=(lambda: currOp), every=1)
|
129 |
+
|
130 |
+
|
131 |
+
demo.css = """
|
132 |
+
.body-container {
|
133 |
+
flex-direction: row;
|
134 |
+
# max-width: 500px;
|
135 |
+
# width: 500px;
|
136 |
+
# margin: 0 auto;
|
137 |
+
# text-align: center;
|
138 |
+
# align-items: center;
|
139 |
+
}
|
140 |
+
|
141 |
+
.debug-container {
|
142 |
+
flex-direction: column;
|
143 |
+
# max-width: 500px;
|
144 |
+
width: 300px;
|
145 |
+
# margin: 0 auto;
|
146 |
+
text-align: center;
|
147 |
+
# align-items: center;
|
148 |
+
}
|
149 |
+
|
150 |
+
.main-container {
|
151 |
+
flex-direction: column;
|
152 |
+
max-width: 500px;
|
153 |
+
# width: 500px;
|
154 |
+
margin: 0 auto;
|
155 |
+
text-align: center;
|
156 |
+
# align-items: center;
|
157 |
+
}
|
158 |
+
|
159 |
+
.output-box input {
|
160 |
+
font-size: 2rem;
|
161 |
+
}
|
162 |
+
|
163 |
+
.calc-btn {
|
164 |
+
font-size: 1.5rem;
|
165 |
+
}
|
166 |
+
|
167 |
+
.input-container {
|
168 |
+
flex-direction: row;
|
169 |
+
# max-width: 500px;
|
170 |
+
# width: 500px;
|
171 |
+
# margin: 0 auto;
|
172 |
+
# text-align: center;
|
173 |
+
# align-items: center;
|
174 |
+
}
|
175 |
+
|
176 |
+
.btn-container {
|
177 |
+
# flex-direction: column;
|
178 |
+
# width: 300px;
|
179 |
+
# margin: 0 auto;
|
180 |
+
}
|
181 |
+
|
182 |
+
.ops-container {
|
183 |
+
flex-direction: column;
|
184 |
+
# width: 100px;
|
185 |
+
max-width: 50px;
|
186 |
+
# margin: 0 auto;
|
187 |
+
}
|
188 |
+
|
189 |
+
.btn {
|
190 |
+
font-size: 1.5rem;
|
191 |
+
}
|
192 |
+
|
193 |
+
.ops-btn {
|
194 |
+
font-size: 1.5rem;
|
195 |
+
}
|
196 |
+
"""
|
197 |
+
|
198 |
+
if __name__ == "__main__":
|
199 |
+
demo.launch(debug=True)
|
200 |
+
|