-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatform.py
238 lines (200 loc) · 8.33 KB
/
platform.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# -*- coding: utf-8 -*-
"""
Created on Tue Dec 13 17:44:48 2022
@author: 12427
"""
import streamlit as st
import pandas as pd
import myfunction
import streamlit.components.v1 as components
from datetime import datetime
from st_aggrid import AgGrid, GridUpdateMode
from st_aggrid.grid_options_builder import GridOptionsBuilder
from st_pages import Page, show_pages
# Specify what pages should be shown in the sidebar, and what their titles and icons
show_pages(
[
Page("platform.py", "Astro Research"),
Page("pages/1_chart.py", "Birth Chart"),
]
)
st.set_page_config(
page_title="Astro Research Platform",
page_icon="♋",
layout="centered",
initial_sidebar_state="auto",
)
st.sidebar.image('data/Logo_Icon.png', use_column_width=True)
hide_streamlit_style = """
<style>
#MainMenu {visibility: hidden;
}
footer {visibility: visible;
}
footer:after{
content:'*Data from Astrodatabank';
display:block;
position:relative;
}
</style>
"""
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
@st.cache_data
def load_data():
researchDf = pd.read_pickle('data/researchDf.pkl')
birthInfo = pd.read_pickle('data/birthInfo.pkl')
birthInfo.rename(columns = {'birthtime':'time'}, inplace = True)
df = researchDf.copy()
return researchDf, birthInfo, df
researchDf, birthInfo, df = load_data()
def selectSubCategory(inputdf, colName):
DfDict = {}
catDict = {}
mycatList = []
for name,subDf in inputdf.groupby(colName):
DfDict[name] = subDf
catDict[f"{name} ({len(subDf['adb_id'].unique())} persons)"] = name
if subDf.shape[0]>0:
mycatList.append(f"{name} ({len(subDf['adb_id'].unique())} persons)")
mycatList.reverse()
catDict['All'] = "All"
mycatList.append("All")
#mycatList = researchDf['category'].unique()
return DfDict, catDict, mycatList
# =============================================================================
# researchDfDict = {}
# catDict = {}
# mycatList = []
# for i,j in researchDf.groupby('category'):
# researchDfDict[i] = j
# catDict[f"{i} ({j.shape[0]} items available)"] = i
# mycatList.append(f"{i} ({j.shape[0]} items available)")
# =============================================================================
#mycatList = researchDf['category'].unique()
researchDfDict, catDict, mycatList = selectSubCategory(df, "category")
#st.write(catDict)
category = st.selectbox('Which Category Are You Looking for?', mycatList)
#df = researchDf[researchDf['category'] == category]
if catDict[category] == 'All':
pass
else:
df = researchDfDict[catDict[category]]
#df = df[~df['person'].duplicated()]
#df = researchDfDict[catDict[category]]
#df = df[~df['person'].duplicated()]
#mysubcatList = ['All']
#mysubcatList.extend(df['subcategory'].unique())
researchDfDict, catDict, mysubcatList = selectSubCategory(df, "subcategory")
subcategory = st.selectbox('Which Subcategory Are You Looking for?', mysubcatList)
if catDict[subcategory] == 'All':
pass
else:
df = researchDfDict[catDict[subcategory]]
#df = df[~df['person'].duplicated()]
#mydetailList = ['All']
#mydetailList.extend(df['detail'].unique())
researchDfDict, catDict, mydetailList = selectSubCategory(df, "detail")
detail = st.selectbox('Which Detail Are You Looking for?', mydetailList)
if catDict[detail] == 'All':
pass
else:
#df = df[df['detail'] == detail]
df = researchDfDict[catDict[detail]]
df = df[~df['person'].duplicated()]
nonDupDf = df[~df['adb_id'].duplicated()]
df = nonDupDf.copy()
nonDupDf.sort_values(by=['adb_id'], inplace = True)
nonDupDf.reset_index(drop = True, inplace = True)
nonDupDf = nonDupDf[['person', 'adb_id','As_sign', 'Sun_sign', 'Moon_sign', 'comment']]
#st.dataframe(nonDupDf)
# =============================================================================
# selected_indices = st.multiselect('Select rows:', nonDupDf.index)
# selected_rows = nonDupDf.loc[selected_indices]
# st.write('### Selected Rows', selected_rows)
# =============================================================================
nonDupDf = nonDupDf.merge(birthInfo, on = 'adb_id')
#selectDf.drop(columns = ['adb_id'], inplace = True)
gd = GridOptionsBuilder.from_dataframe(nonDupDf[['person', 'As_sign', 'Sun_sign', 'Moon_sign', 'comment']])
gd.configure_selection(selection_mode='single',
use_checkbox=False,
suppressRowClickSelection = False)
gridoptions = gd.build()
select_person, chart = st.columns([0.4,0.6])
with select_person:
grid_table = AgGrid(nonDupDf, height=400, gridOptions=gridoptions,
update_mode=GridUpdateMode.SELECTION_CHANGED)
with chart:
birth2 = datetime(2023, 1, 1 , 0, 0)
info2 = ["Irvine", "US", birth2]
try:
selected_row = grid_table["selected_rows"]
birth2 = datetime.fromisoformat(selected_row[0]['time'][:-1])
info2 = [selected_row[0]['place'], selected_row[0]['country'], birth2]
#res = myfunction.JSreadable(myfunction.getAllinfo(*info2))
except:
pass
#st.write(info2)
res = myfunction.JSreadable(myfunction.getAllinfo(*info2))
astrodata = "const data = " + str(res)
location = "chartHtml/radix.html"
#location = "testHtml.html"
HtmlFile = open(location, 'r', encoding='utf-8')
source_code = HtmlFile.read()
location2 = "chartHtml/astrochart.js"
HtmlFile2 = open(location2, 'r', encoding='utf-8')
astroChartFunction = HtmlFile2.read()
astroChartFunction = "<script>\n" + astroChartFunction + "\n</script>"
#print(astroChartFunction)
source_code = source_code.replace('<script src="../../build/astrochart.js"></script>', astroChartFunction)
source_code = source_code.replace("var radix = new astrology.Chart('paper', 600, 600).radix( data );",
"var radix = new astrology.Chart('paper', 400, 400).radix( data );")
source_code = source_code.replace('const data = [0]', astrodata)
#print(type(source_code))
components.html(source_code, height=400, scrolling= True)
# =============================================================================
# data = {
# 'country': ['Japan', 'China', 'Thailand', 'France', 'Belgium', 'South Korea'],
# 'capital': ['Tokyo', 'Beijing', 'Bangkok', 'Paris', 'Brussels', 'Seoul']
# }
#
# df = pd.DataFrame(data)
# gd = GridOptionsBuilder.from_dataframe(df)
# gd.configure_selection(selection_mode='multiple', use_checkbox=True)
# gridoptions = gd.build()
#
# grid_table = AgGrid(df, height=250, gridOptions=gridoptions,
# update_mode=GridUpdateMode.SELECTION_CHANGED)
#
# st.write('## Selected')
# selected_row = grid_table["selected_rows"]
# st.dataframe(selected_row)
# =============================================================================
myvarList = [' ']
# =============================================================================
# myvarList.extend(list(researchDf.columns))
# remove_item = ['person','adb_id','Key','name','category',
# 'subcategory','detail','comment']
# myvarList = [e for e in myvarList if e not in remove_item]
# =============================================================================
var_to_keep = list(pd.read_csv('data/var_to_keep.csv').iloc[1:]['variable'])
myvarList.extend(var_to_keep)
myvar = st.selectbox('Which Variable Are You Looking for?', myvarList)
try:
showedDf = pd.DataFrame((df[myvar].value_counts().reset_index()))
showedDf.rename(columns = {myvar:"count"}, inplace = True)
showedDf.rename(columns = {"index":myvar}, inplace = True)
#st.dataframe(showedDf)
#showedDf['house'] = showedDf['house'].map(lambda x: int(x[5:]))
total = showedDf["count"].sum()
showedDf['total'] = total
showedDf['percentage(%)'] = showedDf["count"]/total *100
if myvar == 'mc_ruler' or myvar == 'as_ruler':
showedDf['expected(%)'] = 2/12 *100
showedDf.loc[showedDf[showedDf[myvar].isin(['Sun','Moon'])].index,'expected(%)'] = 1/12 *100
else:
showedDf['expected(%)'] = 1/showedDf.shape[0] *100
showedDf['difference(%)'] = showedDf['percentage(%)'] - showedDf['expected(%)']
st.dataframe(showedDf.style.format({'expected(%)': '{:.2f}',
'percentage(%)': '{:.2f}', 'difference(%)': '{:.2f}'}))
except:
pass