yoon-gu commited on
Commit
6da718d
โ€ข
1 Parent(s): 89c32d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -21
app.py CHANGED
@@ -11,8 +11,9 @@ from reportlab.lib.units import cm
11
 
12
  with open('pokemon.json', 'r') as f:
13
  pokemons = json.load(f)
14
-
15
  GEN_RANGE = {
 
16
  "1์„ธ๋Œ€": [1, 151],
17
  "2์„ธ๋Œ€": [152, 251],
18
  "3์„ธ๋Œ€": [252, 386],
@@ -25,14 +26,17 @@ GEN_RANGE = {
25
  }
26
 
27
  generation = gr.Dropdown(
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"))
@@ -60,30 +64,35 @@ def write_pdf(gen, progress=gr.Progress()):
60
  types = pokemons[k-1]['types']
61
  image_path = pokemons[k-1]['image_path']
62
 
63
- data_dict.append(
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()
 
11
 
12
  with open('pokemon.json', 'r') as f:
13
  pokemons = json.load(f)
14
+ pokemons_types = ["๋ชจ๋“  ํƒ€์ž…"] + sorted(set([t for poke in pokemons for t in poke['types']]))
15
  GEN_RANGE = {
16
+ "๋ชจ๋“  ์„ธ๋Œ€": [1, 1017],
17
  "1์„ธ๋Œ€": [1, 151],
18
  "2์„ธ๋Œ€": [152, 251],
19
  "3์„ธ๋Œ€": [252, 386],
 
26
  }
27
 
28
  generation = gr.Dropdown(
29
+ [f"{k}์„ธ๋Œ€" for k in range(1, 10)] + ["๋ชจ๋“  ์„ธ๋Œ€"], value="1์„ธ๋Œ€", label="ํฌ์ผ“๋ชฌ ์„ธ๋Œ€", info="์›ํ•˜๋Š” ํฌ์ผ“๋ชฌ ์„ธ๋Œ€๋ฅผ ์„ ํƒํ•˜์„ธ์š”."
30
+ )
31
+ poke_types = gr.Dropdown(
32
+ pokemons_types, value="๋ชจ๋“  ํƒ€์ž…", label="ํฌ์ผ“๋ชฌ ํƒ€์ž…", info="์›ํ•˜๋Š” ํฌ์ผ“๋ชฌ ํƒ€์ž…์„ ์„ ํƒํ•˜์„ธ์š”."
33
  )
34
 
35
  download = gr.File(label="ํŒŒ๋ž€ ๊ธ€์”จ๋ฅผ ๋ˆŒ๋Ÿฌ์„œ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์œผ์„ธ์š”.")
36
  text = gr.DataFrame(label="ํฌ์ผ“๋ชฌ ๋ฆฌ์ŠคํŠธ")
37
 
38
+ def write_pdf(gen, poke_type, progress=gr.Progress()):
39
+ filename = f'ํฌ์ผ“๋ชฌ{gen}_{poke_type}.pdf'
40
  pdfmetrics.registerFont(TTFont("๋‚˜๋ˆ”๊ณ ๋”•", "NanumGothic.ttf"))
41
  # PDF ๋ฌธ์„œ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
42
  pdfmetrics.registerFont(TTFont("๋‚˜๋ˆ”๊ณ ๋”•", "NanumGothic.ttf"))
 
64
  types = pokemons[k-1]['types']
65
  image_path = pokemons[k-1]['image_path']
66
 
67
+ if poke_type == "๋ชจ๋“  ํƒ€์ž…":
68
+ condition = True
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
+ # PDF์— ์ด๋ฏธ์ง€๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•˜์„ธ์š”.
77
+ image = Image(image_path, width=13.5*cm, height=13.5*cm)
78
 
79
+ # ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐ๋ฅผ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค.
80
+ data = [
81
+ [number],
82
+ [image], # 2๋ฒˆ์งธ ํ–‰์— ์ด๋ฏธ์ง€๋ฅผ ์ถ”๊ฐ€ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.
83
+ ['+'.join(types)],
84
+ [name],
85
+ ]
86
+ # ํ…Œ์ด๋ธ” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
87
+ table = Table(data)
88
+ table.setStyle(style)
89
+ story.append(table)
90
 
91
  doc.build(story)
92
  df = pd.DataFrame(data_dict)
93
  return df[['No', '์ด๋ฆ„', 'ํƒ€์ž…']], filename
94
 
95
+ demo = gr.Interface(write_pdf, [generation, poke_types], [text, download], title="๋Œ€์น˜๋™ ํฌ์ผ“๋ชฌ ๋„๊ฐ ์ƒ์„ฑ๊ธฐ",
96
  description="์›ํ•˜๋Š” ํฌ์ผ“๋ชฌ ์„ธ๋Œ€๋ฅผ ์„ ํƒํ•˜๊ณ , ๋‹ค์šด๋กœ๋“œ๋ฅผ ๋ˆŒ๋Ÿฌ์ฃผ์„ธ์š”.")
97
  demo.queue(concurrency_count=3)
98
  demo.launch()