/
app.py
145 lines (135 loc) · 4.34 KB
/
app.py
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import streamlit as st
from st_pages import show_pages_from_config
st.set_page_config(
page_title="포켓몬 도감",
page_icon="./images/monsterball.png"
)
show_pages_from_config()
st.markdown("""
<style>
img {
max-height: 300px;
}
.streamlit-expanderContent div {
display: flex;
justify-content: center;
font-size: 20px;
}
[data-testid="stExpanderToggleIcon"] {
visibility: hidden;
}
.streamlit-expanderHeader {
pointer-events: none;
}
[data-testid="StyledFullScreenButton"] {
visibility: hidden;
}
</style>
""", unsafe_allow_html=True)
st.title("streamlit 포켓몬 도감")
st.markdown("**포켓몬**을 하나씩 추가해서 도감을 채워보세요!")
type_emoji_dict = {
"노말": "⚪",
"격투": "✊",
"비행": "🕊",
"독": "☠️",
"땅": "🌋",
"바위": "🪨",
"벌레": "🐛",
"고스트": "👻",
"강철": "🤖",
"불꽃": "🔥",
"물": "💧",
"풀": "🍃",
"전기": "⚡",
"에스퍼": "🔮",
"얼음": "❄️",
"드래곤": "🐲",
"악": "😈",
"페어리": "🧚"
}
initial_pokemons = [
{
"name": "피카츄",
"types": ["전기"],
"image_url": "https://storage.googleapis.com/firstpenguine-coding-school/pokemons/pikachu.webp"
},
{
"name": "누오",
"types": ["물", "땅"],
"image_url": "https://storage.googleapis.com/firstpenguine-coding-school/pokemons/nuo.webp",
},
{
"name": "갸라도스",
"types": ["물", "비행"],
"image_url": "https://storage.googleapis.com/firstpenguine-coding-school/pokemons/garados.webp",
},
{
"name": "개굴닌자",
"types": ["물", "악"],
"image_url": "https://storage.googleapis.com/firstpenguine-coding-school/pokemons/frogninja.webp"
},
{
"name": "루카리오",
"types": ["격투", "강철"],
"image_url": "https://storage.googleapis.com/firstpenguine-coding-school/pokemons/lukario.webp"
},
{
"name": "에이스번",
"types": ["불꽃"],
"image_url": "https://storage.googleapis.com/firstpenguine-coding-school/pokemons/acebun.webp"
},
]
example_pokemon = {
"name": "알로라 디그다",
"types": ["땅", "강철"],
"image_url": "https://storage.googleapis.com/firstpenguine-coding-school/pokemons/alora_digda.webp"
}
if "pokemons" not in st.session_state:
st.session_state.pokemons = initial_pokemons
auto_complete = st.toggle("예시 데이터로 채우기")
with st.form(key="form"):
col1, col2 = st.columns(2)
with col1:
name = st.text_input(
label="포켓몬 이름",
value=example_pokemon["name"] if auto_complete else ""
)
with col2:
types = st.multiselect(
label="포켓몬 속성",
options=list(type_emoji_dict.keys()),
max_selections=2,
default=example_pokemon["types"] if auto_complete else []
)
image_url = st.text_input(
label="포켓몬 이미지 URL",
value=example_pokemon["image_url"] if auto_complete else ""
)
submit = st.form_submit_button(label="Submit")
if submit:
if not name:
st.error("포켓몬의 이름을 입력해주세요.")
elif len(types) == 0:
st.error("포켓몬의 속성을 적어도 한개 선택해주세요.")
else:
st.success("포켓몬을 추가할 수 있습니다.")
st.session_state.pokemons.append({
"name": name,
"types": types,
"image_url": image_url if image_url else "./images/default.png"
})
for i in range(0, len(st.session_state.pokemons), 3):
row_pokemons = st.session_state.pokemons[i:i+3]
cols = st.columns(3)
for j in range(len(row_pokemons)):
with cols[j]:
pokemon = row_pokemons[j]
with st.expander(label=f"**{i+j+1}. {pokemon['name']}**", expanded=True):
st.image(pokemon["image_url"])
emoji_types = [f"{type_emoji_dict[x]} {x}" for x in pokemon["types"]]
st.text(" / ".join(emoji_types))
delete_button = st.button(label="삭제", key=i+j, use_container_width=True)
if delete_button:
del st.session_state.pokemons[i+j]
st.rerun()