-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
278 lines (242 loc) · 13.1 KB
/
app.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import streamlit as st
import pydeck as pdk
import argparse
from streamlit_extras.stylable_container import stylable_container
from streamlit_option_menu import option_menu
from webpage.pulse import *
from webpage.about import *
from webpage.basic_insight import *
from webpage.search import *
from scripts.pulse.layers import *
from scripts.pulse.viewstate import *
from scripts.data.top_data import *
from tools.mysql import *
def parse_mysql_arg (db_arg):
arg = {}
pairs = db_arg.split()
for pair in pairs:
key, value = pair.split(':')
arg[key.strip()] = value.strip()
k= list(arg.keys())
db_par = ["host", "user", "password", "database"]
if not db_par in k:
raise Exception
return arg
parser = argparse.ArgumentParser(description='Phonepe Pulse Data Visualization')
parser.add_argument('--use_mysql', type= bool, default=False , help="If --use_mysql is true the script will use mysql else it will use the csv files")
parser.add_argument('--mysql_arg', type= parse_mysql_arg, default=False , help="this arguments are used to connected to the database If it is not provide defaults from docker will be used")
args = parser.parse_args()
class Website():
def __init__(self, mysql) :
self.mysql = mysql
self.arg = args.mysql_arg
if mysql:
if self.arg:
Mysql(host=self.arg["host"], user= self.arg["user"], password=self.arg["password"], database=self.arg["database"]).store_data_in_mysql()
Mysql().store_data_in_mysql()
self.website()
def home(self):
space1, head_col, space2 = st.columns([3,7,3])
with head_col:
st.markdown("# :violet[PhonePe Data Visualization and Exploration]")
st.write("")
st.write("")
st.write("")
st.write("")
st.write("")
img_col,data_col = st.columns([2,3],gap="medium")
with img_col:
st.image("src/images/phonepe_home.png", width=600)
with data_col:
st.write("")
st.write("")
st.write("")
st.markdown("### :violet[Domain :] Fintech")
st.markdown("### :violet[Technologies used :] Github Cloning, Python, Pandas, MySQL, mysql-connector-python, Streamlit, and Plotly.")
st.markdown("### :violet[Overview :] This is a streamlit web app you can visualize the phonepe pulse data and gain lot of insights on transactions, number of users, insurances , top 10 state, district, pincode and which brand has most number of users and so on. Bar charts, Pie charts and Geo map visualization are used to get basic insights.")
return None
def pulse(self):
map_column, details_colmun = st.columns([7,4])
with details_colmun:
with stylable_container(key ="info_container", css_styles="""
{background-color: #3A3C5E;}"""):
data_option, year, quater = detail_col_1()
recenter_button, load_map = detail_col_2()
st.markdown("----", unsafe_allow_html=True)
top_data = detail_col_3(load_map, data_option)
info_dict = {"data_option":data_option,"year":year, "quater":quater, "state": load_map, "top_data": top_data}
space1, detial_info, top_10 = st.columns([1,20,20])
with detial_info:
detail_info_col(info_dict)
with top_10:
st.markdown(f"<h3>Top 10 {top_data}</h3>", unsafe_allow_html=True)
top_10_result = top10_data(info_dict)
top_10_result["name"] = top_10_result["name"].str.title()
top_10_result = top_10_result.style.apply(lambda x: ['color: #b069ff' if isinstance(v, int) else '' for v in x], axis=1)
st.markdown(top_10_result.hide(axis = 0).hide(axis = 1).to_html(), unsafe_allow_html = True)
st.write("")
st.write("")
st.write("")
initial_view_state = viewstate(load_map)
layer = list(create_layers(info_dict))
if data_option == "Transaction":
r = pdk.Deck(
initial_view_state=initial_view_state,
layers=layer, map_provider=None, tooltip={"html": """<p style= 'margin: 0; line-height: 1;'>{state}</p>
<b style='color:#b069ff; margin: 0;line-height: 3;'>{ST_NM}</b>
<p style= 'margin: 0; line-height: 1; color:white;'>All Transactions</p>
<b style='color:#b069ff; margin: 0;line-height: 3;'>{Count}</b>
<p style= 'margin: 0; line-height: 1; color:white;'>Total Payment Value</p>
<b style='color:#b069ff; margin: 0;line-height: 3;'>{Amount}</b>
<p style= 'margin: 0; line-height: 1; color:white;'>Avg. Transactions Value</p>
<b style='color:#b069ff; margin: 0;line-height: 3;'>{Avg}</b>""",
"style": {"color": "white", "backgroundColor": "#121326"}})
elif data_option =="User":
r = pdk.Deck(
initial_view_state=initial_view_state,
layers=layer, map_provider=None, tooltip={"html": """<p style= 'margin: 0; line-height: 1;'>{state}</p>
<b style='color:#b069ff; margin: 0;line-height: 3;'>{ST_NM}</b>
<p style= 'margin: 0; line-height: 1; color:white;'>Registered Users</p>
<b style='color:#b069ff; margin: 0;line-height: 3;'>{registeredUsers}</b>
<p style= 'margin: 0; line-height: 1; color:white;'>App Opens</p>
<b style='color:#b069ff; margin: 0;line-height: 3;'>{appOpens}</b> """,
"style": {"color": "white", "backgroundColor": "#121326"}})
else:
r = pdk.Deck(
initial_view_state=initial_view_state,
layers=layer, map_provider=None, tooltip={"html": """<p style= 'margin: 0; line-height: 1;'>{state}</p>
<b style='color:#b069ff; margin: 0;line-height: 3;'>{ST_NM}</b>
<p style= 'margin: 0; line-height: 1; color:white;'>Insurance Policies(Nos.)</p>
<b style='color:#b069ff; margin: 0;line-height: 3;'>{Count}</b>
<p style= 'margin: 0; line-height: 1; color:white;'>Total Premium Value</p>
<b style='color:#b069ff; margin: 0;line-height: 3;'>{Amount}</b>
<p style= 'margin: 0; line-height: 1; color:white;'>Avg. Premium Value</p>
<b style='color:#b069ff; margin: 0;line-height: 3;'>{Avg}</b>""",
"style": {"color": "white", "backgroundColor": "#121326"}})
with map_column:
if recenter_button:
r.initial_view_state = initial_view_state
with st.spinner("Loading the Map"):
c = st.pydeck_chart(r)
return None
def basic_insights(self):
space1,tpye_col, year_col, quater_col, space2 = st.columns([2,4,4,4,2], gap="large")
with tpye_col:
Type = st.selectbox("**Type**", ("Transactions", "Users", "Insurance"))
if Type == "Insurance":
year_min = 2020
else:
year_min = 2018
with year_col:
Year = st.slider("**Year**", min_value=year_min, max_value=2023)
if (Type == "Insurance") & (Year == 2020):
quater_min = 2
else:
quater_min = 1
with quater_col:
Quarter = st.slider("**Quarter**", min_value=quater_min, max_value=4)
data = {"type":Type, "year":Year, "quater":Quarter}
if Type == "Transactions":
if self.arg:
trancaction(data,self.mysql, self.arg)
else:
trancaction(data,self.mysql)
if Type == "Insurance":
if self.arg:
incurance(data,self.mysql, self.arg)
else:
incurance(data,self.mysql)
if Type == "Users":
if self.arg:
users(data,self.mysql, self.arg)
else:
users(data,self.mysql)
return None
def search(self):
colum1,colum2= st.columns([1.25,1],gap="large")
with colum2:
st.write("")
Type = st.selectbox("**Type**", ("Transactions", "Users", "Insurance"))
if Type == "Insurance":
year_min = 2020
else:
year_min = 2018
Year = st.slider("**Year**", min_value=year_min, max_value= 2023)
if (Type == "Insurance") & (Year == 2020):
quater_min = 2
else:
quater_min = 1
Quarter = st.slider("**Quarter**", min_value=quater_min, max_value=4)
data = {"type":Type, "year":Year, "quater":Quarter}
with colum1:
st.markdown("## :violet[Top Charts]")
st.info(
"""
#### From this menu we can get insights like :
- Overall ranking on a particular Year and Quarter.
- Top 10 State, District, Pincode based on Total number of transaction and Total amount spent on phonepe.
- Top 10 State, District, Pincode based on Total phonepe users and their app opening frequency.
- Top 10 mobile brands and its percentage based on the how many people use phonepe.
""",icon="🔍"
)
if Type == "Transactions":
if self.arg:
top_transaction(data,self.mysql, self.arg)
else:
top_transaction(data,self.mysql)
if Type == "Insurance":
if self.arg:
top_insurance(data,self.mysql, self.arg)
else:
top_insurance(data,self.mysql)
if Type == "Users":
if self.arg:
top_insurance(data,self.mysql, self.arg)
else:
top_users(data,self.mysql)
return None
def about(self):
return about_page()
def website(self):
with stylable_container(key ="navigation_bar", css_styles="""
{background-color: #121326;}"""):
title_column,navigation_column = st.columns([12,18])
with title_column:
s,logo, title = st.columns([1,6,40])
with logo:
st.write("")
img = "src/images/phonepe.png"
st.image(img, width=95)
st.write("")
with title:
st.markdown(
"""
<h1 style='text-align: center;'> Phonepe Data Visuvalization</h1>
""",
unsafe_allow_html=True
)
with navigation_column:
options = option_menu(menu_title = "Navigation Bar",
options = ["Home","Pulse","Basic insights","Search","About"],
icons =["house","graph-up-arrow","toggles","search","bar-chart"],
default_index=0,
orientation="horizontal",
styles={
"icon": {"color": "#70717c", "font-size": "20px"},
"nav-link": {"font-size": "20px", "text-align": "center", "margin": "-2px", "--hover-color": "#6F36AD"},
"nav-link-selected": {"background-color": "black"}
})
if options == "Home":
self.home()
elif options == "Pulse":
self.pulse()
elif options == "Basic insights":
self.basic_insights()
elif options == "Search":
self.search()
elif options == "About":
self.about()
if __name__ == "__main__":
# To increase the wide of the webpage
st.set_page_config(layout="wide", page_title="Phonepe Data Visuvalization", page_icon="src/images/phonepe.svg")
Website(args.use_mysql)