File size: 5,891 Bytes
7d13043
 
4393140
7d13043
d200e3d
 
 
 
 
 
 
 
7ee1f08
 
236a96c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d200e3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1764877
 
 
 
 
d200e3d
 
 
 
edc587d
 
d200e3d
767d8ff
 
 
d200e3d
 
a2fe95b
731c683
 
d200e3d
7d13043
a2fe95b
 
 
 
66c0494
d200e3d
8da215d
 
 
 
a2fe95b
 
66c0494
634642d
a2fe95b
66c0494
 
236a96c
a2fe95b
 
 
66c0494
8da215d
 
 
 
 
a2fe95b
 
66c0494
 
2613315
66c0494
d200e3d
66c0494
 
 
 
0eb0f99
236a96c
a2fe95b
634642d
a2fe95b
 
634642d
a2fe95b
 
634642d
a2fe95b
 
66c0494
 
 
8da215d
 
 
 
 
 
 
 
 
66c0494
634642d
a2fe95b
 
634642d
a2fe95b
 
 
8da215d
d200e3d
 
 
66c0494
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import pandas as pd
import numpy as np
import streamlit as st

# import glob
# import yaml
from pathlib import Path
from collections import defaultdict

#########################################
# Helpers Functions

display_cols = ['image','name', 'color', 'star', 'class', 'speed', 'power', 'attack', 'defense', 'health', 'types', 'source', 'family']

def filter_by_1col_num(df, col_name, query, oper_flag="eq"):
    ok_flag_list = []
    assert col_name in df.columns, "col_name must be valid"

    for i, val in enumerate(df[col_name]):
        if oper_flag == 'ge':
            flag = True if val >= query else False
        elif oper_flag == 'le':
            flag = True if val <= query else False
        else: # default = eq
            flag = True if val == query else False

        ok_flag_list.append(flag)
    
    assert len(ok_flag_list) == len(df)
    return np.array(ok_flag_list)

def filter_by_1col(df, col_name, query, exact_flag=False):

    def check_valid_value(query, string, exact_flag=False):
        if exact_flag:
            if query.lower() == string.lower():
                return True

        elif query.lower() in string.lower():
            return True
        
        return False

    ok_flag_list = []
    assert col_name in df.columns, "col_name must be valid"

    for i, s in enumerate(df[col_name]):

        if isinstance(s, list):
            for s2 in s:
                flag = check_valid_value(query, s2, exact_flag=exact_flag)
                if flag: break
        else:
            flag = check_valid_value(query, s, exact_flag=exact_flag)

        
        ok_flag_list.append(flag)
    
    assert len(ok_flag_list) == len(df)
    return np.array(ok_flag_list)

def display_image(url, scale=0.5):
    from urllib.request import urlopen
    from PIL import Image

    image = Image.open(urlopen(url))
    st.image(image.resize(( int(image.width * scale), int(image.height * scale))))

def display_heroes_from_df(df):
    st.dataframe(df[display_cols],
                 column_config={
                         "image": st.column_config.ImageColumn("Avatar", help="")},
                 use_container_width=True,
                 hide_index=True)

    for i in range(len(df)):
        url = df['image'].values[i]
        display_image(url)
        st.write(f"{df['name'].values[i]} - {df['speed'].values[i]} - {df['class'].values[i]}")
        st.write(f'Attack:{df["attack"].values[i]} -- Defence:{df["defense"].values[i]} -- Health:{df["health"].values[i]}')
        st.write(df['skill'].values[i])
        st.write(df['effects'].values[i])
        # for sp in df['effects'].values[i]:
        #     st.write(sp)

#########################################
## Load the main file (TODO: caching)=
st.set_page_config(layout="wide")


df = pd.read_csv('heroes_ep.csv')
class_values = ['None'] + list(df['class'].unique()) 
star_values = ['None'] + list(df['star'].unique())
color_values = ['None'] + list(df['color'].unique())
speed_values = ['None'] + list(df['speed'].unique())
source_values = ['None'] + list(df['source'].unique())

defense_values = ['None'] + list(df['defense'].unique())
attack_values = ['None'] + list(df['attack'].unique())
health_values = ['None'] + list(df['health'].unique())

#########################################
## Select options
## TODO: family, costume

with st.sidebar:
    st.title('Filter Options')
    name_option = st.text_input(label="Name:", value="")
    star_option = st.selectbox(label='Star:', options=star_values, index=0)
    color_option = st.selectbox(label='Color:', options=color_values, index=0)
    speed_option = st.selectbox(label='Speed:', options=speed_values, index=0)
    class_option = st.selectbox(label='Class:', options=class_values, index=0)
    source_option = st.selectbox(label='Origin:', options=source_values, index=0)
    
    defense_option = st.selectbox(label='Defense:', options=defense_values, index=0)
    attack_option = st.selectbox(label='Attack:', options=attack_values, index=0)
    health_option = st.selectbox(label='Health:', options=health_values, index=0)
    
    special_type_option = st.text_input(label="SpecialSkill Category", value="Hit 3")
    special_text_option = st.text_input(label="SpecialSkill Text", value="Dispel")

    st.title('Sorted By')
    sort_option = st.selectbox(label='Sort by', options=display_cols[1:], index=5) # default is power
    
idx_all = []

if name_option != '':
    idx_all.append(filter_by_1col(df, 'name', name_option, exact_flag=False)) 

if star_option != 'None':
    idx_all.append(filter_by_1col_num(df, 'star', star_option, oper_flag="eq"))    

if speed_option != 'None':
    idx_all.append(filter_by_1col(df, 'speed', speed_option, exact_flag=True))    

if color_option != 'None':
    idx_all.append(filter_by_1col(df, 'color', color_option, exact_flag=False))    

if class_option != 'None':
    idx_all.append(filter_by_1col(df, 'class', class_option, exact_flag=False))    

if source_option != 'None':
    idx_all.append(filter_by_1col(df, 'source', source_option, exact_flag=False))    

if defense_option != 'None':
    idx_all.append(filter_by_1col_num(df, 'defense', defense_option, oper_flag="ge"))   

if attack_option != 'None':
    idx_all.append(filter_by_1col_num(df, 'attack', attack_option, oper_flag="ge"))   

if health_option != 'None':
    idx_all.append(filter_by_1col_num(df, 'health', health_option, oper_flag="ge"))   


if special_type_option  != '':
    idx_all.append(filter_by_1col(df, 'types', special_type_option, exact_flag=False))    

if special_text_option != '':
    idx_all.append(filter_by_1col(df, 'effects', special_text_option, exact_flag=False))    
    
#########################################
st.title(f'Updated: Oct 23, 2023 -- Total heroes = {len(df)}')

df2 = df[np.all(idx_all,axis=0)]

display_heroes_from_df(df2.sort_values(sort_option, ascending=False))