import urllib.parse
import gradio as gr
# ---------------------------
# Badge URL generation
# ---------------------------
def generate_static_badge(label, message, color, label_color, logo, logo_color, style, link):
base = "https://img.shields.io/static/v1"
params = []
if 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 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='')}")
if style:
params.append(f"style={urllib.parse.quote(style, safe='')}")
badge_url = base + ("?" + "&".join(params) if params else "")
if link:
html_code = f'
'
else:
html_code = f'
'
badge_preview = f"""
{html_code}
"""
return html_code, badge_preview
# ---------------------------
# Gradio UI
# ---------------------------
with gr.Blocks(theme=gr.themes.Soft()) as demo:
# Custom CSS
gr.HTML("""
""")
# Header section
gr.HTML("""
π¨ BadgeCraft
Create beautiful badges with live preview and HTML snippet
β¨ MIT License
π₯ Created by OpenFreeAI Team
""")
# Define color options
color_options = {
"Red": "#ff0000",
"Blue": "#0000ff",
"Green": "#00ff00",
"Yellow": "#ffff00",
"Purple": "#800080",
"Orange": "#ffa500",
"Pink": "#ffc0cb",
"Teal": "#008080",
"Navy": "#000080",
"Lime": "#00ff00",
"Cyan": "#00ffff",
"Magenta": "#ff00ff",
"Gold": "#ffd700",
"Silver": "#c0c0c0"
}
with gr.Tabs():
with gr.TabItem("Badge Generator"):
with gr.Row():
with gr.Column():
with gr.Group(elem_classes="badge-section"):
gr.HTML("βοΈ Badge Settings
")
label = gr.Textbox(label="Label", value="Discord", elem_id="label-input", lines=1)
message = gr.Textbox(label="Message", value="Join our community", elem_id="message-input", lines=1)
logo = gr.Textbox(label="Logo", value="discord", elem_id="logo-input", lines=1)
style = gr.Dropdown(
label="Style",
choices=["flat", "flat-square", "plastic", "for-the-badge", "social"],
value="for-the-badge",
elem_id="style-input"
)
# Replace color pickers with dropdown selections
color_choices = [f"{name}
" for name, hex_code in color_options.items()]
color = gr.Dropdown(
label="Background Color",
choices=list(color_options.keys()),
value="Blue",
elem_id="color-input"
)
label_color = gr.Dropdown(
label="Label Background Color",
choices=list(color_options.keys()),
value="Purple",
elem_id="label-color-input"
)
logo_color = gr.Dropdown(
label="Logo Color",
choices=["white", "black"] + list(color_options.keys()),
value="white",
elem_id="logo-color-input"
)
link = gr.Textbox(label="Link (URL)", value="https://discord.gg/openfreeai", elem_id="link-input", lines=1)
with gr.Column():
with gr.Group(elem_classes="badge-section"):
gr.HTML("ποΈ Preview
")
out_preview = gr.HTML(label="")
with gr.Group(elem_classes="badge-section"):
gr.HTML("π» HTML Code
")
out_code = gr.Code(label="", language="html", lines=3)
# μμ 리μ€νΈ (label, message, bg_color, label_color, logo, logo_color, style, link)
examples = [
["Discord", "Openfree AI", "Blue", "Purple", "discord", "white", "for-the-badge", "https://discord.gg/openfreeai"],
["X.com", "Follow us", "Blue", "Cyan", "x", "white", "for-the-badge", "https://x.com/openfree_ai"],
["Collections","Explore", "Orange", "Yellow", "huggingface","black","for-the-badge", "https://huggingface.co/collections/VIDraft/best-open-ai-services-68057e6e312880ea92abaf4c"],
["GitHub", "Star us", "Navy", "Lime", "github", "white", "for-the-badge", "https://github.com/openfreeai"],
["YouTube", "Watch now", "Red", "Orange", "youtube", "white", "for-the-badge", "https://www.youtube.com/@AITechTree"],
["Facebook", "Like us", "Blue", "Cyan", "facebook", "white", "for-the-badge", "https://www.facebook.com/profile.php?id=61575353674679"],
["Instagram", "εζ
θ¬δΈ", "Magenta", "Pink", "instagram", "white", "for-the-badge", "https://www.instagram.com/openfree_ai/"],
["Threads", "ν¨κ» μ¦κ²¨μ.", "Navy", "Magenta", "threads", "white", "for-the-badge", "https://www.threads.net/@openfree_ai"],
]
# μμ 미리보기
html_items = ''
for idx, ex in enumerate(examples):
# ex = [label, message, bg_color, label_color, logo, logo_color, style, link]
color_hex = color_options.get(ex[2], ex[2])
label_color_hex = color_options.get(ex[3], ex[3])
logo_color_val = color_options.get(ex[5], ex[5]) if ex[5] in color_options else ex[5]
badge_url = (
"https://img.shields.io/static/v1?" + "&".join([
f"label={urllib.parse.quote(ex[0], safe='')}",
f"message={urllib.parse.quote(ex[1], safe='')}",
f"color={urllib.parse.quote(color_hex, safe='')}",
f"labelColor={urllib.parse.quote(label_color_hex, safe='')}",
f"logo={urllib.parse.quote(ex[4], safe='')}",
f"logoColor={urllib.parse.quote(logo_color_val, safe='')}",
f"style={urllib.parse.quote(ex[6], safe='')}"
])
)
html_items += f'''
{ex[0]}
'''
html_items += '
'
# μμ λ₯Ό ν΄λ¦νμ λ μ μ©νλ main ν¨μ
apply_example_js = """
"""
gr.HTML(html_items + apply_example_js)
# ν¨μλ₯Ό μμ νμ¬ μμ κ°μ λ³ν
def process_badge(label, message, color_name, label_color_name, logo, logo_color_name, style, link):
# μμ μ΄λ¦μ hex μ½λλ‘ λ³ν
color_hex = color_options.get(color_name, "#000000")
label_color_hex = color_options.get(label_color_name, "#000000")
# logo_colorκ° whiteλ blackμΈ κ²½μ° κ·Έλλ‘ μ¬μ©, μλλ©΄ hex μ½λλ‘ λ³ν
if logo_color_name in ["white", "black"]:
logo_color_hex = logo_color_name
else:
logo_color_hex = color_options.get(logo_color_name, "#ffffff")
return generate_static_badge(label, message, color_hex, label_color_hex, logo, logo_color_hex, style, link)
# μ΄κΈ°/μ€μκ° μ
λ°μ΄νΈ
demo.load(
fn=process_badge,
inputs=[label, message, color, label_color, logo, logo_color, style, link],
outputs=[out_code, out_preview]
)
for inp in [label, message, color, label_color, logo, logo_color, style, link]:
inp.change(
fn=process_badge,
inputs=[label, message, color, label_color, logo, logo_color, style, link],
outputs=[out_code, out_preview]
)
with gr.TabItem("Help"):
gr.HTML('''
π How to Use BadgeCraft
β¨ What are Badges?
Badges are small visual indicators that can be used in README files, websites, and documentation. Shields.io badges are widely used to display project status, social media links, version information, and more.
π οΈ Basic Settings
- Label: Text displayed on the left side of the badge (e.g., "Discord", "Version", "Status")
- Message: Text displayed on the right side of the badge
- Logo: Name of a logo provided by Simple Icons (View List)
- Style: Determines the shape of the badge (flat, plastic, for-the-badge, etc.)
π¨ ColorSettings
- Background Color: Background color for the right side of the badge
- Label Background Color: Background color for the left side of the badge
- Logo Color: Color of the logo (e.g. white or black)
π Using the HTML
Copy the generated HTML code and paste it into your website, blog, GitHub README, etc.
HTML works in GitHub READMEs, but if you prefer markdown, use 
.
π‘ Tips
- Click on any example in the grid to automatically fill in all settings
- The preview updates in real-time as you make changes
- You can use over 2000+ logos from Simple Icons - just enter the name
- Choose from predefined colors in the dropdown menus for consistent results
''')
# Footer
gr.HTML('''
''')
if __name__ == "__main__":
demo.launch()