-
Notifications
You must be signed in to change notification settings - Fork 2
/
visualization_page.py
248 lines (210 loc) · 12.3 KB
/
visualization_page.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
239
240
241
242
243
244
245
from typing import Text
from sqlalchemy import create_engine, engine
import streamlit as st
import plotly.express as px
import pandas as pd
import datetime
import os
def visualization_page():
# html_temp = "<div class='tableauPlaceholder' id='viz1638766654954' style='position: relative'><noscript><a href=''><img alt='DStreamlit ' src='https://public.tableau.com/static/images/Ba/BansosRevisi/DStreamlit/1_rss.png' style='border: none' /></a></noscript><object class='tableauViz' style='display:none;'><param name='host_url' value='https%3A%2F%2Fpublic.tableau.com%2F' /> <param name='embed_code_version' value='3' /> <param name='site_root' value='' /><param name='name' value='BansosRevisi/DStreamlit' /><param name='tabs' value='no' /><param name='toolbar' value='yes' /><param name='static_image' value='https://public.tableau.com/static/images/Ba/BansosRevisi/DStreamlit/1.png' /> <param name='animate_transition' value='yes' /><param name='display_static_image' value='yes' /><param name='display_spinner' value='yes' /><param name='display_overlay' value='yes' /><param name='display_count' value='yes' /><param name='language' value='en-US' /></object></div> <script type='text/javascript'> var divElement = document.getElementById('viz1638766654954'); var vizElement = divElement.getElementsByTagName('object')[0]; if ( divElement.offsetWidth > 800 ) { vizElement.style.width='1000px';vizElement.style.height='1827px';} else if ( divElement.offsetWidth > 500 ) { vizElement.style.width='1000px';vizElement.style.height='1827px';} else { vizElement.style.width='100%';vizElement.style.height='3327px';} var scriptElement = document.createElement('script'); scriptElement.src = 'https://public.tableau.com/javascripts/api/viz_v1.js'; vizElement.parentNode.insertBefore(scriptElement, vizElement); </script>"
#components.html(html_temp, width=1000, height=1500)
st.markdown("""
<iframe width="975" height="1975" src="https://datastudio.google.com/embed/reporting/88cbb69d-8792-440d-b8d4-e232eca1c746/page/6Z7gC" frameborder="0" style="border:0" allowfullscreen></iframe>
""", unsafe_allow_html=True)
# try:
# conn = create_engine('mysql://root:@localhost/db_bansos')
# except Exception as error:
# print("Error while connecting to MySQL", error)
# all_bansos = pd.read_sql_query('select * from bansos', conn)
# all_kecamatan = pd.read_sql_query('select * from kecamatan', conn)
# all_kelurahan = pd.read_sql_query('select * from kelurahan', conn)
# st.write("Total Data: ", all_bansos.shape[0])
# st.write("Total Penerima: ", all_bansos["KET_KK_NIK" == "KK AMAN"].sum())
# st.write("Total Ditolak: ", )
# dbconnection_str = 'mysql+pymysql://'+os.environ['db_username']+':'+os.environ['db_password']+'@'+os.environ['db_host']+'/'+os.environ['db_name']
# engine = create_engine(dbconnection_str)
# query = engine.execute('select bansos.nik, bansos.no_kk, bansos.nama, bansos.nik_capil, bansos.no_kk_capil, bansos.nama_lgkp_capil, bansos.status, bansos.kategori, bansos.opd_pengampu, bansos.tahap, bansos.alamat_capil, bansos.kelurahan_capil, bansos.kecamatan_capil, bansos.domisili, bansos.ket_nik, bansos.jenis_kelamin, bansos.ket_nama, bansos.ket_kk_nik, bansos.usia, bansos.label, bansos.date, kelurahan.latitude, kelurahan.longitude from bansos left join kelurahan on bansos.kelurahan_capil = kelurahan.kelurahan_capil')
# rows = query.fetchall()
# df = pd.DataFrame( [[ij for ij in i] for i in rows] )
# df.rename(columns={0: 'NIK',
# 1: 'NO_KK',
# 2: 'NAMA',
# 3: 'NIK_CAPIL',
# 4: 'NO_KK_CAPIL',
# 5: 'NAMA_LGKP_CAPIL',
# 6: 'STATUS',
# 7: 'KATEGORI',
# 8: 'OPD_PENGAMPU',
# 9: 'TAHAP',
# 10: 'ALAMAT_CAPIL',
# 11: 'KELURAHAN_CAPIL',
# 12: 'KECAMATAN_CAPIL',
# 13: 'DOMISILI',
# 14: 'KET_NIK',
# 15: 'JENIS_KELAMIN',
# 16: 'KET_NAMA',
# 17: 'KET_KK_NIK',
# 18: 'USIA',
# 19: 'LABEL',
# 20: 'DATE',
# 21: 'LATITUDE',
# 22: 'LONGITUDE',}, inplace=True);
# # FILTERING CATEGORY
# st.subheader("Filter berdasarkan: ")
# # ROW FILTER DATE
# row1_1, row1_2, row1_3 = st.columns((1,1,1))
# with row1_1:
# today = datetime.date.today()
# tomorrow = today + datetime.timedelta(days=1)
# start_date = st.date_input('Start date', today)
# with row1_2:
# end_date = st.date_input('End date', tomorrow)
# with row1_3:
# if start_date < end_date:
# st.success('Start date: `%s`\n\nEnd date:`%s`' % (start_date, end_date))
# else:
# st.error('Error: End date must fall after start date.')
# # ROW FILTER GENERAL
# row2_1, row2_2, row2_3, row2_4 = st.columns((1,1,1,1))
# with row2_1:
# with st.expander("LABEL"):
# label = st.multiselect(
# "Pilih Label Bansos:",
# options = df["LABEL"].unique(),
# default = df["LABEL"].unique()
# )
# with st.expander("OPD PENGAMPU"):
# opd_pengampu = st.multiselect(
# "Pilih OPD Pengampu Bansos:",
# options = df["OPD_PENGAMPU"].unique(),
# default = df["OPD_PENGAMPU"].unique()
# )
# with row2_2:
# with st.expander("STATUS"):
# status = st.multiselect(
# "Pilih Status Bansos:",
# options = df["STATUS"].unique(),
# default = df["STATUS"].unique()
# )
# with st.expander("KATEGORI"):
# kategori = st.multiselect(
# "Pilih Kategori Bansos:",
# options = df["KATEGORI"].unique(),
# default = df["KATEGORI"].unique()
# )
# with row2_3:
# with st.expander("JENIS KELAMIN"):
# jenis_kelamin = st.multiselect(
# "Pilih Jenis Kelamin:",
# options = df["JENIS_KELAMIN"].unique(),
# default = df["JENIS_KELAMIN"].unique()
# )
# with st.expander("TAHAP"):
# tahap = st.multiselect(
# "Pilih Tahap Bansos:",
# options = df["TAHAP"].unique(),
# default = df["TAHAP"].unique()
# )
# with row2_4:
# with st.expander("KECAMATAN_CAPIL"):
# kecamatan_capil = st.multiselect(
# "Pilih Kecamatan:",
# options = df["KECAMATAN_CAPIL"].unique(),
# default = df["KECAMATAN_CAPIL"].unique()
# )
# with st.expander("KELURAHAN_CAPIL"):
# kelurahan_capil = st.multiselect(
# "Pilih Kelurahan:",
# options = df["KELURAHAN_CAPIL"].unique(),
# default = df["KELURAHAN_CAPIL"].unique()
# )
# df_selection = df.query(
# "KECAMATAN_CAPIL == @kecamatan_capil & KELURAHAN_CAPIL == @kelurahan_capil & JENIS_KELAMIN == @jenis_kelamin & LABEL == @label & STATUS == @status & OPD_PENGAMPU == @opd_pengampu & TAHAP == @tahap"
# )
# # Jumlah Data
# total_data = (df_selection["LABEL"] != "" ).sum()
# total_diterima = (df_selection["LABEL"] != "TIDAK LAYAK DAPAT BANSOS").sum()
# total_ditolak = (df_selection["LABEL"] == "TIDAK LAYAK DAPAT BANSOS").sum()
# left_column, middle_column, right_column = st.columns(3)
# with left_column:
# st.subheader("Total Data:")
# st.subheader(f"{total_data} Jiwa")
# with middle_column:
# st.subheader("Total Diterima:")
# st.subheader(f"{total_diterima} Jiwa")
# with right_column:
# st.subheader("Total Ditolak:")
# st.subheader(f"{total_ditolak} Jiwa")
# # PETA
# dfKelurahan = df_selection[['KELURAHAN_CAPIL', 'LATITUDE', 'LONGITUDE']]
# dfKelurahan = dfKelurahan.value_counts().reset_index()
# dfKelurahan.columns = ['KELURAHAN_CAPIL', 'lat', 'lon', 'count']
# dfKelurahan['text'] = dfKelurahan['KELURAHAN_CAPIL']
# access_token = 'pk.eyJ1IjoiZGV2YW5pc2R3aSIsImEiOiJja3kzeXFhcjQwMzU1MnZxYzJ5OG1rYmIxIn0.nB78CAvkZi-J9os0VsBoCw'
# px.set_mapbox_access_token(access_token)
# figKelurahan = px.scatter_mapbox(dfKelurahan, lat="lat", lon='lon', size='count', size_max=15, zoom=11, hover_name='KELURAHAN_CAPIL')
# st.plotly_chart(figKelurahan, use_container_width=True)
# row_OPD, row_tahap = st.columns(2)
# with row_OPD:
# dfOPD = df_selection[['OPD_PENGAMPU']]
# dfOPD = dfOPD.fillna('Tidak ada OPD')
# dfOPD = dfOPD.value_counts().reset_index()
# dfOPD.columns = ['OPD_PENGAMPU', 'count']
# figOPD = px.bar(dfOPD, x='OPD_PENGAMPU',
# y='count', color='count',
# title='Persebaran Data berdasarkan OPD Pengampu')
# st.plotly_chart(figOPD, use_container_width=True)
# with row_tahap:
# dfTahap = df_selection[['TAHAP']]
# dfTahap = dfTahap.fillna('Tidak Terdeteksi')
# dfTahap = dfTahap.value_counts().reset_index()
# dfTahap.columns = ['TAHAP', 'count']
# figTahap = px.pie(dfTahap, values='count', names='TAHAP', title='Persebaran Data berdasarkan Tahap', hole=0.4)
# st.plotly_chart(figTahap, use_container_width=True)
# row_gender_label, row_kategori = st.columns(2)
# with row_gender_label:
# # GENDER
# dfGender = df_selection[['JENIS_KELAMIN']]
# dfGender = dfGender.fillna('Tidak Terdeteksi')
# dfGender = dfGender.value_counts().reset_index()
# dfGender.columns = ['JENIS_KELAMIN', 'count']
# parentGender = []
# for i in range(len(dfGender['JENIS_KELAMIN'].unique())):
# parentGender.append("")
# figGender = px.treemap(dfGender, names=dfGender['JENIS_KELAMIN'].unique(), parents=parentGender, values='count', title='Persebaran Data berdasarkan Jenis Kelamin')
# st.plotly_chart(figGender, use_container_width=True)
# with row_kategori:
# # LABEL
# dfLabel = df_selection[['LABEL']]
# dfLabel = dfLabel.fillna('Tidak Terdeteksi')
# dfLabel = dfLabel.value_counts().reset_index()
# dfLabel.columns = ['LABEL', 'count']
# parentLabel = []
# for i in range(len(dfLabel['LABEL'].unique())):
# parentLabel.append("")
# figLabel = px.treemap(dfLabel, names=dfLabel['LABEL'].unique(), parents=parentLabel, values='count', title='Persebaran Data berdasarkan Label')
# st.plotly_chart(figLabel, use_container_width=True)
# # PIVOT TABLE KATEGORI
# dfKategori = df_selection[['KATEGORI', 'LABEL']]
# dfKategori = dfKategori.fillna('Tidak ada KATEGORI')
# dfKategori = dfKategori.value_counts().reset_index()
# dfKategori.columns = ['KATEGORI', 'LABEL', 'JUMLAH']
# st.write("Persebaran Data berdasarkan Kategori Pekerjaan")
# dfKategori = pd.pivot_table(data = dfKategori, index=['KATEGORI'], columns=['LABEL'], values=['JUMLAH'])
# st.dataframe(dfKategori)
# # STATUS
# dfStatus = df_selection[['STATUS']]
# dfStatus = dfStatus.fillna('Tidak Terdeteksi')
# dfStatus = dfStatus.value_counts().reset_index()
# dfStatus.columns = ['STATUS', 'count']
# figStatus = px.bar(dfStatus, x='count',
# y='STATUS', color='STATUS', orientation='h',
# title='Persebaran Data berdasarkan Status')
# st.plotly_chart(figStatus, use_container_width=True)
# # USIA DAN GENDER
# dfUsia = df_selection[['USIA', 'JENIS_KELAMIN']]
# dfUsia = dfUsia.value_counts().reset_index()
# dfUsia.columns = ['USIA', 'JENIS KELAMIN', 'JUMLAH']
# dfUsia = dfUsia.sort_values(by = 'USIA')
# figUsia = px.line(dfUsia, x='USIA', y='JUMLAH', color='JENIS KELAMIN', symbol="JENIS KELAMIN", title='Persebaran Data berdasarkan Usia')
# st.plotly_chart(figUsia, use_container_width=True)