-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsu_finder.py
184 lines (147 loc) · 4.44 KB
/
psu_finder.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import os
import sys
import numpy as np
import pandas as pd
import streamlit as st
st.set_page_config(page_title='Cari Power Supply Unit')
def get_list(column: str, cast_func=None):
c = getattr(orig_df, column)
tmp_df = orig_df[c.notnull()]
c = getattr(tmp_df, column)
if cast_func:
list_ = [cast_func(x) for x in c.drop_duplicates()]
else:
list_ = [x for x in c.drop_duplicates()]
list_.sort()
if column in DEFAULT:
index = -1
for val in list_:
index += 1
if val >= DEFAULT[column]:
break
else:
index = 0
return list_, index
def filter_name(column, label):
list_, index = get_list(column)
choice = st.selectbox(label, list_, index=index)
c = getattr(df, column)
return df[c == choice]
def filter_min(column: str, label: str, cast_func=None) -> pd.DataFrame:
list_, index = get_list(column, cast_func)
choice = st.selectbox(label, list_, index=index)
c = getattr(df, column)
return df[c >= choice]
def sort_by_label(key):
return SORT_BY[key]
def get_title(cols):
return f'<a href="{cols.url}">{cols.title}</a>'\
f'<br/><em>{cols.time}</em>'
def get_is_new(is_new: int, stock: int):
if not stock:
return 'HABIS'
if is_new == 1:
return 'BARU'
return 'BEKAS'
def get_price(cols):
s = '{:0,}'.format(int(cols.price))
s = s.replace(',', '.')
s = f'Rp {s}'
label = get_is_new(cols.is_new, cols.stock)
if label:
cls = ['c-label']
if cols.stock:
if cols.is_new:
cls.append('c-label--green')
else:
cls.append('c-label--pink')
cls = ' '.join(cls)
s += f'<div class="{cls}">{label}</div>'
return s
def get_power(cols):
if pd.isnull(cols.power_watt):
return ''
return f'{int(cols.power_watt)} Watt'
def sort_by_label(key):
return SORT_BY[key]
COLUMNS = ['title', 'price', 'power_watt', 'model_name']
SORT_BY = dict(price='Price', power_watt='Watt')
SORT_BY_KEYS = list(SORT_BY.keys())
ASC = dict(price=True, power_watt=False)
DEFAULT = dict(price=4000000, power_watt=1000, model_name='Platinum')
csv_file = None
for argv in sys.argv[1:]:
if argv[-4:] == '.csv':
csv_file = argv
if not csv_file:
FILES = [
'psu.csv',
'http://warga.web.id/files/dijual/psu.csv.gz']
for csv_file in FILES:
if os.path.exists(csv_file):
break
@st.cache_data(ttl=60*60*24)
def read_csv():
return pd.read_csv(csv_file)
orig_df = read_csv()
orig_df = orig_df[orig_df.category == 'psu']
df = orig_df.copy()
st.title('Power Supply Unit')
if st.checkbox('Brand'):
df = filter_name('brand_name', 'Brand')
if st.checkbox('Minimum power'):
df = filter_min('power_watt', 'Watt', int)
if st.checkbox('Model'):
df = filter_name('model_name', 'Name')
if st.checkbox('Maximum price'):
step = 500000
tmp_df = orig_df[orig_df.stock > 0]
min_ = int(tmp_df.price.min() / step + 1) * step
max_ = int(tmp_df.price.max() / step + 1) * step
choice = st.slider('Rp', min_, max_, DEFAULT['price'], step)
df = df[df.price <= choice]
if st.checkbox('New'):
df = df[df.is_new == 1]
if st.checkbox('Stock'):
df = df[df.stock > 1]
choice = st.selectbox(
'Sort by', options=SORT_BY_KEYS, format_func=sort_by_label)
if choice != 'price':
c = getattr(df, choice)
df = df[c.notnull()]
df = df.sort_values(by=[choice], ascending=[ASC[choice]])
count = len(df)
if count:
df = df.replace(np.nan, '', regex=True)
tmp_df = df[COLUMNS].copy()
tmp_df['title'] = df.apply(get_title, axis='columns')
tmp_df['price'] = df.apply(get_price, axis='columns')
tmp_df['power_watt'] = df.apply(get_power, axis='columns')
st.write(f'Found {count} rows')
css = '''
<style>
.block-container {max-width: 100rem}
th {display: none}
td {vertical-align: top}
.c-label {
height: 18px;
padding: 1px 6px;
margin: 0;
overflow: visible;
line-height: 14px;
vertical-align: middle;
background-color: #fafafa;
border: 1px solid #ddd;
border-radius: 2px;
}
.c-label--pink {
background-color: #ff566a;
}
.c-label--green {
background-color: #3cff33;
}
</style>'''
st.markdown(css, unsafe_allow_html=True)
st.write(tmp_df.to_html(escape=False), unsafe_allow_html=True)
else:
st.write('No result')