openfree commited on
Commit
3f8fd88
ยท
verified ยท
1 Parent(s): 2c52707

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -26
app.py CHANGED
@@ -12,13 +12,14 @@ def generate_static_badge(label, message, color, label_color, logo, logo_color,
12
  if message:
13
  params.append(f"message={urllib.parse.quote(message, safe='')}")
14
  if color:
15
- params.append(f"color={urllib.parse.quote(color, safe='')}")
 
16
  if label_color:
17
- params.append(f"labelColor={urllib.parse.quote(label_color, safe='')}")
18
  if logo:
19
  params.append(f"logo={urllib.parse.quote(logo, safe='')}")
20
  if logo_color:
21
- params.append(f"logoColor={urllib.parse.quote(logo_color, safe='')}")
22
  if style:
23
  params.append(f"style={urllib.parse.quote(style, safe='')}")
24
 
@@ -175,9 +176,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
175
  value="for-the-badge",
176
  elem_id="style-input"
177
  )
178
- color = gr.ColorPicker(label="Background Color", value="#5865F2", elem_id="color-input")
179
- label_color = gr.ColorPicker(label="Label Background Color", value="#99AAFF", elem_id="label-color-input")
180
- logo_color = gr.ColorPicker(label="Logo Color", value="#ffffff", elem_id="logo-color-input")
181
  link = gr.Textbox(label="Link (URL)", value="https://discord.gg/openfreeai", elem_id="link-input", lines=1)
182
  with gr.Column():
183
  with gr.Group(elem_classes="badge-section"):
@@ -208,8 +209,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
208
  "https://img.shields.io/static/v1?" + "&".join([
209
  f"label={urllib.parse.quote(ex[0], safe='')}",
210
  f"message={urllib.parse.quote(ex[1], safe='')}",
211
- f"color={urllib.parse.quote(ex[2], safe='')}",
212
- f"labelColor={urllib.parse.quote(ex[3], safe='')}",
213
  f"logo={urllib.parse.quote(ex[4], safe='')}",
214
  f"logoColor={urllib.parse.quote(ex[5], safe='')}",
215
  f"style={urllib.parse.quote(ex[6], safe='')}"
@@ -254,17 +255,30 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
254
  const container = document.getElementById(elemId);
255
  if (!container) return;
256
 
257
- // Gradio ColorPicker ๋Š” ๋ณดํ†ต input[type='text'] ๋กœ ๋ Œ๋”๋จ
258
- let colorInput = container.querySelector("input[type='text'], input[type='color']");
259
- if (!colorInput) return;
260
-
261
- reactSet(colorInput, colorValue);
 
 
 
 
 
262
 
263
- // ๋ฐฐ๊ฒฝ ๋™๊ธฐํ™” (Gradio >= 4.17)
264
- const colorArea = container.querySelector("[data-testid='color-picker']");
265
  if (colorArea) {
266
  colorArea.style.background = colorValue;
267
  }
 
 
 
 
 
 
 
 
268
  }
269
 
270
  const examples = REPLACE_EXAMPLES; // placeholder
@@ -276,20 +290,38 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
276
  reactSet(document.querySelector("#label-input input,#label-input textarea"), ex[0]);
277
  // Message
278
  reactSet(document.querySelector("#message-input input,#message-input textarea"), ex[1]);
279
- // Background Color
280
- setColor("color-input", convertToHexIfNeeded(ex[2]));
281
- // Label Background
282
- setColor("label-color-input", convertToHexIfNeeded(ex[3]));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
283
  // Logo
284
  reactSet(document.querySelector("#logo-input input,#logo-input textarea"), ex[4]);
285
- // Logo Color
286
- setColor("logo-color-input", convertToHexIfNeeded(ex[5]));
287
  // Style (Dropdown)
288
  const styleSelect = document.querySelector("#style-input select");
289
  if (styleSelect) {
290
  styleSelect.value = ex[6];
291
  styleSelect.dispatchEvent(new Event("change", { bubbles: true }));
292
  }
 
293
  // Link
294
  reactSet(document.querySelector("#link-input input,#link-input textarea"), ex[7]);
295
  }
@@ -304,18 +336,24 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
304
  # ---------------------------
305
  # 4) ์‹ค์‹œ๊ฐ„ ์—…๋ฐ์ดํŠธ
