File size: 6,918 Bytes
185e69f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78d8f43
185e69f
78d8f43
185e69f
 
 
 
 
 
78d8f43
185e69f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78d8f43
 
185e69f
 
 
 
78d8f43
185e69f
 
 
 
 
 
 
 
 
 
78d8f43
 
185e69f
 
 
 
 
78d8f43
 
 
 
 
185e69f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import gradio as gr

def add_numbers(data):
    """Python function that adds two numbers"""
    try:
        num1 = float(data.get("num1", 0))
        num2 = float(data.get("num2", 0))
        result = num1 + num2
        return {"result": result, "expression": f"{num1} + {num2} = {result}"}
    except (ValueError, TypeError) as e:
        return {"result": "Error", "expression": f"Invalid input: {e}"}

# HTML for the math calculator
MATH_HTML = """
<div id="math-app">
  <h1>Math Calculator</h1>
  <p class="subtitle">Custom HTML calling Python via Gradio</p>

  <div class="calculator">
    <div class="input-row">
      <div class="input-group">
        <label>First Number</label>
        <input type="number" id="num1" placeholder="Enter number..." value="0">
      </div>
      <div class="operator">+</div>
      <div class="input-group">
        <label>Second Number</label>
        <input type="number" id="num2" placeholder="Enter number..." value="0">
      </div>
    </div>

    <button id="calculate-btn">Calculate (calls Python)</button>

    <div class="result-box" id="result-box">
      <div class="result-label">Result from Python:</div>
      <div class="result-value" id="result-value">${value.result !== undefined ? value.result : '—'}</div>
      <div class="result-expression" id="result-expression">${value.expression || ''}</div>
    </div>

    <div class="status" id="status">Ready</div>
  </div>
</div>
"""

# CSS for the math calculator
MATH_CSS = """
<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
html, body {
  height: 100%;
  width: 100%;
  overflow: hidden;
  font-family: system-ui, -apple-system, sans-serif;
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
}
#math-app {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
  color: #fff;
  padding: 2rem;
}
h1 {
  font-size: 2.5rem;
  text-align: center;
  background: linear-gradient(90deg, #00d2ff, #3a7bd5);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: 0.5rem;
}
.subtitle {
  color: #aaa;
  font-size: 1.1rem;
  margin-bottom: 2rem;
}
.calculator {
  background: rgba(255,255,255,0.05);
  padding: 2rem;
  border-radius: 1rem;
  border: 1px solid rgba(255,255,255,0.1);
  width: 100%;
  max-width: 500px;
}
.input-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  margin-bottom: 1.5rem;
}
.input-group {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
.input-group label {
  font-size: 0.9rem;
  color: #aaa;
}
.input-group input {
  width: 120px;
  padding: 1rem;
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: 0.5rem;
  background: rgba(255,255,255,0.05);
  color: white;
  font-size: 1.5rem;
  text-align: center;
}
.input-group input:focus {
  outline: none;
  border-color: #00d2ff;
}
.operator {
  font-size: 2rem;
  color: #00d2ff;
  margin-top: 1.5rem;
}
#calculate-btn {
  width: 100%;
  padding: 1rem;
  border: none;
  border-radius: 0.5rem;
  background: linear-gradient(135deg, #00d2ff, #3a7bd5);
  color: white;
  font-size: 1.1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  margin-bottom: 1.5rem;
}
#calculate-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 15px rgba(0, 210, 255, 0.3);
}
#calculate-btn:active {
  transform: translateY(0);
}
.result-box {
  background: rgba(0, 210, 255, 0.1);
  border: 1px solid rgba(0, 210, 255, 0.3);
  border-radius: 0.5rem;
  padding: 1.5rem;
  text-align: center;
  margin-bottom: 1rem;
}
.result-label {
  font-size: 0.9rem;
  color: #aaa;
  margin-bottom: 0.5rem;
}
.result-value {
  font-size: 3rem;
  font-weight: bold;
  color: #00d2ff;
}
.result-expression {
  font-size: 1rem;
  color: #aaa;
  margin-top: 0.5rem;
}
.status {
  text-align: center;
  padding: 0.5rem;
  color: #666;
  font-size: 0.9rem;
}

/* Hide Gradio chrome */
footer { display: none !important; }
.gradio-container, [class*="gradio-container"] {
  max-width: 100% !important;
  width: 100% !important;
  padding: 0 !important;
  margin: 0 !important;
  min-height: 0 !important;
  height: 100% !important;
}
main.fillable, main[class*="fillable"], .app {
  padding: 0 !important;
  margin: 0 !important;
  min-height: 0 !important;
}
.block, .block.padded {
  padding: 0 !important;
  border: none !important;
  background: transparent !important;
}
html, body, #root, .gradio-container, .contain, gradio-app {
  height: 100% !important;
  margin: 0 !important;
  padding: 0 !important;
  overflow: hidden !important;
  min-height: 0 !important;
}
[class*="svelte"] {
  padding: 0 !important;
  margin: 0 !important;
}
/* Prevent iframe resizer infinite loop */
.contain, .main, gradio-app {
  min-height: 0 !important;
  max-height: 100% !important;
}
</style>
"""

# JavaScript that wires up the UI and calls Python via trigger
MATH_JS = """
(function() {
  function $(sel) { return element.querySelector(sel); }

  function updateStatus(msg) {
    $('#status').textContent = msg;
  }

  // When calculate button is clicked
  $('#calculate-btn').addEventListener('click', function() {
    var num1 = parseFloat($('#num1').value) || 0;
    var num2 = parseFloat($('#num2').value) || 0;

    updateStatus('Calling Python...');

    // Update props.value with the data to send to Python
    props.value = { num1: num1, num2: num2 };

    // Trigger the 'submit' event which calls the Python function
    trigger('submit');
  });

  // Allow Enter key to trigger calculation
  $('#num1').addEventListener('keypress', function(e) {
    if (e.key === 'Enter') $('#calculate-btn').click();
  });
  $('#num2').addEventListener('keypress', function(e) {
    if (e.key === 'Enter') $('#calculate-btn').click();
  });

  updateStatus('Ready - Enter numbers and click Calculate');
})();
"""

# JavaScript to handle the response from Python
MATH_JS_CHANGE = """
(function() {
  function $(sel) { return element.querySelector(sel); }

  // This runs when the component value changes (i.e., when Python returns)
  if (props.value && props.value.result !== undefined) {
    $('#result-value').textContent = props.value.result;
    $('#result-expression').textContent = props.value.expression || '';
    $('#status').textContent = 'Calculated by Python at ' + new Date().toLocaleTimeString();
  }
})();
"""

# Create the Gradio app
with gr.Blocks(fill_height=True, title="Math Calculator") as demo:
    calculator = gr.HTML(
        value={},
        html_template=MATH_CSS + MATH_HTML,
        js_on_load=MATH_JS,
        container=False
    )

    # When JS triggers 'submit', call add_numbers and update the component
    calculator.submit(add_numbers, inputs=calculator, outputs=calculator)

if __name__ == "__main__":
    demo.launch()