-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexplore.py
More file actions
66 lines (55 loc) · 2.42 KB
/
explore.py
File metadata and controls
66 lines (55 loc) · 2.42 KB
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
import streamlit as st
import pandas as pd
def objects(mydb):
mycursor = mydb.cursor()
sql_cat = "SELECT DISTINCT category_name FROM stock_list ORDER BY category_name ASC"
sql_exchange = "SELECT DISTINCT exchange FROM stock_list ORDER BY exchange ASC"
sql_country = "SELECT DISTINCT country FROM stock_list ORDER BY country ASC"
mycursor.execute(sql_cat)
Cat = mycursor.fetchall()
mycursor.execute(sql_exchange)
Exc = mycursor.fetchall()
mycursor.execute(sql_country)
Coun = mycursor.fetchall()
c = [ i[0] for i in Cat]
e = [ i[0] for i in Exc]
co = [ i[0] for i in Coun]
options_country = st.multiselect('Select Country', co, ['Taiwan'])
options_category = st.multiselect('Select Category', c)
options_exchange = st.multiselect('Select Exchange', e)
cat_name = ['category_name' for i in range(len(options_category))]
cou_name = ['country' for i in range(len(options_country))]
exc_name = ['exchange' for i in range(len(options_exchange))]
options = list(zip(cou_name, options_country)) + list(zip(cat_name, options_category)) + list(zip(exc_name, options_exchange))
orderby = st.radio("Data is ordered by ", ('Country', 'Category', 'Exchange'))
if orderby == 'Country':
order = 'country'
elif orderby == 'Category':
order = 'category_name'
else:
order = 'exchange'
if not options:
pass
else:
x = True
query = "SELECT * FROM stock_list"
for i in range(len(options)):
if x == True:
x = False
query = query + " WHERE ("+ options[i][0] + "='" + options[i][1] + "'"
elif options[i-1][0] != options[i][0]:
query = query + ") AND ("+ options[i][0] + "='" + options[i][1] + "'"
else:
query = query + " OR "+ options[i][0] +"='" + options[i][1]+"'"
query = query + ") ORDER BY " + order + " ASC"
mycursor.execute(query)
result = mycursor.fetchall()
DFC = pd.DataFrame(result, columns=['Ticker','Name' ,'Exchange','Category','Country'])
DFC.reset_index(inplace=False)
if orderby == 'Country':
DFC.set_index("Country", inplace=True)
elif orderby == 'Exchange':
DFC.set_index("Exchange", inplace=True)
else:
DFC.set_index("Category", inplace=True)
st.table(DFC)