Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,13 @@
|
|
1 |
import gradio as gr
|
2 |
import json
|
3 |
-
import os
|
4 |
-
import docx
|
5 |
-
from docx.oxml.ns import qn
|
6 |
-
from docx import Document
|
7 |
-
from docx.shared import Inches, Pt, Cm, Mm, RGBColor
|
8 |
-
from docx.enum.table import WD_TABLE_ALIGNMENT
|
9 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
with open('pokemon.json', 'r') as f:
|
12 |
pokemons = json.load(f)
|
@@ -27,27 +28,30 @@ generation = gr.Dropdown(
|
|
27 |
[f"{k}์ธ๋" for k in range(1, 10)], value="1์ธ๋", label="ํฌ์ผ๋ชฌ ์ธ๋", info="์ํ๋ ํฌ์ผ๋ชฌ ์ธ๋๋ฅผ ์ ํํ์ธ์."
|
28 |
)
|
29 |
|
30 |
-
download = gr.File(label="
|
31 |
-
text = gr.DataFrame()
|
32 |
-
|
33 |
-
def
|
34 |
-
filename = f'ํฌ์ผ๋ชฌ{gen}.
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
#
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
51 |
data_dict = []
|
52 |
start, end = GEN_RANGE[gen]
|
53 |
for k in progress.tqdm(range(start, end+1)):
|
@@ -60,42 +64,26 @@ def write_docx(gen, progress=gr.Progress()):
|
|
60 |
dict(์ด๋ฆ=name, No=number, ํ์
='+'.join(types))
|
61 |
)
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
hdr_cells = table.rows[3].cells
|
82 |
-
hdr_cells[0].text = f"{name}"
|
83 |
-
hdr_cells[0].paragraphs[0].runs[0].font.size = Pt(70)
|
84 |
-
hdr_cells[0].paragraphs[0].runs[0].font.color.rgb = RGBColor(192, 192, 192)
|
85 |
-
hdr_cells[0].paragraphs[0].alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.CENTER
|
86 |
-
|
87 |
-
hdr_cells = table.rows[2].cells
|
88 |
-
hdr_cells[0].text = f"{'+'.join(types)}"
|
89 |
-
hdr_cells[0].paragraphs[0].runs[0].font.size = Pt(70)
|
90 |
-
hdr_cells[0].paragraphs[0].runs[0].font.color.rgb = RGBColor(192, 192, 192)
|
91 |
-
hdr_cells[0].paragraphs[0].alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.CENTER
|
92 |
-
|
93 |
-
document.add_page_break()
|
94 |
-
|
95 |
-
document.save(filename)
|
96 |
return df[['No', '์ด๋ฆ', 'ํ์
']], filename
|
97 |
|
98 |
-
demo = gr.Interface(
|
99 |
description="์ํ๋ ํฌ์ผ๋ชฌ ์ธ๋๋ฅผ ์ ํํ๊ณ , ๋ค์ด๋ก๋๋ฅผ ๋๋ฌ์ฃผ์ธ์.")
|
100 |
demo.queue(concurrency_count=3)
|
101 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import pandas as pd
|
4 |
+
from reportlab.lib import colors
|
5 |
+
from reportlab.pdfbase import pdfmetrics
|
6 |
+
from reportlab.pdfbase.ttfonts import TTFont
|
7 |
+
from reportlab.platypus import Table, TableStyle, Image, SimpleDocTemplate, PageBreak
|
8 |
+
from reportlab.lib import colors
|
9 |
+
from reportlab.lib.pagesizes import A4
|
10 |
+
from reportlab.lib.units import cm
|
11 |
|
12 |
with open('pokemon.json', 'r') as f:
|
13 |
pokemons = json.load(f)
|
|
|
28 |
[f"{k}์ธ๋" for k in range(1, 10)], value="1์ธ๋", label="ํฌ์ผ๋ชฌ ์ธ๋", info="์ํ๋ ํฌ์ผ๋ชฌ ์ธ๋๋ฅผ ์ ํํ์ธ์."
|
29 |
)
|
30 |
|
31 |
+
download = gr.File(label="ํ๋ ๊ธ์จ๋ฅผ ๋๋ฌ์ ๋ค์ด๋ก๋ ๋ฐ์ผ์ธ์.")
|
32 |
+
text = gr.DataFrame(label="ํฌ์ผ๋ชฌ ๋ฆฌ์คํธ")
|
33 |
+
|
34 |
+
def write_pdf(gen, progress=gr.Progress()):
|
35 |
+
filename = f'ํฌ์ผ๋ชฌ{gen}.pdf'
|
36 |
+
pdfmetrics.registerFont(TTFont("๋๋๊ณ ๋", "NanumGothic.ttf"))
|
37 |
+
# PDF ๋ฌธ์๋ฅผ ์์ฑํฉ๋๋ค.
|
38 |
+
pdfmetrics.registerFont(TTFont("๋๋๊ณ ๋", "NanumGothic.ttf"))
|
39 |
+
doc = SimpleDocTemplate(filename, pagesize=A4)
|
40 |
+
|
41 |
+
# ํ
์ด๋ธ ์คํ์ผ์ ์ ์ํฉ๋๋ค.
|
42 |
+
style = TableStyle([
|
43 |
+
('TEXTCOLOR', (0, 1), (-1, -1), colors.gray),
|
44 |
+
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
|
45 |
+
('FONTNAME', (0, 0), (-1, -1), '๋๋๊ณ ๋'),
|
46 |
+
('SIZE', (0, 0), (-1, -1), 50),
|
47 |
+
('BACKGROUND', (0, 1), (-1, 1), colors.white),
|
48 |
+
('GRID', (0, 0), (-1, -1), 1, colors.black),
|
49 |
+
('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
|
50 |
+
('LEADING', (0, 0), (-1, -1), 14), # ๋ผ์ธ ๊ฐ๊ฒฉ ์กฐ์
|
51 |
+
('BOTTOMPADDING', (0, 0), (-1, -1), 60),
|
52 |
+
])
|
53 |
+
|
54 |
+
story = []
|
55 |
data_dict = []
|
56 |
start, end = GEN_RANGE[gen]
|
57 |
for k in progress.tqdm(range(start, end+1)):
|
|
|
64 |
dict(์ด๋ฆ=name, No=number, ํ์
='+'.join(types))
|
65 |
)
|
66 |
|
67 |
+
# PDF์ ์ด๋ฏธ์ง๋ฅผ ์ถ๊ฐํฉ๋๋ค. ์ด๋ฏธ์ง ํ์ผ์ ๊ฒฝ๋ก๋ฅผ ์ง์ ํ์ธ์.
|
68 |
+
image = Image(image_path, width=13.5*cm, height=13.5*cm)
|
69 |
+
|
70 |
+
# ํ
์ด๋ธ ๋ฐ์ดํฐ๋ฅผ ์ค๋นํฉ๋๋ค.
|
71 |
+
data = [
|
72 |
+
[number],
|
73 |
+
[image], # 2๋ฒ์งธ ํ์ ์ด๋ฏธ์ง๋ฅผ ์ถ๊ฐํ ๊ฒ์
๋๋ค.
|
74 |
+
['+'.join(types)],
|
75 |
+
[name],
|
76 |
+
]
|
77 |
+
# ํ
์ด๋ธ ๊ฐ์ฒด๋ฅผ ์์ฑํฉ๋๋ค.
|
78 |
+
table = Table(data)
|
79 |
+
table.setStyle(style)
|
80 |
+
story.append(table)
|
81 |
+
|
82 |
+
doc.build(story)
|
83 |
+
df = pd.DataFrame(data_dict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
return df[['No', '์ด๋ฆ', 'ํ์
']], filename
|
85 |
|
86 |
+
demo = gr.Interface(write_pdf, generation, [text, download], title="๋์น๋ ํฌ์ผ๋ชฌ ๋๊ฐ ์์ฑ๊ธฐ",
|
87 |
description="์ํ๋ ํฌ์ผ๋ชฌ ์ธ๋๋ฅผ ์ ํํ๊ณ , ๋ค์ด๋ก๋๋ฅผ ๋๋ฌ์ฃผ์ธ์.")
|
88 |
demo.queue(concurrency_count=3)
|
89 |
demo.launch()
|