File size: 10,300 Bytes
ef7934a |
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 |
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()
|