Badge / app.py
fantos's picture
Create app.py
ef7934a verified
raw
history blame
10.3 kB
import urllib.parse
import gradio as gr
# ----------------------------------------------------
# 1. ๋ฐฐ์ง€ URL ์ƒ์„ฑ ํ•จ์ˆ˜ ์ •์˜
# ----------------------------------------------------
def generate_static_badge(label, message, color, style, label_color, logo, logo_color):
base = "https://img.shields.io/static/v1"
params = []
if label: # Label์ด ๋นˆ ๋ฌธ์ž์—ด์ด ์•„๋‹Œ ๊ฒฝ์šฐ์—๋งŒ ์ถ”๊ฐ€
params.append(f"label={urllib.parse.quote(label, safe='')}")
if message:
params.append(f"message={urllib.parse.quote(message, safe='')}")
if color:
params.append(f"color={urllib.parse.quote(color, safe='')}")
if style:
params.append(f"style={urllib.parse.quote(style, safe='')}")
if label_color:
params.append(f"labelColor={urllib.parse.quote(label_color, safe='')}")
if logo:
params.append(f"logo={urllib.parse.quote(logo, safe='')}")
if logo_color:
params.append(f"logoColor={urllib.parse.quote(logo_color, safe='')}")
url = base + ("?" + "&".join(params) if params else "")
html_code = f'<img src="{url}" alt="{label or message} badge">'
return html_code, url # (HTML ์ฝ”๋“œ ์Šค๋‹ˆํŽซ, ์ด๋ฏธ์ง€ URL)์„ ๋ฐ˜ํ™˜
def generate_dynamic_json_badge(json_url, json_path, label, prefix, suffix, color, style, label_color, logo, logo_color):
base = "https://img.shields.io/badge/dynamic/json"
params = []
if json_url:
params.append(f"url={urllib.parse.quote(json_url, safe='')}")
if json_path:
params.append(f"query={urllib.parse.quote(json_path, safe='')}")
if label:
params.append(f"label={urllib.parse.quote(label, safe='')}")
if prefix:
params.append(f"prefix={urllib.parse.quote(prefix, safe='')}")
if suffix:
params.append(f"suffix={urllib.parse.quote(suffix, safe='')}")
if color:
params.append(f"color={urllib.parse.quote(color, safe='')}")
if style:
params.append(f"style={urllib.parse.quote(style, safe='')}")
if label_color:
params.append(f"labelColor={urllib.parse.quote(label_color, safe='')}")
if logo:
params.append(f"logo={urllib.parse.quote(logo, safe='')}")
if logo_color:
params.append(f"logoColor={urllib.parse.quote(logo_color, safe='')}")
url = base + ("?" + "&".join(params) if params else "")
html_code = f'<img src="{url}" alt="Dynamic JSON badge">'
return html_code, url
def generate_endpoint_badge(endpoint_url, label, color, style, label_color, logo, logo_color):
base = "https://img.shields.io/endpoint"
params = []
if endpoint_url:
params.append(f"url={urllib.parse.quote(endpoint_url, safe='')}")
if label:
params.append(f"label={urllib.parse.quote(label, safe='')}")
if color:
params.append(f"color={urllib.parse.quote(color, safe='')}")
if style:
params.append(f"style={urllib.parse.quote(style, safe='')}")
if label_color:
params.append(f"labelColor={urllib.parse.quote(label_color, safe='')}")
if logo:
params.append(f"logo={urllib.parse.quote(logo, safe='')}")
if logo_color:
params.append(f"logoColor={urllib.parse.quote(logo_color, safe='')}")
url = base + ("?" + "&".join(params) if params else "")
html_code = f'<img src="{url}" alt="Endpoint badge">'
return html_code, url
# ----------------------------------------------------
# 2. Gradio UI ๊ตฌ์„ฑ
# ----------------------------------------------------
with gr.Blocks(theme=gr.themes.Default()) as app:
gr.Markdown("""
# Shields.io Badge Generator ๐Ÿ› 
์ด ์•ฑ์€ [Shields.io](https://shields.io/)์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ฐฐ์ง€๋ฅผ **GUI**๋กœ ๊ฐ„ํŽธํ•˜๊ฒŒ ์ƒ์„ฑํ•˜๋„๋ก ๋„์™€์ค๋‹ˆ๋‹ค.<br>
์•„๋ž˜ ํƒญ์—์„œ ๋ฐฐ์ง€ ์œ ํ˜•์„ ์„ ํƒํ•˜๊ณ  ์›ํ•˜๋Š” ์˜ต์…˜์„ ์ž…๋ ฅํ•ด๋ณด์„ธ์š”. ์ž๋™์œผ๋กœ HTML ์ฝ”๋“œ ์Šค๋‹ˆํŽซ๊ณผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ ์ด๋ฏธ์ง€๋ฅผ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
""")
with gr.Tabs():
# ------------------------------------------------
# --- Tab 1: Static Badge ---
# ------------------------------------------------
with gr.Tab("Static Badge"):
gr.Markdown("""
**์ •์ (Static) ๋ฐฐ์ง€**๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
์˜ˆ: ์ขŒ์ธก ํ…์ŠคํŠธ(label), ์šฐ์ธก ํ…์ŠคํŠธ(message), ์ƒ‰์ƒ(color) ๋“ฑ์„ ์ž…๋ ฅํ•˜์„ธ์š”.
""")
with gr.Row():
lbl = gr.Textbox(label="Label (์ขŒ์ธก ํ…์ŠคํŠธ)", placeholder="์˜ˆ: build")
msg = gr.Textbox(label="Message (์šฐ์ธก ํ…์ŠคํŠธ)", placeholder="์˜ˆ: passing")
with gr.Row():
col = gr.Textbox(label="Color (์ƒ‰์ƒ)", value="blue", placeholder="์˜ˆ: brightgreen, #4c1 ๋“ฑ")
lbl_col = gr.Textbox(label="Label Color (๋ ˆ์ด๋ธ” ๋ฐฐ๊ฒฝ์ƒ‰)", placeholder="(์„ ํƒ ์‚ฌํ•ญ)")
with gr.Row():
logo_in = gr.Textbox(label="Logo (์•„์ด์ฝ˜)", placeholder="์˜ˆ: github (์„ ํƒ ์‚ฌํ•ญ)")
logo_col = gr.Textbox(label="Logo Color (์•„์ด์ฝ˜ ์ƒ‰์ƒ)", placeholder="(์„ ํƒ ์‚ฌํ•ญ)")
style_in = gr.Dropdown(
label="Style (์Šคํƒ€์ผ)",
choices=["flat", "flat-square", "plastic", "for-the-badge", "social"],
value="flat"
)
# ์ถœ๋ ฅ: ์ฝ”๋“œ ์Šค๋‹ˆํŽซ & ์ด๋ฏธ์ง€ ๋ฏธ๋ฆฌ๋ณด๊ธฐ
out_code = gr.Code(label="HTML Snippet", language="html")
out_img = gr.Image(label="Badge Preview", type="auto")
# ์ž…๋ ฅ ๋ณ€ํ™” -> ์ถœ๋ ฅ ๊ฐฑ์‹ 
inputs = [lbl, msg, col, style_in, lbl_col, logo_in, logo_col]
for inp in inputs:
inp.change(
fn=generate_static_badge,
inputs=inputs,
outputs=[out_code, out_img]
)
# ------------------------------------------------
# --- Tab 2: Dynamic JSON Badge ---
# ------------------------------------------------
with gr.Tab("Dynamic JSON Badge"):
gr.Markdown("""
**๋™์ (JSON) ๋ฐฐ์ง€**๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
URL๋กœ๋ถ€ํ„ฐ JSON ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์™€ ํŠน์ • ํ•„๋“œ๋ฅผ ์ถ”์ถœํ•˜์—ฌ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.
""")
with gr.Row():
json_url = gr.Textbox(label="JSON URL", placeholder="์˜ˆ: https://example.com/data.json")
json_path = gr.Textbox(label="JSONPath Query", placeholder="์˜ˆ: $.version")
with gr.Row():
label_dyn = gr.Textbox(label="Label (์ขŒ์ธก ํ…์ŠคํŠธ)", placeholder="(์„ ํƒ ์‚ฌํ•ญ)")
prefix_dyn = gr.Textbox(label="Prefix (์ ‘๋‘์‚ฌ)", placeholder="๊ฐ’ ์•ž์— ๋ถ™์ผ ๋ฌธ์ž์—ด (์„ ํƒ)")
suffix_dyn = gr.Textbox(label="Suffix (์ ‘๋ฏธ์‚ฌ)", placeholder="๊ฐ’ ๋’ค์— ๋ถ™์ผ ๋ฌธ์ž์—ด (์„ ํƒ)")
with gr.Row():
color_dyn = gr.Textbox(label="Color (์ƒ‰์ƒ)", value="blue", placeholder="์˜ˆ: blue, #4183c4 ๋“ฑ")
lbl_color_dyn = gr.Textbox(label="Label Color (๋ ˆ์ด๋ธ” ๋ฐฐ๊ฒฝ์ƒ‰)", placeholder="(์„ ํƒ ์‚ฌํ•ญ)")
with gr.Row():
logo_dyn = gr.Textbox(label="Logo (์•„์ด์ฝ˜)", placeholder="์˜ˆ: google (์„ ํƒ ์‚ฌํ•ญ)")
logo_color_dyn = gr.Textbox(label="Logo Color (์•„์ด์ฝ˜ ์ƒ‰์ƒ)", placeholder="(์„ ํƒ ์‚ฌํ•ญ)")
style_dyn = gr.Dropdown(
label="Style (์Šคํƒ€์ผ)",
choices=["flat", "flat-square", "plastic", "for-the-badge", "social"],
value="flat"
)
out_code2 = gr.Code(label="HTML Snippet", language="html")
out_img2 = gr.Image(label="Badge Preview", type="auto")
inputs_dyn = [
json_url, json_path, label_dyn, prefix_dyn, suffix_dyn,
color_dyn, style_dyn, lbl_color_dyn, logo_dyn, logo_color_dyn
]
for inp in inputs_dyn:
inp.change(
fn=generate_dynamic_json_badge,
inputs=inputs_dyn,
outputs=[out_code2, out_img2]
)
# ------------------------------------------------
# --- Tab 3: Endpoint Badge ---
# ------------------------------------------------
with gr.Tab("Endpoint Badge"):
gr.Markdown("""
**Endpoint ๋ฐฐ์ง€**๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
์—”๋“œํฌ์ธํŠธ(Endpoint)์—์„œ ๋ฏธ๋ฆฌ ์ •์˜๋œ JSON ๊ตฌ์กฐ(`schemaVersion`, `label`, `message`, `color` ๋“ฑ)๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ ,
Shields.io๊ฐ€ ์ด๋ฅผ ์ฝ์–ด์™€ ๋ฐฐ์ง€๋ฅผ ๋ Œ๋”๋งํ•ฉ๋‹ˆ๋‹ค.
""")
with gr.Row():
endpoint = gr.Textbox(label="Endpoint URL", placeholder="๋ฐฐ์ง€ JSON์„ ์ œ๊ณตํ•˜๋Š” ์—”๋“œํฌ์ธํŠธ URL")
label_ep = gr.Textbox(label="Override Label", placeholder="์—”๋“œํฌ์ธํŠธ JSON์˜ label ๋Œ€์‹  (์„ ํƒ)")
with gr.Row():
color_ep = gr.Textbox(label="Override Color", placeholder="์—”๋“œํฌ์ธํŠธ JSON์˜ color ๋Œ€์‹  (์„ ํƒ)")
lbl_color_ep = gr.Textbox(label="Label Color", placeholder="(์„ ํƒ ์‚ฌํ•ญ)")
with gr.Row():
logo_ep = gr.Textbox(label="Logo (์•„์ด์ฝ˜)", placeholder="์˜ˆ: custom (์„ ํƒ ์‚ฌํ•ญ)")
logo_color_ep = gr.Textbox(label="Logo Color", placeholder="(์„ ํƒ ์‚ฌํ•ญ)")
style_ep = gr.Dropdown(
label="Style (์Šคํƒ€์ผ)",
choices=["flat", "flat-square", "plastic", "for-the-badge", "social"],
value="flat"
)
out_code3 = gr.Code(label="HTML Snippet", language="html")
out_img3 = gr.Image(label="Badge Preview", type="auto")
inputs_ep = [
endpoint, label_ep, color_ep, style_ep,
lbl_color_ep, logo_ep, logo_color_ep
]
for inp in inputs_ep:
inp.change(
fn=generate_endpoint_badge,
inputs=inputs_ep,
outputs=[out_code3, out_img3]
)
# ์ „์ฒด Blocks ์ข…๋ฃŒ
# ์‹ค์ œ ์‹คํ–‰ ์‹œ ์•„๋ž˜ ์ฃผ์„์„ ํ•ด์ œํ•˜์—ฌ ์‚ฌ์šฉํ•˜์„ธ์š”.
app.launch()