306
  # ---------------------------
 
 
 
 
307
  # - ์ดˆ๊ธฐ ๋กœ๋“œ
308
  demo.load(
309
- fn=generate_static_badge,
310
  inputs=[label, message, color, label_color, logo, logo_color, style, link],
311
  outputs=[out_code, out_preview]
312
  )
313
- # - ๋ณ€๊ฒฝ ์ด๋ฒคํŠธ
 
314
  for inp in [label, message, color, label_color, logo, logo_color, style, link]:
315
  inp.change(
316
- fn=generate_static_badge,
317
  inputs=[label, message, color, label_color, logo, logo_color, style, link],
318
- outputs=[out_code, out_preview]
 
319
  )
320
 
321
  # ---------------------------
@@ -365,4 +403,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
365
  ''')
366
 
367
  if __name__ == "__main__":
368
- demo.launch()
 
12
  if message:
13
  params.append(f"message={urllib.parse.quote(message, safe='')}")
14
  if color:
15
+ # ์ƒ‰์ƒ์—์„œ # ๊ธฐํ˜ธ ์ œ๊ฑฐ (URL ์ธ์ฝ”๋”ฉ ๋ฌธ์ œ ๋ฐฉ์ง€)
16
+ params.append(f"color={urllib.parse.quote(color.lstrip('#'), safe='')}")
17
  if label_color:
18
+ params.append(f"labelColor={urllib.parse.quote(label_color.lstrip('#'), safe='')}")
19
  if logo:
20
  params.append(f"logo={urllib.parse.quote(logo, safe='')}")
21
  if logo_color:
22
+ params.append(f"logoColor={urllib.parse.quote(logo_color.lstrip('#'), safe='')}")
23
  if style:
24
  params.append(f"style={urllib.parse.quote(style, safe='')}")
25
 
 
176
  value="for-the-badge",
177
  elem_id="style-input"
178
  )
179
+ color = gr.ColorPicker(label="Background Color", value="#5865F2", elem_id="color-input", interactive=True)
180
+ label_color = gr.ColorPicker(label="Label Background Color", value="#99AAFF", elem_id="label-color-input", interactive=True)
181
+ logo_color = gr.ColorPicker(label="Logo Color", value="#ffffff", elem_id="logo-color-input", interactive=True)
182
  link = gr.Textbox(label="Link (URL)", value="https://discord.gg/openfreeai", elem_id="link-input", lines=1)
183
  with gr.Column():
184
  with gr.Group(elem_classes="badge-section"):
 
209
  "https://img.shields.io/static/v1?" + "&".join([
210
  f"label={urllib.parse.quote(ex[0], safe='')}",
211
  f"message={urllib.parse.quote(ex[1], safe='')}",
212
+ f"color={urllib.parse.quote(ex[2].lstrip('#'), safe='')}",
213
+ f"labelColor={urllib.parse.quote(ex[3].lstrip('#'), safe='')}",
214
  f"logo={urllib.parse.quote(ex[4], safe='')}",
215
  f"logoColor={urllib.parse.quote(ex[5], safe='')}",
216
  f"style={urllib.parse.quote(ex[6], safe='')}"
 
255
  const container = document.getElementById(elemId);
256
  if (!container) return;
257
 
258
+ // Gradio ColorPicker ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€ ๊ตฌ์กฐ ์ ‘๊ทผ
259
+ const colorInputs = container.querySelectorAll("input");
260
+ if (colorInputs.length === 0) return;
261
+
262
+ // ์ปฌ๋Ÿฌํ”ผ์ปค ์š”์†Œ ๋ฐ ํ…์ŠคํŠธ ์ž…๋ ฅ ์š”์†Œ ๋ชจ๋‘์— ์ ์šฉ
263
+ for (const input of colorInputs) {
264
+ if (input.type === 'color' || input.type === 'text') {
265
+ reactSet(input, colorValue);
266
+ }
267
+ }
268
 
269
+ // ์ƒ‰์ƒ ํ‘œ์‹œ ์˜์—ญ ์—…๋ฐ์ดํŠธ (Gradio ๋ฒ„์ „์— ๋”ฐ๋ผ ์„ ํƒ์ž ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ์Œ)
270
+ const colorArea = container.querySelector("[data-testid='color-picker'], .color-circle, .color-preview");
271
  if (colorArea) {
272
  colorArea.style.background = colorValue;
273
  }
274
+
275
+ // ์ถ”๊ฐ€: ๋ณ€๊ฒฝ ์‚ฌํ•ญ ์‹ค์‹œ๊ฐ„ ๋ฐ˜์˜์„ ์œ„ํ•œ ์ฒ˜๋ฆฌ
276
+ setTimeout(() => {
277
+ const anyInput = colorInputs[0];
278
+ if (anyInput) {
279
+ anyInput.dispatchEvent(new Event('change', { bubbles: true }));
280
+ }
281
+ }, 50);
282
  }
283
 
284
  const examples = REPLACE_EXAMPLES; // placeholder
 
290
  reactSet(document.querySelector("#label-input input,#label-input textarea"), ex[0]);
291
  // Message
292
  reactSet(document.querySelector("#message-input input,#message-input textarea"), ex[1]);
293
+
294
+ // ์ƒ‰์ƒ ์„ค์ • - ์ ์šฉ ์ˆœ์„œ์™€ ์ง€์—ฐ ์ถ”๊ฐ€
295
+ setTimeout(() => {
296
+ // Background Color
297
+ setColor("color-input", convertToHexIfNeeded(ex[2]));
298
+
299
+ setTimeout(() => {
300
+ // Label Background
301
+ setColor("label-color-input", convertToHexIfNeeded(ex[3]));
302
+
303
+ setTimeout(() => {
304
+ // Logo Color
305
+ setColor("logo-color-input", convertToHexIfNeeded(ex[5]));
306
+
307
+ // ๋ชจ๋“  ์ƒ‰์ƒ ์„ค์ • ํ›„ ๊ฐ•์ œ ์—…๋ฐ์ดํŠธ ํŠธ๋ฆฌ๊ฑฐ
308
+ document.querySelector("#label-input input").dispatchEvent(
309
+ new Event('change', { bubbles: true })
310
+ );
311
+ }, 50);
312
+ }, 50);
313
+ }, 50);
314
+
315
  // Logo
316
  reactSet(document.querySelector("#logo-input input,#logo-input textarea"), ex[4]);
317
+
 
318
  // Style (Dropdown)
319
  const styleSelect = document.querySelector("#style-input select");
320
  if (styleSelect) {
321
  styleSelect.value = ex[6];
322
  styleSelect.dispatchEvent(new Event("change", { bubbles: true }));
323
  }
324
+
325
  // Link
326
  reactSet(document.querySelector("#link-input input,#link-input textarea"), ex[7]);
327
  }
 
336
  # ---------------------------
337
  # 4) ์‹ค์‹œ๊ฐ„ ์—…๋ฐ์ดํŠธ
338
  # ---------------------------
339
+ # ์‹ค์‹œ๊ฐ„ ์—…๋ฐ์ดํŠธ ๋นˆ๋„ ์ฆ๊ฐ€ ๋ฐ ๋ณ„๋„ ํ•จ์ˆ˜ ์ถ”๊ฐ€
340
+ def update_badge(*args):
341
+ return generate_static_badge(*args)
342
+
343
  # - ์ดˆ๊ธฐ ๋กœ๋“œ
344
  demo.load(
345
+ fn=update_badge,
346
  inputs=[label, message, color, label_color, logo, logo_color, style, link],
347
  outputs=[out_code, out_preview]
348
  )
349
+
350
+ # - ๋ณ€๊ฒฝ ์ด๋ฒคํŠธ - ๊ฐ ์ž…๋ ฅ์— ๋Œ€ํ•ด ๋…๋ฆฝ์ ์œผ๋กœ ์ฒ˜๋ฆฌ
351
  for inp in [label, message, color, label_color, logo, logo_color, style, link]:
352
  inp.change(
353
+ fn=update_badge,
354
  inputs=[label, message, color, label_color, logo, logo_color, style, link],
355
+ outputs=[out_code, out_preview],
356
+ queue=False # ๋น ๋ฅธ ์‘๋‹ต์„ ์œ„ํ•ด ํ ์‚ฌ์šฉ ์•ˆ ํ•จ
357
  )
358
 
359
  # ---------------------------
 
403
  ''')
404
 
405
  if __name__ == "__main__":
406
+ demo.launch()