Update app.py
Browse files
app.py
CHANGED
@@ -25,74 +25,83 @@ GEN_RANGE = {
|
|
25 |
"9์ธ๋": [906, 1017]
|
26 |
}
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
)
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
('TEXTCOLOR', (0, 1), (-1, -1), colors.gray),
|
48 |
-
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
|
49 |
-
('FONTNAME', (0, 0), (-1, -1), '๋๋๊ณ ๋'),
|
50 |
-
('SIZE', (0, 0), (-1, -1), 50),
|
51 |
-
('BACKGROUND', (0, 1), (-1, 1), colors.white),
|
52 |
-
('GRID', (0, 0), (-1, -1), 1, colors.black),
|
53 |
-
('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
|
54 |
-
('LEADING', (0, 0), (-1, -1), 14), # ๋ผ์ธ ๊ฐ๊ฒฉ ์กฐ์
|
55 |
-
('BOTTOMPADDING', (0, 0), (-1, -1), 60),
|
56 |
-
])
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
else:
|
70 |
-
condition = poke_type in types
|
71 |
-
if condition:
|
72 |
-
data_dict.append(
|
73 |
-
dict(์ด๋ฆ=name, No=number, ํ์
='+'.join(types))
|
74 |
-
)
|
75 |
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
[image], # 2๋ฒ์งธ ํ์ ์ด๋ฏธ์ง๋ฅผ ์ถ๊ฐํ ๊ฒ์
๋๋ค.
|
83 |
-
['+'.join(types)],
|
84 |
-
[name],
|
85 |
-
]
|
86 |
-
# ํ
์ด๋ธ ๊ฐ์ฒด๋ฅผ ์์ฑํฉ๋๋ค.
|
87 |
-
table = Table(data)
|
88 |
-
table.setStyle(style)
|
89 |
-
story.append(table)
|
90 |
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
demo = gr.Interface(write_pdf, [generation, poke_types], [text, download], title="๋์น๋ ํฌ์ผ๋ชฌ ๋๊ฐ ์์ฑ๊ธฐ",
|
96 |
-
description="์ํ๋ ํฌ์ผ๋ชฌ ์ธ๋๋ฅผ ์ ํํ๊ณ , ๋ค์ด๋ก๋๋ฅผ ๋๋ฌ์ฃผ์ธ์.")
|
97 |
-
demo.queue(concurrency_count=3)
|
98 |
-
demo.launch()
|
|
|
25 |
"9์ธ๋": [906, 1017]
|
26 |
}
|
27 |
|
28 |
+
with gr.Blocks(title="๋์น๋ ํฌ์ผ๋ชฌ ๋๊ฐ ์์ฑ๊ธฐ ๐") as block:
|
29 |
+
with gr.Row():
|
30 |
+
gr.Markdown("# ๋์น๋ ํฌ์ผ๋ชฌ ๋๊ฐ ์์ฑ๊ธฐ ๐")
|
31 |
+
with gr.Row():
|
32 |
+
generation = gr.Dropdown(
|
33 |
+
[f"{k}์ธ๋" for k in range(1, 10)] + ["๋ชจ๋ ์ธ๋"], value="1์ธ๋", label="ํฌ์ผ๋ชฌ ์ธ๋", info="์ํ๋ ํฌ์ผ๋ชฌ ์ธ๋๋ฅผ ์ ํํ์ธ์."
|
34 |
+
)
|
35 |
+
poke_types = gr.Dropdown(
|
36 |
+
pokemons_types, value="๋ชจ๋ ํ์
", label="ํฌ์ผ๋ชฌ ํ์
", info="์ํ๋ ํฌ์ผ๋ชฌ ํ์
์ ์ ํํ์ธ์."
|
37 |
+
)
|
38 |
+
button = gr.Button(label="๐ฉ ๋๊ฐ ์์ฑ")
|
39 |
+
with gr.Row():
|
40 |
+
with gr.Column():
|
41 |
+
download = gr.File(label="ํ๋ ๊ธ์จ๋ฅผ ๋๋ฌ์ ๋ค์ด๋ก๋ ๋ฐ์ผ์ธ์.")
|
42 |
+
df_view = gr.DataFrame(label="ํฌ์ผ๋ชฌ ๋ฆฌ์คํธ", wrap=True, row_count=10)
|
43 |
+
with gr.Column():
|
44 |
+
gallery = gr.Gallery(columns=4, label="ํฌ์ผ๋ชฌ ๊ฐค๋ฌ๋ฆฌ")
|
45 |
+
report = gr.Markdown("## ํ์
๋ณ ํฌ์ผ๋ชฌ ๋ถ์")
|
46 |
|
47 |
+
def write_pdf(gen, poke_type):
|
48 |
+
filename = f'ํฌ์ผ๋ชฌ{gen}_{poke_type}.pdf'
|
49 |
+
# PDF ๋ฌธ์๋ฅผ ์์ฑํฉ๋๋ค.
|
50 |
+
pdfmetrics.registerFont(TTFont("๋๋๊ณ ๋", "NanumGothic.ttf"))
|
51 |
+
doc = SimpleDocTemplate(filename, pagesize=A4)
|
52 |
+
|
53 |
+
# ํ
์ด๋ธ ์คํ์ผ์ ์ ์ํฉ๋๋ค.
|
54 |
+
style = TableStyle([
|
55 |
+
('TEXTCOLOR', (0, 1), (-1, -1), colors.gray),
|
56 |
+
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
|
57 |
+
('FONTNAME', (0, 0), (-1, -1), '๋๋๊ณ ๋'),
|
58 |
+
('SIZE', (0, 0), (-1, -1), 50),
|
59 |
+
('BACKGROUND', (0, 1), (-1, 1), colors.white),
|
60 |
+
('GRID', (0, 0), (-1, -1), 1, colors.black),
|
61 |
+
('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
|
62 |
+
('LEADING', (0, 0), (-1, -1), 14), # ๋ผ์ธ ๊ฐ๊ฒฉ ์กฐ์
|
63 |
+
('BOTTOMPADDING', (0, 0), (-1, -1), 60),
|
64 |
+
])
|
65 |
|
66 |
+
images = []
|
67 |
+
story = []
|
68 |
+
data_dict = []
|
69 |
+
start, end = GEN_RANGE[gen]
|
70 |
+
for k in range(start, end+1):
|
71 |
+
name = pokemons[k-1]['name']
|
72 |
+
number = pokemons[k-1]['number']
|
73 |
+
types = pokemons[k-1]['types']
|
74 |
+
image_path = pokemons[k-1]['image_path']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
+
if poke_type == "๋ชจ๋ ํ์
":
|
77 |
+
condition = True
|
78 |
+
else:
|
79 |
+
condition = poke_type in types
|
80 |
+
if condition:
|
81 |
+
data_dict.append(
|
82 |
+
dict(์ด๋ฆ=name, ๋ฒํธ=number, ํ์
='+'.join(types))
|
83 |
+
)
|
84 |
+
images.append((image_path, f"{number} {name}"))
|
85 |
|
86 |
+
# PDF์ ์ด๋ฏธ์ง๋ฅผ ์ถ๊ฐํฉ๋๋ค. ์ด๋ฏธ์ง ํ์ผ์ ๊ฒฝ๋ก๋ฅผ ์ง์ ํ์ธ์.
|
87 |
+
image = Image(image_path, width=13.5*cm, height=13.5*cm)
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
+
# ํ
์ด๋ธ ๋ฐ์ดํฐ๋ฅผ ์ค๋นํฉ๋๋ค.
|
90 |
+
data = [
|
91 |
+
[number],
|
92 |
+
[image], # 2๋ฒ์งธ ํ์ ์ด๋ฏธ์ง๋ฅผ ์ถ๊ฐํ ๊ฒ์
๋๋ค.
|
93 |
+
['+'.join(types)],
|
94 |
+
[name],
|
95 |
+
]
|
96 |
+
# ํ
์ด๋ธ ๊ฐ์ฒด๋ฅผ ์์ฑํฉ๋๋ค.
|
97 |
+
table = Table(data)
|
98 |
+
table.setStyle(style)
|
99 |
+
story.append(table)
|
100 |
|
101 |
+
doc.build(story)
|
102 |
+
df = pd.DataFrame(data_dict)
|
103 |
+
return df[['๋ฒํธ', '์ด๋ฆ', 'ํ์
']], filename, images
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
+
button.click(write_pdf, [generation, poke_types], [df_view, download, gallery])
|
106 |
+
block.queue(concurrency_count=3)
|
107 |
+
block.launch()
|
|
|
|
|
|
|
|
|
|