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()