Update app.py
Browse files
app.py
CHANGED
@@ -4,379 +4,185 @@ import gradio as gr
|
|
4 |
# ---------------------------
|
5 |
# Badge URL generation
|
6 |
# ---------------------------
|
7 |
-
def generate_static_badge(label, message, color, label_color,
|
|
|
8 |
base = "https://img.shields.io/static/v1"
|
9 |
params = []
|
10 |
-
if label:
|
11 |
-
|
12 |
-
if
|
13 |
-
|
14 |
-
if
|
15 |
-
|
16 |
-
if
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
if logo_color:
|
21 |
-
params.append(f"logoColor={urllib.parse.quote(logo_color, safe='')}")
|
22 |
-
if style:
|
23 |
-
params.append(f"style={urllib.parse.quote(style, safe='')}")
|
24 |
-
|
25 |
-
badge_url = base + ("?" + "&".join(params) if params else "")
|
26 |
if link:
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
30 |
|
31 |
-
badge_preview = f"""
|
32 |
-
<div style='padding:30px; background: linear-gradient(135deg, #f8f9fa, #e9ecef);
|
33 |
-
border-radius: 16px; display: flex; justify-content: center;
|
34 |
-
box-shadow: 0 6px 12px rgba(0,0,0,0.05);'>
|
35 |
-
{html_code}
|
36 |
-
</div>
|
37 |
-
"""
|
38 |
-
return html_code, badge_preview
|
39 |
|
40 |
# ---------------------------
|
41 |
# Gradio UI
|
42 |
# ---------------------------
|
43 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
44 |
-
#
|
45 |
gr.HTML("""
|
46 |
<style>
|
47 |
-
body
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
border-radius: 10px !important;
|
65 |
-
font-weight: 600 !important;
|
66 |
-
transition: all 0.3s ease !important;
|
67 |
-
box-shadow: 0 4px 10px rgba(138, 198, 209, 0.3) !important;
|
68 |
-
}
|
69 |
-
.gr-button:hover {
|
70 |
-
transform: translateY(-2px) !important;
|
71 |
-
box-shadow: 0 6px 15px rgba(138, 198, 209, 0.4) !important;
|
72 |
-
}
|
73 |
-
.gr-textbox, .gr-select, .gr-color {
|
74 |
-
background: #e8f6f3 !important;
|
75 |
-
border: 2px solid #d9f0ea !important;
|
76 |
-
border-radius: 12px !important;
|
77 |
-
transition: all 0.3s ease !important;
|
78 |
-
}
|
79 |
-
.gr-textbox:focus, .gr-select:focus, .gr-color:focus {
|
80 |
-
border-color: #a8dadc !important;
|
81 |
-
box-shadow: 0 0 0 3px rgba(168, 218, 220, 0.25) !important;
|
82 |
-
}
|
83 |
-
label.block span {
|
84 |
-
color: #457b9d !important;
|
85 |
-
font-weight: 600 !important;
|
86 |
-
font-size: 1rem !important;
|
87 |
-
}
|
88 |
-
h1 {
|
89 |
-
color: #5e60ce;
|
90 |
-
font-weight: 800;
|
91 |
-
letter-spacing: -0.5px;
|
92 |
-
}
|
93 |
-
h3 {
|
94 |
-
color: #5e60ce;
|
95 |
-
font-weight: 600;
|
96 |
-
}
|
97 |
-
.footer {
|
98 |
-
margin-top: 30px;
|
99 |
-
text-align: center;
|
100 |
-
font-size: 0.9rem;
|
101 |
-
color: #6d6875;
|
102 |
-
}
|
103 |
-
.badge-section {
|
104 |
-
background: rgba(255, 255, 255, 0.7);
|
105 |
-
border-radius: 16px;
|
106 |
-
padding: 20px;
|
107 |
-
box-shadow: 0 4px 12px rgba(0,0,0,0.03);
|
108 |
-
margin-bottom: 24px;
|
109 |
-
border: 1px solid rgba(230, 240, 255, 0.7);
|
110 |
-
}
|
111 |
-
.example-grid {
|
112 |
-
display: grid;
|
113 |
-
grid-template-columns: repeat(4, 1fr);
|
114 |
-
grid-template-rows: repeat(2, auto);
|
115 |
-
gap: 16px;
|
116 |
-
margin-top: 20px;
|
117 |
-
}
|
118 |
-
.example-item {
|
119 |
-
background: linear-gradient(135deg, #f1f8ff, #e8f4ff);
|
120 |
-
border-radius: 12px;
|
121 |
-
padding: 16px;
|
122 |
-
text-align: center;
|
123 |
-
cursor: pointer;
|
124 |
-
transition: all 0.3s ease;
|
125 |
-
border: 2px solid transparent;
|
126 |
-
}
|
127 |
-
.example-item:hover {
|
128 |
-
transform: translateY(-3px);
|
129 |
-
box-shadow: 0 8px 15px rgba(0,0,0,0.05);
|
130 |
-
border-color: #a8dadc;
|
131 |
-
}
|
132 |
-
@media (max-width: 768px) {
|
133 |
-
.example-grid {
|
134 |
-
grid-template-columns: repeat(2, 1fr);
|
135 |
-
}
|
136 |
-
}
|
137 |
-
@media (max-width: 600px) {
|
138 |
-
.example-grid {
|
139 |
-
grid-template-columns: 1fr;
|
140 |
-
}
|
141 |
-
}
|
142 |
</style>
|
143 |
-
""
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
<div style="text-align:center; margin-bottom:24px;">
|
148 |
-
<h1 style="font-size:2.8rem; margin-bottom:0.2em; background: linear-gradient(90deg, #5e60ce, #64dfdf); -webkit-background-clip: text; -webkit-text-fill-color: transparent;">
|
149 |
-
🎨 BadgeCraft
|
150 |
-
</h1>
|
151 |
-
<p style="font-size:1.2rem; margin:0.5em 0; color:#457b9d; max-width:700px; margin:0 auto;">
|
152 |
-
Create beautiful badges with live preview and HTML snippet
|
153 |
-
</p>
|
154 |
-
<div style="margin-top:10px; display:flex; justify-content:center; gap:12px; flex-wrap:wrap;">
|
155 |
-
<span style="display:inline-block; background:#e9f5db; color:#588157; padding:6px 12px; border-radius:30px; font-size:0.9rem;">
|
156 |
-
<strong>✨ MIT License</strong>
|
157 |
-
</span>
|
158 |
-
<span style="display:inline-block; background:#d8f3dc; color:#2d6a4f; padding:6px 12px; border-radius:30px; font-size:0.9rem;">
|
159 |
-
<strong>👥 Created by OpenFreeAI Team</strong>
|
160 |
-
</span>
|
161 |
-
</div>
|
162 |
</div>
|
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 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
// ex = [label, message, bg_color, label_bg, logo, logo_color, style, link]
|
278 |
-
|
279 |
-
// Label
|
280 |
-
const labelBox = document.querySelector("#label-input textarea, #label-input input");
|
281 |
-
if (labelBox) {{
|
282 |
-
updateReactValue(labelBox, ex[0]);
|
283 |
-
}}
|
284 |
-
|
285 |
-
// Message
|
286 |
-
const msgBox = document.querySelector("#message-input textarea, #message-input input");
|
287 |
-
if (msgBox) {{
|
288 |
-
updateReactValue(msgBox, ex[1]);
|
289 |
-
}}
|
290 |
-
|
291 |
-
// Background Color
|
292 |
-
const newBg = convertToHexIfNeeded(ex[2]);
|
293 |
-
setColorValue("color-input", newBg);
|
294 |
-
|
295 |
-
// Label Background Color
|
296 |
-
const newLabel = convertToHexIfNeeded(ex[3]);
|
297 |
-
setColorValue("label-color-input", newLabel);
|
298 |
-
|
299 |
-
// Logo
|
300 |
-
const logoBox = document.querySelector("#logo-input textarea, #logo-input input");
|
301 |
-
if (logoBox) {{
|
302 |
-
updateReactValue(logoBox, ex[4]);
|
303 |
-
}}
|
304 |
-
|
305 |
-
// Logo Color
|
306 |
-
const newLogoColor = convertToHexIfNeeded(ex[5]);
|
307 |
-
setColorValue("logo-color-input", newLogoColor);
|
308 |
-
|
309 |
-
// Style (Dropdown)
|
310 |
-
const styleSelect = document.querySelector("#style-input select");
|
311 |
-
if (styleSelect) {{
|
312 |
-
styleSelect.value = ex[6];
|
313 |
-
styleSelect.dispatchEvent(new Event("change", {{ bubbles: true }}));
|
314 |
-
}}
|
315 |
-
|
316 |
-
// Link
|
317 |
-
const linkBox = document.querySelector("#link-input textarea, #link-input input");
|
318 |
-
if (linkBox) {{
|
319 |
-
updateReactValue(linkBox, ex[7]);
|
320 |
-
}}
|
321 |
-
}}
|
322 |
-
</script>
|
323 |
-
"""
|
324 |
-
|
325 |
-
gr.HTML(html_items + apply_example_js)
|
326 |
|
327 |
-
|
328 |
-
demo.load(
|
329 |
-
fn=generate_static_badge,
|
330 |
-
inputs=[label, message, color, label_color, logo, logo_color, style, link],
|
331 |
-
outputs=[out_code, out_preview]
|
332 |
-
)
|
333 |
-
for inp in [label, message, color, label_color, logo, logo_color, style, link]:
|
334 |
-
inp.change(
|
335 |
-
fn=generate_static_badge,
|
336 |
-
inputs=[label, message, color, label_color, logo, logo_color, style, link],
|
337 |
-
outputs=[out_code, out_preview]
|
338 |
-
)
|
339 |
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
<h4 style="color: #457b9d; margin-bottom: 8px;">✨ What are Badges?</h4>
|
345 |
-
<p>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.</p>
|
346 |
-
<h4 style="color: #457b9d; margin-bottom: 8px;">🛠️ Basic Settings</h4>
|
347 |
-
<ul>
|
348 |
-
<li><strong>Label</strong>: Text displayed on the left side of the badge (e.g., "Discord", "Version", "Status")</li>
|
349 |
-
<li><strong>Message</strong>: Text displayed on the right side of the badge</li>
|
350 |
-
<li><strong>Logo</strong>: Name of a logo provided by Simple Icons (<a href="https://simpleicons.org/" target="_blank">View List</a>)</li>
|
351 |
-
<li><strong>Style</strong>: Determines the shape of the badge (flat, plastic, for-the-badge, etc.)</li>
|
352 |
-
</ul>
|
353 |
-
<h4 style="color: #457b9d; margin-bottom: 8px;">🎨 ColorSettings</h4>
|
354 |
-
<ul>
|
355 |
-
<li><strong>Background Color</strong>: Background color for the right side of the badge</li>
|
356 |
-
<li><strong>Label Background Color</strong>: Background color for the left side of the badge</li>
|
357 |
-
<li><strong>Logo Color</strong>: Color of the logo (e.g. white or black)</li>
|
358 |
-
</ul>
|
359 |
-
<h4 style="color: #457b9d; margin-bottom: 8px;">🔗 Using the HTML</h4>
|
360 |
-
<p>Copy the generated HTML code and paste it into your website, blog, GitHub README, etc.</p>
|
361 |
-
<p>HTML works in GitHub READMEs, but if you prefer markdown, use <code></code>.</p>
|
362 |
-
<h4 style="color: #457b9d; margin-bottom: 8px;">💡 Tips</h4>
|
363 |
-
<ul>
|
364 |
-
<li>Click on any example in the grid to automatically fill in all settings</li>
|
365 |
-
<li>The preview updates in real-time as you make changes</li>
|
366 |
-
<li>You can use over 2000+ logos from Simple Icons - just enter the name</li>
|
367 |
-
<li>Custom colors can be selected with the color picker or by entering a hex code</li>
|
368 |
-
</ul>
|
369 |
-
</div>
|
370 |
-
''')
|
371 |
|
372 |
-
#
|
373 |
-
gr.HTML(''
|
374 |
-
|
375 |
-
|
376 |
-
<a href="https://discord.gg/openfreeai" target="_blank" style="color:#5e60ce;">Discord</a>
|
377 |
-
</p>
|
378 |
-
</div>
|
379 |
-
''')
|
380 |
|
381 |
if __name__ == "__main__":
|
382 |
demo.launch()
|
|
|
4 |
# ---------------------------
|
5 |
# Badge URL generation
|
6 |
# ---------------------------
|
7 |
+
def generate_static_badge(label, message, color, label_color,
|
8 |
+
logo, logo_color, style, link):
|
9 |
base = "https://img.shields.io/static/v1"
|
10 |
params = []
|
11 |
+
if label: params.append(f"label={urllib.parse.quote(label, safe='')}")
|
12 |
+
if message: params.append(f"message={urllib.parse.quote(message, safe='')}")
|
13 |
+
if color: params.append(f"color={urllib.parse.quote(color, safe='')}")
|
14 |
+
if label_color: params.append(f"labelColor={urllib.parse.quote(label_color, safe='')}")
|
15 |
+
if logo: params.append(f"logo={urllib.parse.quote(logo, safe='')}")
|
16 |
+
if logo_color: params.append(f"logoColor={urllib.parse.quote(logo_color, safe='')}")
|
17 |
+
if style: params.append(f"style={urllib.parse.quote(style, safe='')}")
|
18 |
+
|
19 |
+
badge_url = base + "?" + "&".join(params)
|
20 |
+
img = f'<img src="{badge_url}" alt="badge">'
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
if link:
|
22 |
+
img = f'<a href="{link}" target="_blank">{img}</a>'
|
23 |
+
|
24 |
+
preview = (f"<div style='padding:30px;background:linear-gradient(135deg,#f8f9fa,#e9ecef);"
|
25 |
+
f"border-radius:16px;display:flex;justify-content:center;box-shadow:0 6px 12px"
|
26 |
+
f" rgba(0,0,0,0.05);'>{img}</div>")
|
27 |
+
return img, preview
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
# ---------------------------
|
31 |
# Gradio UI
|
32 |
# ---------------------------
|
33 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
34 |
+
# ------------------ CSS & Header ------------------
|
35 |
gr.HTML("""
|
36 |
<style>
|
37 |
+
body{background:linear-gradient(120deg,#f8f9fa,#e2eafc);font-family:'Poppins','Noto Sans KR',sans-serif;}
|
38 |
+
.gradio-container{background:rgba(255,255,255,0.85);backdrop-filter:blur(10px);
|
39 |
+
border-radius:20px;padding:24px;box-shadow:0 10px 30px rgba(0,0,0,0.08);max-width:1000px;margin:0 auto;}
|
40 |
+
.gr-button{background:linear-gradient(135deg,#a8dadc,#88c1e9)!important;color:#1d3557!important;
|
41 |
+
border:none!important;border-radius:10px!important;font-weight:600!important;transition:all .3s;
|
42 |
+
box-shadow:0 4px 10px rgba(138,198,209,.3)!important;}
|
43 |
+
.gr-button:hover{transform:translateY(-2px)!important;box-shadow:0 6px 15px rgba(138,198,209,.4)!important;}
|
44 |
+
.gr-textbox,.gr-select,.gr-color{background:#e8f6f3!important;border:2px solid #d9f0ea!important;
|
45 |
+
border-radius:12px!important;transition:all .3s;}
|
46 |
+
.gr-textbox:focus,.gr-select:focus,.gr-color:focus{
|
47 |
+
border-color:#a8dadc!important;box-shadow:0 0 0 3px rgba(168,218,220,.25)!important;}
|
48 |
+
.example-grid{display:grid;grid-template-columns:repeat(4,1fr);gap:16px;margin-top:20px;}
|
49 |
+
.example-item{background:linear-gradient(135deg,#f1f8ff,#e8f4ff);border-radius:12px;padding:16px;
|
50 |
+
text-align:center;cursor:pointer;transition:all .3s;border:2px solid transparent;}
|
51 |
+
.example-item:hover{transform:translateY(-3px);box-shadow:0 8px 15px rgba(0,0,0,0.05);border-color:#a8dadc;}
|
52 |
+
@media(max-width:768px){.example-grid{grid-template-columns:repeat(2,1fr);} }
|
53 |
+
@media(max-width:600px){.example-grid{grid-template-columns:1fr;} }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
</style>
|
55 |
+
<div style="text-align:center;margin-bottom:24px;">
|
56 |
+
<h1 style="font-size:2.8rem;margin-bottom:.2em;background:linear-gradient(90deg,#5e60ce,#64dfdf);
|
57 |
+
-webkit-background-clip:text;-webkit-text-fill-color:transparent;">🎨 BadgeCraft</h1>
|
58 |
+
<p style="font-size:1.2rem;color:#457b9d;">Create beautiful badges with live preview and HTML snippet</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
</div>
|
60 |
""")
|
61 |
|
62 |
+
# ------------------ Components ------------------
|
63 |
+
label = gr.Textbox("Discord", label="Label", elem_id="label-input")
|
64 |
+
message = gr.Textbox("Join our community", label="Message", elem_id="message-input")
|
65 |
+
color = gr.ColorPicker("#5865F2", label="Background Color", elem_id="color-input")
|
66 |
+
label_color = gr.ColorPicker("#99AAFF", label="Label Background Color", elem_id="label-color-input")
|
67 |
+
logo = gr.Textbox("discord", label="Logo", elem_id="logo-input")
|
68 |
+
logo_color = gr.ColorPicker("#ffffff", label="Logo Color", elem_id="logo-color-input")
|
69 |
+
style = gr.Dropdown(["flat","flat-square","plastic","for-the-badge","social"],
|
70 |
+
value="for-the-badge", label="Style", elem_id="style-input")
|
71 |
+
link = gr.Textbox("https://discord.gg/openfreeai", label="Link (URL)", elem_id="link-input")
|
72 |
+
|
73 |
+
out_code = gr.Code(language="html", lines=3, label="HTML Snippet")
|
74 |
+
out_prev = gr.HTML()
|
75 |
+
|
76 |
+
# ------------------ Example badges ------------------
|
77 |
+
examples = [
|
78 |
+
["Discord","Openfree AI","#5865F2","#99AAFF","discord","white","for-the-badge",
|
79 |
+
"https://discord.gg/openfreeai"],
|
80 |
+
["X.com","Follow us","#1DA1F2","#00CFFF","x","white","for-the-badge",
|
81 |
+
"https://x.com/openfree_ai"],
|
82 |
+
["Collections","Explore","#FFB300","#FFF176","huggingface","black","for-the-badge",
|
83 |
+
"https://huggingface.co/collections/VIDraft/best-open-ai-services-68057e6e312880ea92abaf4c"],
|
84 |
+
["GitHub","Star us","#0A0A0A","#39FF14","github","white","for-the-badge",
|
85 |
+
"https://github.com/openfreeai"],
|
86 |
+
["YouTube","Watch now","#E50000","#FF5E5E","youtube","white","for-the-badge",
|
87 |
+
"https://www.youtube.com/@AITechTree"],
|
88 |
+
["Facebook","Like us","#1877F2","#6FAFFF","facebook","white","for-the-badge",
|
89 |
+
"https://www.facebook.com/profile.php?id=61575353674679"],
|
90 |
+
["Instagram","友情 萬世","#E4405F","#FF77A9","instagram","white","for-the-badge",
|
91 |
+
"https://www.instagram.com/openfree_ai/"],
|
92 |
+
["Threads","함께 즐겨요.","#000000","#FF00FF","threads","white","for-the-badge",
|
93 |
+
"https://www.threads.net/@openfree_ai"]
|
94 |
+
]
|
95 |
+
|
96 |
+
# grid html
|
97 |
+
grid = '<div class="example-grid">'
|
98 |
+
for i,e in enumerate(examples):
|
99 |
+
lbl,msg,bg,lbg,logo_e,logoc,sty,_ = e
|
100 |
+
def fix(c):
|
101 |
+
return "#ffffff" if c=="white" else "#000000" if c=="black" else c
|
102 |
+
url = ("https://img.shields.io/static/v1?"
|
103 |
+
f"label={urllib.parse.quote(lbl)}&message={urllib.parse.quote(msg)}&"
|
104 |
+
f"color={urllib.parse.quote(fix(bg))}&labelColor={urllib.parse.quote(fix(lbg))}&"
|
105 |
+
f"logo={urllib.parse.quote(logo_e)}&logoColor={urllib.parse.quote(fix(logoc))}&"
|
106 |
+
f"style={urllib.parse.quote(sty)}")
|
107 |
+
grid += (f'<div class="example-item" onclick="applyExample({i})">'
|
108 |
+
f'<img src="{url}" style="margin-bottom:8px;"><br>'
|
109 |
+
f'<span style="font-size:.9rem;color:#457b9d;">{lbl}</span></div>')
|
110 |
+
grid += "</div>"
|
111 |
+
|
112 |
+
# ------------------ JS (fixed color propagation) ------------------
|
113 |
+
js = f"""
|
114 |
+
<script>
|
115 |
+
const examples = {examples};
|
116 |
+
|
117 |
+
/* -------- helpers -------- */
|
118 |
+
function reactSet(el,val){{
|
119 |
+
if(!el) return;
|
120 |
+
const proto = (el.tagName==="TEXTAREA")
|
121 |
+
? HTMLTextAreaElement.prototype
|
122 |
+
: HTMLInputElement.prototype;
|
123 |
+
const set = Object.getOwnPropertyDescriptor(proto,"value").set;
|
124 |
+
set.call(el,val);
|
125 |
+
el.dispatchEvent(new Event("input",{{bubbles:true}}));
|
126 |
+
el.dispatchEvent(new Event("change",{{bubbles:true}})); // ColorPicker는 change에 반응
|
127 |
+
el.dispatchEvent(new Event("blur", {{bubbles:true}})); // 최종 확정
|
128 |
+
}}
|
129 |
+
function toHex(c){{
|
130 |
+
if(!c) return "#000000";
|
131 |
+
c = c.toLowerCase();
|
132 |
+
if(c==="white") return "#ffffff";
|
133 |
+
if(c==="black") return "#000000";
|
134 |
+
return c;
|
135 |
+
}}
|
136 |
+
function setColor(elemId,val){{
|
137 |
+
const root = document.getElementById(elemId);
|
138 |
+
if(!root) return;
|
139 |
+
// gr.ColorPicker 내부 input 은 type=text 이므로 먼저 찾음
|
140 |
+
let inp = root.querySelector("input[type='text'],input[type='color']");
|
141 |
+
if(!inp) return;
|
142 |
+
reactSet(inp,val);
|
143 |
+
// gradio 4.17 이후 : data-testid=color-picker 값 동기화 필요
|
144 |
+
const pickerDiv = root.querySelector("[data-testid='color-picker']")
|
145 |
+
if(pickerDiv) pickerDiv.style.background = val; // 버튼 시각적 즉시 변경
|
146 |
+
}}
|
147 |
+
|
148 |
+
/* -------- main: example click -------- */
|
149 |
+
window.applyExample = (i) => {{
|
150 |
+
const ex = examples[i];
|
151 |
+
// 0,label
|
152 |
+
reactSet(document.querySelector("#label-input input,#label-input textarea"), ex[0]);
|
153 |
+
// 1,message
|
154 |
+
reactSet(document.querySelector("#message-input input,#message-input textarea"), ex[1]);
|
155 |
+
// 2,bg color
|
156 |
+
setColor("color-input", toHex(ex[2]));
|
157 |
+
// 3,label bg
|
158 |
+
setColor("label-color-input", toHex(ex[3]));
|
159 |
+
// 4,logo
|
160 |
+
reactSet(document.querySelector("#logo-input input,#logo-input textarea"), ex[4]);
|
161 |
+
// 5,logo color
|
162 |
+
setColor("logo-color-input", toHex(ex[5]));
|
163 |
+
// 6,style
|
164 |
+
const sel = document.querySelector("#style-input select");
|
165 |
+
if(sel){{
|
166 |
+
sel.value = ex[6];
|
167 |
+
sel.dispatchEvent(new Event("change",{{bubbles:true}}));
|
168 |
+
}}
|
169 |
+
// 7,link
|
170 |
+
reactSet(document.querySelector("#link-input input,#link-input textarea"), ex[7]);
|
171 |
+
}}
|
172 |
+
</script>
|
173 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
|
175 |
+
gr.HTML(grid + js)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
|
177 |
+
# ------------------ live update handlers ------------------
|
178 |
+
comps = [label,message,color,label_color,logo,logo_color,style,link]
|
179 |
+
for c in comps:
|
180 |
+
c.change(generate_static_badge, comps, [out_code,out_prev])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
181 |
|
182 |
+
# ------------------ footer ------------------
|
183 |
+
gr.HTML('<p style="text-align:center;margin-top:30px;font-size:.9rem;color:#6d6875">'
|
184 |
+
'© 2023‑2025 BadgeCraft | MIT License '
|
185 |
+
'<a href="https://discord.gg/openfreeai" style="color:#5e60ce" target="_blank">Discord</a></p>')
|
|
|
|
|
|
|
|
|
186 |
|
187 |
if __name__ == "__main__":
|
188 |
demo.launch()
|