fantos commited on
Commit
d44a7aa
·
verified ·
1 Parent(s): b1b6dc1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -190
app.py CHANGED
@@ -1,13 +1,10 @@
1
  import urllib.parse
2
  import gradio as gr
3
 
4
- # ----------------------------------------------------
5
- # 1. 배지 URL 생성 함수 정의
6
- # ----------------------------------------------------
7
  def generate_static_badge(label, message, color, style, label_color, logo, logo_color):
8
  base = "https://img.shields.io/static/v1"
9
  params = []
10
- if label: # Label이 빈 문자열이 아닌 경우에만 추가
11
  params.append(f"label={urllib.parse.quote(label, safe='')}")
12
  if message:
13
  params.append(f"message={urllib.parse.quote(message, safe='')}")
@@ -21,195 +18,43 @@ def generate_static_badge(label, message, color, style, label_color, logo, logo_
21
  params.append(f"logo={urllib.parse.quote(logo, safe='')}")
22
  if logo_color:
23
  params.append(f"logoColor={urllib.parse.quote(logo_color, safe='')}")
24
- url = base + ("?" + "&".join(params) if params else "")
25
- html_code = f'<img src="{url}" alt="{label or message} badge">'
26
- return html_code, url # (HTML 코드 스니펫, 이미지 URL)을 반환
27
 
28
- def generate_dynamic_json_badge(json_url, json_path, label, prefix, suffix, color, style, label_color, logo, logo_color):
29
- base = "https://img.shields.io/badge/dynamic/json"
30
- params = []
31
- if json_url:
32
- params.append(f"url={urllib.parse.quote(json_url, safe='')}")
33
- if json_path:
34
- params.append(f"query={urllib.parse.quote(json_path, safe='')}")
35
- if label:
36
- params.append(f"label={urllib.parse.quote(label, safe='')}")
37
- if prefix:
38
- params.append(f"prefix={urllib.parse.quote(prefix, safe='')}")
39
- if suffix:
40
- params.append(f"suffix={urllib.parse.quote(suffix, safe='')}")
41
- if color:
42
- params.append(f"color={urllib.parse.quote(color, safe='')}")
43
- if style:
44
- params.append(f"style={urllib.parse.quote(style, safe='')}")
45
- if label_color:
46
- params.append(f"labelColor={urllib.parse.quote(label_color, safe='')}")
47
- if logo:
48
- params.append(f"logo={urllib.parse.quote(logo, safe='')}")
49
- if logo_color:
50
- params.append(f"logoColor={urllib.parse.quote(logo_color, safe='')}")
51
  url = base + ("?" + "&".join(params) if params else "")
52
- html_code = f'<img src="{url}" alt="Dynamic JSON badge">'
53
- return html_code, url
54
-
55
- def generate_endpoint_badge(endpoint_url, label, color, style, label_color, logo, logo_color):
56
- base = "https://img.shields.io/endpoint"
57
- params = []
58
- if endpoint_url:
59
- params.append(f"url={urllib.parse.quote(endpoint_url, safe='')}")
60
- if label:
61
- params.append(f"label={urllib.parse.quote(label, safe='')}")
62
- if color:
63
- params.append(f"color={urllib.parse.quote(color, safe='')}")
64
- if style:
65
- params.append(f"style={urllib.parse.quote(style, safe='')}")
66
- if label_color:
67
- params.append(f"labelColor={urllib.parse.quote(label_color, safe='')}")
68
- if logo:
69
- params.append(f"logo={urllib.parse.quote(logo, safe='')}")
70
- if logo_color:
71
- params.append(f"logoColor={urllib.parse.quote(logo_color, safe='')}")
72
- url = base + ("?" + "&".join(params) if params else "")
73
- html_code = f'<img src="{url}" alt="Endpoint badge">'
74
- return html_code, url
75
-
76
- # ----------------------------------------------------
77
- # 2. Gradio UI 구성
78
- # ----------------------------------------------------
79
- with gr.Blocks(theme=gr.themes.Default()) as app:
80
- gr.Markdown("""
81
- # Shields.io Badge Generator 🛠
82
- 이 앱은 [Shields.io](https://shields.io/)에서 제공하는 배지를 **GUI**로 간편하게 생성하도록 도와줍니다.<br>
83
- 아래 탭에서 배지 유형을 선택하고 원하는 옵션을 입력해보세요. 자동으로 HTML 코드 스니펫과 미리보기 이미지를 확인하실 수 있습니다.
84
- """)
85
-
86
- with gr.Tabs():
87
- # ------------------------------------------------
88
- # --- Tab 1: Static Badge ---
89
- # ------------------------------------------------
90
- with gr.Tab("Static Badge"):
91
- gr.Markdown("""
92
- **정적(Static) 배지**를 생성합니다.
93
- 예: 좌측 텍스트(label), 우측 텍스트(message), 색상(color) 등을 입력하세요.
94
- """)
95
- with gr.Row():
96
- lbl = gr.Textbox(label="Label (좌측 텍스트)", placeholder="예: build")
97
- msg = gr.Textbox(label="Message (우측 텍스트)", placeholder="예: passing")
98
-
99
- with gr.Row():
100
- col = gr.Textbox(label="Color (색상)", value="blue", placeholder="예: brightgreen, #4c1 등")
101
- lbl_col = gr.Textbox(label="Label Color (레이블 배경색)", placeholder="(선택 사항)")
102
-
103
- with gr.Row():
104
- logo_in = gr.Textbox(label="Logo (아이콘)", placeholder="예: github (선택 사항)")
105
- logo_col = gr.Textbox(label="Logo Color (아이콘 색상)", placeholder="(선택 사항)")
106
-
107
- style_in = gr.Dropdown(
108
- label="Style (스타일)",
109
- choices=["flat", "flat-square", "plastic", "for-the-badge", "social"],
110
- value="flat"
111
- )
112
-
113
- # 출력: 코드 스니펫 & 이미지 미리보기
114
- out_code = gr.Code(label="HTML Snippet", language="html")
115
- out_img = gr.Image(label="Badge Preview", type="auto")
116
-
117
- # 입력 변화 -> 출력 갱신
118
- inputs = [lbl, msg, col, style_in, lbl_col, logo_in, logo_col]
119
- for inp in inputs:
120
- inp.change(
121
- fn=generate_static_badge,
122
- inputs=inputs,
123
- outputs=[out_code, out_img]
124
- )
125
-
126
- # ------------------------------------------------
127
- # --- Tab 2: Dynamic JSON Badge ---
128
- # ------------------------------------------------
129
- with gr.Tab("Dynamic JSON Badge"):
130
- gr.Markdown("""
131
- **동적(JSON) 배지**를 생성합니다.
132
- URL로부터 JSON 데이터를 읽어와 특정 필드를 추출하여 표시합니다.
133
- """)
134
- with gr.Row():
135
- json_url = gr.Textbox(label="JSON URL", placeholder="예: https://example.com/data.json")
136
- json_path = gr.Textbox(label="JSONPath Query", placeholder="예: $.version")
137
-
138
- with gr.Row():
139
- label_dyn = gr.Textbox(label="Label (좌측 텍스트)", placeholder="(선택 사항)")
140
- prefix_dyn = gr.Textbox(label="Prefix (접두사)", placeholder="값 앞에 붙일 문자열 (선택)")
141
- suffix_dyn = gr.Textbox(label="Suffix (접미사)", placeholder="값 뒤에 붙일 문자열 (선택)")
142
-
143
- with gr.Row():
144
- color_dyn = gr.Textbox(label="Color (색상)", value="blue", placeholder="예: blue, #4183c4 등")
145
- lbl_color_dyn = gr.Textbox(label="Label Color (레이블 배경색)", placeholder="(선택 사항)")
146
-
147
- with gr.Row():
148
- logo_dyn = gr.Textbox(label="Logo (아이콘)", placeholder="예: google (선택 사항)")
149
- logo_color_dyn = gr.Textbox(label="Logo Color (아이콘 색상)", placeholder="(선택 사항)")
150
-
151
- style_dyn = gr.Dropdown(
152
- label="Style (스타일)",
153
- choices=["flat", "flat-square", "plastic", "for-the-badge", "social"],
154
- value="flat"
155
- )
156
-
157
- out_code2 = gr.Code(label="HTML Snippet", language="html")
158
- out_img2 = gr.Image(label="Badge Preview", type="auto")
159
 
160
- inputs_dyn = [
161
- json_url, json_path, label_dyn, prefix_dyn, suffix_dyn,
162
- color_dyn, style_dyn, lbl_color_dyn, logo_dyn, logo_color_dyn
163
- ]
164
- for inp in inputs_dyn:
165
- inp.change(
166
- fn=generate_dynamic_json_badge,
167
- inputs=inputs_dyn,
168
- outputs=[out_code2, out_img2]
169
- )
170
-
171
- # ------------------------------------------------
172
- # --- Tab 3: Endpoint Badge ---
173
- # ------------------------------------------------
174
- with gr.Tab("Endpoint Badge"):
175
- gr.Markdown("""
176
- **Endpoint 배지**를 생성합니다.
177
- 엔드포인트(Endpoint)에서 미리 정의된 JSON 구조(`schemaVersion`, `label`, `message`, `color` 등)를 반환하고,
178
- Shields.io가 이를 읽어와 배지를 렌더링합니다.
179
- """)
180
- with gr.Row():
181
- endpoint = gr.Textbox(label="Endpoint URL", placeholder="배지 JSON을 제공하는 엔드포인트 URL")
182
- label_ep = gr.Textbox(label="Override Label", placeholder="엔드포인트 JSON의 label 대신 (선택)")
183
-
184
- with gr.Row():
185
- color_ep = gr.Textbox(label="Override Color", placeholder="엔드포인트 JSON의 color 대신 (선택)")
186
- lbl_color_ep = gr.Textbox(label="Label Color", placeholder="(선택 사항)")
187
-
188
- with gr.Row():
189
- logo_ep = gr.Textbox(label="Logo (아이콘)", placeholder="예: custom (선택 사항)")
190
- logo_color_ep = gr.Textbox(label="Logo Color", placeholder="(선택 사항)")
191
-
192
- style_ep = gr.Dropdown(
193
- label="Style (스타일)",
194
- choices=["flat", "flat-square", "plastic", "for-the-badge", "social"],
195
- value="flat"
196
- )
197
 
198
- out_code3 = gr.Code(label="HTML Snippet", language="html")
199
- out_img3 = gr.Image(label="Badge Preview", type="auto")
200
 
201
- inputs_ep = [
202
- endpoint, label_ep, color_ep, style_ep,
203
- lbl_color_ep, logo_ep, logo_color_ep
204
- ]
205
- for inp in inputs_ep:
206
- inp.change(
207
- fn=generate_endpoint_badge,
208
- inputs=inputs_ep,
209
- outputs=[out_code3, out_img3]
210
- )
211
 
212
- # 전체 Blocks 종료
213
-
214
- # 실제 실행 시 아래 주석을 해제하여 사용하세요.
215
- app.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import urllib.parse
2
  import gradio as gr
3
 
 
 
 
4
  def generate_static_badge(label, message, color, style, label_color, logo, logo_color):
5
  base = "https://img.shields.io/static/v1"
6
  params = []
7
+ if label:
8
  params.append(f"label={urllib.parse.quote(label, safe='')}")
9
  if message:
10
  params.append(f"message={urllib.parse.quote(message, safe='')}")
 
18
  params.append(f"logo={urllib.parse.quote(logo, safe='')}")
19
  if logo_color:
20
  params.append(f"logoColor={urllib.parse.quote(logo_color, safe='')}")
 
 
 
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  url = base + ("?" + "&".join(params) if params else "")
23
+ # HTML snippet
24
+ html_code = f'<img src="{url}" alt="{label or message} badge">'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
+ # HTML 컴포넌트로 그대로 보여줄 미리보기(크기 지정 가능)
27
+ preview_html = f"""
28
+ <div style="display:inline-block; padding:10px; border:1px solid #ccc;">
29
+ <img src="{url}" alt="Badge Preview">
30
+ </div>
31
+ """
32
+ return html_code, preview_html
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
 
 
34
 
35
+ with gr.Blocks() as demo:
36
+ gr.Markdown("## Shields.io Badge Generator 🛠")
 
 
 
 
 
 
 
 
37
 
38
+ with gr.Row():
39
+ lbl = gr.Textbox(label="Label")
40
+ msg = gr.Textbox(label="Message")
41
+ with gr.Row():
42
+ col = gr.Textbox(label="Color", value="blue")
43
+ lbl_col = gr.Textbox(label="Label Color")
44
+ with gr.Row():
45
+ logo_in = gr.Textbox(label="Logo")
46
+ logo_col = gr.Textbox(label="Logo Color")
47
+ style_in = gr.Dropdown(
48
+ label="Style",
49
+ choices=["flat", "flat-square", "plastic", "for-the-badge", "social"],
50
+ value="flat"
51
+ )
52
+
53
+ out_code = gr.Code(label="HTML Snippet", language="html")
54
+ out_html = gr.HTML(label="Preview")
55
+
56
+ inputs = [lbl, msg, col, style_in, lbl_col, logo_in, logo_col]
57
+ for inp in inputs:
58
+ inp.change(fn=generate_static_badge, inputs=inputs, outputs=[out_code, out_html])
59
+
60
+ demo.launch()