openfree commited on
Commit
afe2127
·
verified ·
1 Parent(s): 197cf43

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -75
app.py CHANGED
@@ -4,6 +4,7 @@ import gradio as gr
4
  # ---------------------------
5
  # Badge URL generation
6
  # ---------------------------
 
7
  def generate_static_badge(label, message, color, label_color, logo, logo_color, style, link):
8
  base = "https://img.shields.io/static/v1"
9
  params = []
@@ -41,6 +42,17 @@ def generate_static_badge(label, message, color, label_color, logo, logo_color,
41
  # Gradio UI
42
  # ---------------------------
43
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
 
 
 
 
 
 
 
 
 
 
 
44
  # Custom CSS
45
  gr.HTML("""
46
  <style>
@@ -203,9 +215,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
203
  value="for-the-badge",
204
  elem_id="style-input"
205
  )
206
-
207
- # Replace color pickers with dropdown selections
208
- color_choices = [f"<div><span class='color-preview' style='background-color:{hex_code}'></span>{name}</div>" for name, hex_code in color_options.items()]
209
  color = gr.Dropdown(
210
  label="Background Color",
211
  choices=list(color_options.keys()),
@@ -233,7 +244,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
233
  gr.HTML("<h3 style='margin-top:0; margin-bottom:16px; font-size:1.3rem;'>💻 HTML Code</h3>")
234
  out_code = gr.Code(label="", language="html", lines=3)
235
 
236
- # 예제 리스트 (label, message, bg_color, label_color, logo, logo_color, style, link)
237
  examples = [
238
  ["Discord", "Openfree AI", "Blue", "Purple", "discord", "white", "for-the-badge", "https://discord.gg/openfreeai"],
239
  ["X.com", "Follow us", "Blue", "Cyan", "x", "white", "for-the-badge", "https://x.com/openfree_ai"],
@@ -245,14 +256,12 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
245
  ["Threads", "함께 즐겨요.", "Navy", "Magenta", "threads", "white", "for-the-badge", "https://www.threads.net/@openfree_ai"],
246
  ]
247
 
248
- # 예제 미리보기
249
  html_items = '<div class="example-grid">'
250
  for idx, ex in enumerate(examples):
251
- # ex = [label, message, bg_color, label_color, logo, logo_color, style, link]
252
  color_hex = color_options.get(ex[2], ex[2])
253
  label_color_hex = color_options.get(ex[3], ex[3])
254
  logo_color_val = color_options.get(ex[5], ex[5]) if ex[5] in color_options else ex[5]
255
-
256
  badge_url = (
257
  "https://img.shields.io/static/v1?" + "&".join([
258
  f"label={urllib.parse.quote(ex[0], safe='')}",
@@ -261,7 +270,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
261
  f"labelColor={urllib.parse.quote(label_color_hex, safe='')}",
262
  f"logo={urllib.parse.quote(ex[4], safe='')}",
263
  f"logoColor={urllib.parse.quote(logo_color_val, safe='')}",
264
- f"style={urllib.parse.quote(ex[6], safe='')}"
265
  ])
266
  )
267
  html_items += f'''
@@ -272,7 +281,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
272
  '''
273
  html_items += '</div>'
274
 
275
- # 예제를 클릭했을 때 적용하는 main 함수
276
  apply_example_js = """
277
  <script>
278
  function updateSelectValue(selectEl, newValue) {
@@ -280,98 +288,63 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
280
  selectEl.value = newValue;
281
  selectEl.dispatchEvent(new Event("change", { bubbles: true }));
282
  }
283
-
284
  function updateTextValue(el, newValue) {
285
  if (!el) return;
286
  el.value = newValue;
287
  el.dispatchEvent(new Event("input", { bubbles: true }));
288
  el.dispatchEvent(new Event("change", { bubbles: true }));
289
  }
290
-
291
  const examples = """ + str(examples).replace("'", '"') + """;
292
-
293
  function applyExample(i) {
294
  const ex = examples[i];
295
- // ex = [label, message, bg_color, label_color, logo, logo_color, style, link]
296
-
297
- // Label
298
- const labelBox = document.querySelector("#label-input textarea, #label-input input");
299
- if (labelBox) {
300
- updateTextValue(labelBox, ex[0]);
301
- }
302
-
303
- // Message
304
- const msgBox = document.querySelector("#message-input textarea, #message-input input");
305
- if (msgBox) {
306
- updateTextValue(msgBox, ex[1]);
307
- }
308
-
309
- // Background Color (Dropdown)
310
- const bgColorSelect = document.querySelector("#color-input select");
311
- if (bgColorSelect) {
312
- updateSelectValue(bgColorSelect, ex[2]);
313
- }
314
-
315
- // Label Background Color (Dropdown)
316
- const labelColorSelect = document.querySelector("#label-color-input select");
317
- if (labelColorSelect) {
318
- updateSelectValue(labelColorSelect, ex[3]);
319
- }
320
-
321
- // Logo
322
- const logoBox = document.querySelector("#logo-input textarea, #logo-input input");
323
- if (logoBox) {
324
- updateTextValue(logoBox, ex[4]);
325
- }
326
-
327
- // Logo Color (Dropdown)
328
- const logoColorSelect = document.querySelector("#logo-color-input select");
329
- if (logoColorSelect) {
330
- updateSelectValue(logoColorSelect, ex[5]);
331
- }
332
-
333
- // Style (Dropdown)
334
- const styleSelect = document.querySelector("#style-input select");
335
- if (styleSelect) {
336
- updateSelectValue(styleSelect, ex[6]);
337
- }
338
-
339
- // Link
340
- const linkBox = document.querySelector("#link-input textarea, #link-input input");
341
- if (linkBox) {
342
- updateTextValue(linkBox, ex[7]);
343
- }
344
  }
345
  </script>
346
  """
347
 
348
  gr.HTML(html_items + apply_example_js)
349
 
350
- # 함수를 수정하여 색상 값을 변환
351
  def process_badge(label, message, color_name, label_color_name, logo, logo_color_name, style, link):
352
- # 색상 이름을 hex 코드로 변환
353
  color_hex = color_options.get(color_name, "#000000")
354
  label_color_hex = color_options.get(label_color_name, "#000000")
355
-
356
- # logo_color가 white나 black인 경우 그대로 사용, 아니면 hex 코드로 변환
357
- if logo_color_name in ["white", "black"]:
358
- logo_color_hex = logo_color_name
359
- else:
360
- logo_color_hex = color_options.get(logo_color_name, "#ffffff")
361
-
362
  return generate_static_badge(label, message, color_hex, label_color_hex, logo, logo_color_hex, style, link)
363
 
364
- # 초기/실시간 업데이트
365
  demo.load(
366
  fn=process_badge,
367
  inputs=[label, message, color, label_color, logo, logo_color, style, link],
368
- outputs=[out_code, out_preview]
369
  )
370
  for inp in [label, message, color, label_color, logo, logo_color, style, link]:
371
  inp.change(
372
  fn=process_badge,
373
  inputs=[label, message, color, label_color, logo, logo_color, style, link],
374
- outputs=[out_code, out_preview]
375
  )
376
 
377
  with gr.TabItem("Help"):
@@ -387,7 +360,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
387
  <li><strong>Logo</strong>: Name of a logo provided by Simple Icons (<a href="https://simpleicons.org/" target="_blank">View List</a>)</li>
388
  <li><strong>Style</strong>: Determines the shape of the badge (flat, plastic, for-the-badge, etc.)</li>
389
  </ul>
390
- <h4 style="color: #457b9d; margin-bottom: 8px;">🎨 ColorSettings</h4>
391
  <ul>
392
  <li><strong>Background Color</strong>: Background color for the right side of the badge</li>
393
  <li><strong>Label Background Color</strong>: Background color for the left side of the badge</li>
@@ -416,4 +389,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
416
  ''')
417
 
418
  if __name__ == "__main__":
419
- demo.launch()
 
4
  # ---------------------------
5
  # Badge URL generation
6
  # ---------------------------
7
+
8
  def generate_static_badge(label, message, color, label_color, logo, logo_color, style, link):
9
  base = "https://img.shields.io/static/v1"
10
  params = []
 
42
  # Gradio UI
43
  # ---------------------------
44
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
45
+ # Product Hunt badge (prominent but non‑intrusive)
46
+ gr.HTML(
47
+ """
48
+ <div style='display:flex; justify-content:center; margin-top:8px; margin-bottom:18px;'>
49
+ <a href="https://www.producthunt.com/posts/badgecraft?embed=true&utm_source=badge-featured&utm_medium=badge&utm_souce=badge-badgecraft" target="_blank">
50
+ <img src="https://api.producthunt.com/widgets/embed-image/v1/featured.svg?post_id=955939&theme=light&t=1745232319766" alt="BadgeCraft | Product Hunt" style="width:250px; height:54px;" />
51
+ </a>
52
+ </div>
53
+ """
54
+ )
55
+
56
  # Custom CSS
57
  gr.HTML("""
58
  <style>
 
215
  value="for-the-badge",
216
  elem_id="style-input"
217
  )
218
+
219
+ # Dropdown color selections
 
220
  color = gr.Dropdown(
221
  label="Background Color",
222
  choices=list(color_options.keys()),
 
244
  gr.HTML("<h3 style='margin-top:0; margin-bottom:16px; font-size:1.3rem;'>💻 HTML Code</h3>")
245
  out_code = gr.Code(label="", language="html", lines=3)
246
 
247
+ # Example list (label, message, bg_color, label_color, logo, logo_color, style, link)
248
  examples = [
249
  ["Discord", "Openfree AI", "Blue", "Purple", "discord", "white", "for-the-badge", "https://discord.gg/openfreeai"],
250
  ["X.com", "Follow us", "Blue", "Cyan", "x", "white", "for-the-badge", "https://x.com/openfree_ai"],
 
256
  ["Threads", "함께 즐겨요.", "Navy", "Magenta", "threads", "white", "for-the-badge", "https://www.threads.net/@openfree_ai"],
257
  ]
258
 
259
+ # Example grid preview
260
  html_items = '<div class="example-grid">'
261
  for idx, ex in enumerate(examples):
 
262
  color_hex = color_options.get(ex[2], ex[2])
263
  label_color_hex = color_options.get(ex[3], ex[3])
264
  logo_color_val = color_options.get(ex[5], ex[5]) if ex[5] in color_options else ex[5]
 
265
  badge_url = (
266
  "https://img.shields.io/static/v1?" + "&".join([
267
  f"label={urllib.parse.quote(ex[0], safe='')}",
 
270
  f"labelColor={urllib.parse.quote(label_color_hex, safe='')}",
271
  f"logo={urllib.parse.quote(ex[4], safe='')}",
272
  f"logoColor={urllib.parse.quote(logo_color_val, safe='')}",
273
+ f"style={urllib.parse.quote(ex[6], safe='')}",
274
  ])
275
  )
276
  html_items += f'''
 
281
  '''
282
  html_items += '</div>'
283
 
 
284
  apply_example_js = """
285
  <script>
286
  function updateSelectValue(selectEl, newValue) {
 
288
  selectEl.value = newValue;
289
  selectEl.dispatchEvent(new Event("change", { bubbles: true }));
290
  }
 
291
  function updateTextValue(el, newValue) {
292
  if (!el) return;
293
  el.value = newValue;
294
  el.dispatchEvent(new Event("input", { bubbles: true }));
295
  el.dispatchEvent(new Event("change", { bubbles: true }));
296
  }
 
297
  const examples = """ + str(examples).replace("'", '"') + """;
 
298
  function applyExample(i) {
299
  const ex = examples[i];
300
+ const selectors = {
301
+ label: "#label-input",
302
+ message: "#message-input",
303
+ color: "#color-input select",
304
+ labelColor: "#label-color-input select",
305
+ logo: "#logo-input",
306
+ logoColor: "#logo-color-input select",
307
+ style: "#style-input select",
308
+ link: "#link-input"
309
+ };
310
+ const labelBox = document.querySelector(`${selectors.label} textarea, ${selectors.label} input`);
311
+ if (labelBox) { updateTextValue(labelBox, ex[0]); }
312
+ const msgBox = document.querySelector(`${selectors.message} textarea, ${selectors.message} input`);
313
+ if (msgBox) { updateTextValue(msgBox, ex[1]); }
314
+ const bgColorSelect = document.querySelector(selectors.color);
315
+ if (bgColorSelect) { updateSelectValue(bgColorSelect, ex[2]); }
316
+ const labelColorSelect = document.querySelector(selectors.labelColor);
317
+ if (labelColorSelect) { updateSelectValue(labelColorSelect, ex[3]); }
318
+ const logoBox = document.querySelector(`${selectors.logo} textarea, ${selectors.logo} input`);
319
+ if (logoBox) { updateTextValue(logoBox, ex[4]); }
320
+ const logoColorSelect = document.querySelector(selectors.logoColor);
321
+ if (logoColorSelect) { updateSelectValue(logoColorSelect, ex[5]); }
322
+ const styleSelect = document.querySelector(selectors.style);
323
+ if (styleSelect) { updateSelectValue(styleSelect, ex[6]); }
324
+ const linkBox = document.querySelector(`${selectors.link} textarea, ${selectors.link} input`);
325
+ if (linkBox) { updateTextValue(linkBox, ex[7]); }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
326
  }
327
  </script>
328
  """
329
 
330
  gr.HTML(html_items + apply_example_js)
331
 
 
332
  def process_badge(label, message, color_name, label_color_name, logo, logo_color_name, style, link):
 
333
  color_hex = color_options.get(color_name, "#000000")
334
  label_color_hex = color_options.get(label_color_name, "#000000")
335
+ logo_color_hex = logo_color_name if logo_color_name in ["white", "black"] else color_options.get(logo_color_name, "#ffffff")
 
 
 
 
 
 
336
  return generate_static_badge(label, message, color_hex, label_color_hex, logo, logo_color_hex, style, link)
337
 
 
338
  demo.load(
339
  fn=process_badge,
340
  inputs=[label, message, color, label_color, logo, logo_color, style, link],
341
+ outputs=[out_code, out_preview],
342
  )
343
  for inp in [label, message, color, label_color, logo, logo_color, style, link]:
344
  inp.change(
345
  fn=process_badge,
346
  inputs=[label, message, color, label_color, logo, logo_color, style, link],
347
+ outputs=[out_code, out_preview],
348
  )
349
 
350
  with gr.TabItem("Help"):
 
360
  <li><strong>Logo</strong>: Name of a logo provided by Simple Icons (<a href="https://simpleicons.org/" target="_blank">View List</a>)</li>
361
  <li><strong>Style</strong>: Determines the shape of the badge (flat, plastic, for-the-badge, etc.)</li>
362
  </ul>
363
+ <h4 style="color: #457b9d; margin-bottom: 8px;">🎨 Color Settings</h4>
364
  <ul>
365
  <li><strong>Background Color</strong>: Background color for the right side of the badge</li>
366
  <li><strong>Label Background Color</strong>: Background color for the left side of the badge</li>
 
389
  ''')
390
 
391
  if __name__ == "__main__":
392
+ demo.launch()