-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.py
117 lines (95 loc) · 2.78 KB
/
web.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
import os
import sys
from math import ceil
import numpy as np
import pandas as pd
import streamlit as st
from annotated_text import annotated_text
def price(n):
s = '{:0,}'.format(int(n))
s = s.replace(',', '.')
return f'Rp {s}'
def to_annotated_text(s, keywords):
r = []
keywords = keywords.lower()
length = len(keywords)
while True:
pos = s.lower().find(keywords)
if pos < 0:
if s:
r += [s]
break
r += [s[:pos]]
r += [(s[pos:pos+length],)]
s = s[pos+length:]
annotated_text(*r)
csv_file = None
for argv in sys.argv[1:]:
if argv[-4:] == '.csv':
csv_file = argv
if not csv_file:
FILES = [
'all.csv',
'http://warga.web.id/files/dijual/all.csv.gz']
for csv_file in FILES:
if os.path.exists(csv_file):
break
COLUMNS = ['title', 'price', 'image', 'description', 'url']
DEFAULT = dict(price=10000)
HOSTS = [
'www.bukalapak.com',
'www.tokopedia.com']
@st.cache(ttl=60*60*24)
def read_csv():
return pd.read_csv(csv_file)
orig_df = read_csv()
price_step = 10000
price_min = int(orig_df.price.min() / price_step + 1) * price_step
price_max = int(orig_df.price.max() / price_step + 1) * price_step
df = orig_df[COLUMNS].copy()
df = df.sort_values(by=['price'])
# Sembunyikan nomor dan price
css = """
<style>
.block-container {max-width: 100rem}
</style>
"""
st.markdown(css, unsafe_allow_html=True)
st.title('Temukan')
search = st.text_input('Cari')
df = df[
df.title.str.contains(search, na=False, case=False) |
df.description.str.contains(search, na=False, case=False)]
if st.checkbox('Harga tertinggi'):
price_choice = st.slider(
'Rp', price_min, price_max, DEFAULT['price'], price_step)
df = df[df.price <= price_choice]
if st.checkbox('Sumber'):
host_choice = st.selectbox('Situs', HOSTS)
df = df[df.url.str.contains(host_choice, na=False)]
limit = 20
row_count = len(df)
page_count = ceil(row_count / limit)
if page_count > 99:
page_count = 99
page_list = [x+1 for x in range(page_count)]
page_choice = st.selectbox('Halaman', page_list) or 1
df = df.sort_values(by=['price', 'title'], ascending=[True, True])
df = df.replace(np.nan, '', regex=True)
st.write(f'Ketemu {row_count}')
offset = (page_choice - 1) * limit
df = df[offset:offset+limit]
for i in df.index:
title = df['title'][i]
url = df['url'][i]
price_rp = price(df['price'][i])
col1, mid, col2 = st.columns([3, 1, 17])
with col1:
st.image(df['image'][i], width=200)
with col2:
st.write(f'[{title}]({url})')
st.write(f'{price_rp}')
if search:
to_annotated_text(df['description'][i], search)
else:
st.write(df['description'][i])