File size: 4,746 Bytes
2a33559
 
 
6cd90ae
 
 
26d8ca3
 
 
 
 
 
 
6cd90ae
 
 
6da718d
6cd90ae
6da718d
6cd90ae
 
 
 
 
 
 
 
 
 
 
55efc3b
 
7c1249e
55efc3b
 
b78a8f5
55efc3b
 
b78a8f5
55efc3b
b78a8f5
55efc3b
 
b78a8f5
 
55efc3b
86113c7
55efc3b
6cd90ae
55efc3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26d8ca3
55efc3b
 
 
 
 
 
 
 
 
26d8ca3
55efc3b
 
 
 
 
 
 
 
5f54590
6cd90ae
55efc3b
 
6cd90ae
55efc3b
 
 
 
 
 
 
 
 
 
 
26d8ca3
55efc3b
 
7c1249e
b78a8f5
26d8ca3
b78a8f5
55efc3b
fea25ac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import os
os.system("pip install gradio==3.46.0")

import gradio as gr
import json
import pandas as pd
from reportlab.lib import colors
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.platypus import Table, TableStyle, Image, SimpleDocTemplate, PageBreak
from reportlab.lib import colors
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import cm

with open('pokemon.json', 'r') as f:
    pokemons = json.load(f)
pokemons_types = ["λͺ¨λ“  νƒ€μž…"] + sorted(set([t for poke in pokemons for t in poke['types']]))
GEN_RANGE = {
    "λͺ¨λ“  μ„ΈλŒ€": [1, 1017],
    "1μ„ΈλŒ€": [1, 151],
    "2μ„ΈλŒ€": [152, 251],
    "3μ„ΈλŒ€": [252, 386],
    "4μ„ΈλŒ€": [387, 493],
    "5μ„ΈλŒ€": [494, 649],
    "6μ„ΈλŒ€": [650, 721],
    "7μ„ΈλŒ€": [722, 809],
    "8μ„ΈλŒ€": [810, 905],
    "9μ„ΈλŒ€": [906, 1017]
}

with gr.Blocks(title="λŒ€μΉ˜λ™ 포켓λͺ¬ 도감 생성기 πŸ™") as block:
    with gr.Row():
        gr.Markdown("# λŒ€μΉ˜λ™ 포켓λͺ¬ 도감 생성기 πŸ™\n- 포켓λͺ¬μ˜ μ„ΈλŒ€λ³„, νƒ€μž…λ³„λ‘œ 도감을 λ§Œλ“€μ–΄μ£ΌλŠ” κ³³μž…λ‹ˆλ‹€.\n- 자료의 μΆœμ²˜λŠ” http://pokemon.fandom.com μž…λ‹ˆλ‹€.")
    with gr.Row():
        generation = gr.Dropdown(
                    [f"{k}μ„ΈλŒ€" for k in range(1, 10)] + ["λͺ¨λ“  μ„ΈλŒ€"], value="1μ„ΈλŒ€", label="πŸŒˆν¬μΌ“λͺ¬ μ„ΈλŒ€", info="μ›ν•˜λŠ” 포켓λͺ¬ μ„ΈλŒ€λ₯Ό μ„ νƒν•˜μ„Έμš”."
                )
        poke_types = gr.Dropdown(
                    pokemons_types, value="λͺ¨λ“  νƒ€μž…", label="πŸ©»ν¬μΌ“λͺ¬ νƒ€μž…", info="μ›ν•˜λŠ” 포켓λͺ¬ νƒ€μž…μ„ μ„ νƒν•˜μ„Έμš”."
                )
        button = gr.Button(value="🎩 도감 생성")
    with gr.Row():
        with gr.Column():
            download = gr.File(label="πŸ”‘νŒŒλž€ 글씨λ₯Ό λˆŒλŸ¬μ„œ λ‹€μš΄λ‘œλ“œ λ°›μœΌμ„Έμš”.")
            df_view = gr.DataFrame(label="πŸ“ν¬μΌ“λͺ¬ 리슀트", wrap=True, row_count=10)
        with gr.Column():
            gallery = gr.Gallery(columns=3, label="πŸ‘©πŸ»β€πŸŽ¨ν¬μΌ“λͺ¬ 가러리", rows=4, height=600)
            report = gr.Markdown("## νƒ€μž…λ³„ 포켓λͺ¬ 뢄석")            

    def write_pdf(gen, poke_type):
        filename = f'포켓λͺ¬{gen}_{poke_type}.pdf'
        # PDF λ¬Έμ„œλ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.
        pdfmetrics.registerFont(TTFont("λ‚˜λˆ”κ³ λ”•", "NanumGothic.ttf"))
        doc = SimpleDocTemplate(filename, pagesize=A4)
        
        # ν…Œμ΄λΈ” μŠ€νƒ€μΌμ„ μ •μ˜ν•©λ‹ˆλ‹€.
        style = TableStyle([
            ('TEXTCOLOR', (0, 1), (-1, -1), colors.gray),
            ('ALIGN', (0, 0), (-1, -1), 'CENTER'),
            ('FONTNAME', (0, 0), (-1, -1), 'λ‚˜λˆ”κ³ λ”•'),
            ('SIZE', (0, 0), (-1, -1), 50),
            ('BACKGROUND', (0, 1), (-1, 1), colors.white),
            ('GRID', (0, 0), (-1, -1), 1, colors.black),
            ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
            ('LEADING', (0, 0), (-1, -1), 14),  # 라인 간격 μ‘°μ •
            ('BOTTOMPADDING', (0, 0), (-1, -1), 60),
        ])

        images = []
        story = []
        data_dict = []
        start, end = GEN_RANGE[gen]
        for k in range(start, end+1):
            name = pokemons[k-1]['name']
            number = pokemons[k-1]['number']
            types = pokemons[k-1]['types']
            image_path = pokemons[k-1]['image_path']

            if poke_type == "λͺ¨λ“  νƒ€μž…":
                condition = True
            else:
                condition = poke_type in types
            if condition:
                data_dict.append(
                    dict(이름=name, 번호=number, νƒ€μž…='+'.join(types))
                )
                images.append((image_path, f"{number} {name}({'+'.join(types)})"))

                # PDF에 이미지λ₯Ό μΆ”κ°€ν•©λ‹ˆλ‹€. 이미지 파일의 경둜λ₯Ό μ§€μ •ν•˜μ„Έμš”.
                image = Image(image_path, width=13.5*cm, height=13.5*cm)

                # ν…Œμ΄λΈ” 데이터λ₯Ό μ€€λΉ„ν•©λ‹ˆλ‹€.
                data = [
                    [number],
                    [image],  # 2번째 행에 이미지λ₯Ό μΆ”κ°€ν•  κ²ƒμž…λ‹ˆλ‹€.
                    ['+'.join(types)],
                    [name],
                ]
                # ν…Œμ΄λΈ” 객체λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.
                table = Table(data)
                table.setStyle(style)
                story.append(table)

        doc.build(story)
        df = pd.DataFrame(data_dict)
        analysis = df[['이름', 'νƒ€μž…']].groupby(by='νƒ€μž…').count().sort_values(by='이름', ascending=False).to_markdown().replace('이름', '개수')
        return df[['번호', '이름', 'νƒ€μž…']], filename, images, analysis

    button.click(write_pdf, [generation, poke_types], [df_view, download, gallery, report])
block.queue(concurrency_count=3)
block.launch()