-
Notifications
You must be signed in to change notification settings - Fork 126
/
Home.py
222 lines (182 loc) · 7.89 KB
/
Home.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
import streamlit as st
import pandas as pd
import plotly.express as px
from streamlit_option_menu import option_menu
from numerize.numerize import numerize
import time
from streamlit_extras.metric_cards import style_metric_cards
st.set_option('deprecation.showPyplotGlobalUse', False)
import plotly.graph_objs as go
#uncomment this line if you use mysql
#from query import *
st.set_page_config(page_title="Dashboard",page_icon="🌍",layout="wide")
st.header("ANALYTICS WEB DASHBOARD | INSURANCE KPI & TRENDS ")
#all graphs we use custom css not streamlit
theme_plotly = None
# load Style css
with open('style.css')as f:
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html = True)
#uncomment these two lines if you fetch data from mysql
#result = view_all_data()
#df=pd.DataFrame(result,columns=["Policy","Expiry","Location","State","Region","Investment","Construction","BusinessType","Earthquake","Flood","Rating","id"])
#load excel file | comment this line when you fetch data from mysql
df=pd.read_excel('data.xlsx', sheet_name='Sheet1')
#side bar logo
#switcher
region=st.sidebar.multiselect(
"SELECT REGION",
options=df["Region"].unique(),
default=df["Region"].unique(),
)
location=st.sidebar.multiselect(
"SELECT LOCATION",
options=df["Location"].unique(),
default=df["Location"].unique(),
)
construction=st.sidebar.multiselect(
"SELECT CONSTRUCTION",
options=df["Construction"].unique(),
default=df["Construction"].unique(),
)
df_selection=df.query(
"Region==@region & Location==@location & Construction ==@construction"
)
#this function performs basic descriptive analytics like Mean,Mode,Sum etc
def Home():
with st.expander("VIEW EXCEL DATASET"):
showData=st.multiselect('Filter: ',df_selection.columns,default=["Policy","Expiry","Location","State","Region","Investment","Construction","BusinessType","Earthquake","Flood","Rating"])
st.dataframe(df_selection[showData],use_container_width=True)
#compute top analytics
total_investment = float(pd.Series(df_selection['Investment']).sum())
investment_mode = float(pd.Series(df_selection['Investment']).mode())
investment_mean = float(pd.Series(df_selection['Investment']).mean())
investment_median= float(pd.Series(df_selection['Investment']).median())
rating = float(pd.Series(df_selection['Rating']).sum())
total1,total2,total3,total4,total5=st.columns(5,gap='small')
with total1:
st.info('Total Sum Investment',icon="💰")
st.metric(label="Sum TZS",value=f"{total_investment:,.0f}")
with total2:
st.info('Most Frequent Investment',icon="💰")
st.metric(label="Mode TZS",value=f"{investment_mode:,.0f}")
with total3:
st.info('Average Mean',icon="💰")
st.metric(label="Average TZS",value=f"{investment_mean:,.0f}")
with total4:
st.info('Central Earnings',icon="💰")
st.metric(label="Median TZS",value=f"{investment_median:,.0f}")
with total5:
st.info('Ratings',icon="💰")
st.metric(label="Rating",value=numerize(rating),help=f""" Total Rating: {rating} """)
style_metric_cards(background_color="#FFFFFF",border_left_color="#686664",border_color="#000000",box_shadow="#F71938")
#variable distribution Histogram
with st.expander("EXPLORATORY VARIABLE DISTRIBUTIONS BY FREQUENCY"):
df.hist(figsize=(16,8),color='#898784', zorder=2, rwidth=0.9,legend = ['Investment']);
st.pyplot()
#graphs
def graphs():
#total_investment=int(df_selection["Investment"]).sum()
#averageRating=int(round(df_selection["Rating"]).mean(),2)
#simple bar graph investment by business type
investment_by_business_type=(
df_selection.groupby(by=["BusinessType"]).count()[["Investment"]].sort_values(by="Investment")
)
fig_investment=px.bar(
investment_by_business_type,
x="Investment",
y=investment_by_business_type.index,
orientation="h",
title="<b> INVESTMENT BY BUSINESS TYPE </b>",
color_discrete_sequence=["#0083B8"]*len(investment_by_business_type),
template="plotly_white",
)
fig_investment.update_layout(
plot_bgcolor="rgba(0,0,0,0)",
font=dict(color="black"),
yaxis=dict(showgrid=True, gridcolor='#cecdcd'), # Show y-axis grid and set its color
paper_bgcolor='rgba(0, 0, 0, 0)', # Set paper background color to transparent
xaxis=dict(showgrid=True, gridcolor='#cecdcd'), # Show x-axis grid and set its color
)
#simple line graph investment by state
investment_state=df_selection.groupby(by=["State"]).count()[["Investment"]]
fig_state=px.line(
investment_state,
x=investment_state.index,
y="Investment",
orientation="v",
title="<b> INVESTMENT BY STATE </b>",
color_discrete_sequence=["#0083b8"]*len(investment_state),
template="plotly_white",
)
fig_state.update_layout(
xaxis=dict(tickmode="linear"),
plot_bgcolor="rgba(0,0,0,0)",
yaxis=(dict(showgrid=False))
)
left,right,center=st.columns(3)
left.plotly_chart(fig_state,use_container_width=True)
right.plotly_chart(fig_investment,use_container_width=True)
with center:
#pie chart
fig = px.pie(df_selection, values='Rating', names='State', title='RATINGS BY REGIONS')
fig.update_layout(legend_title="Regions", legend_y=0.9)
fig.update_traces(textinfo='percent+label', textposition='inside')
st.plotly_chart(fig, use_container_width=True, theme=theme_plotly)
#function to show current earnings against expected target
def Progressbar():
st.markdown("""<style>.stProgress > div > div > div > div { background-image: linear-gradient(to right, #99ff99 , #FFFF00)}</style>""",unsafe_allow_html=True,)
target=3000000000
current=df_selection["Investment"].sum()
percent=round((current/target*100))
mybar=st.progress(0)
if percent>100:
st.subheader("Target done !")
else:
st.write("you have ",percent, "% " ,"of ", (format(target, 'd')), "TZS")
for percent_complete in range(percent):
time.sleep(0.1)
mybar.progress(percent_complete+1,text=" Target Percentage")
#menu bar
def sideBar():
with st.sidebar:
selected=option_menu(
menu_title="Main Menu",
options=["Home","Progress"],
icons=["house","eye"],
menu_icon="cast",
default_index=0
)
if selected=="Home":
#st.subheader(f"Page: {selected}")
Home()
graphs()
if selected=="Progress":
#st.subheader(f"Page: {selected}")
Progressbar()
graphs()
sideBar()
st.sidebar.image("data/logo1.png",caption="")
st.subheader('PICK FEATURES TO EXPLORE DISTRIBUTIONS TRENDS BY QUARTILES',)
#feature_x = st.selectbox('Select feature for x Qualitative data', df_selection.select_dtypes("object").columns)
feature_y = st.selectbox('Select feature for y Quantitative Data', df_selection.select_dtypes("number").columns)
fig2 = go.Figure(
data=[go.Box(x=df['BusinessType'], y=df[feature_y])],
layout=go.Layout(
title=go.layout.Title(text="BUSINESS TYPE BY QUARTILES OF INVESTMENT"),
plot_bgcolor='rgba(0, 0, 0, 0)', # Set plot background color to transparent
paper_bgcolor='rgba(0, 0, 0, 0)', # Set paper background color to transparent
xaxis=dict(showgrid=True, gridcolor='#cecdcd'), # Show x-axis grid and set its color
yaxis=dict(showgrid=True, gridcolor='#cecdcd'), # Show y-axis grid and set its color
font=dict(color='#cecdcd'), # Set text color to black
)
)
# Display the Plotly figure using Streamlit
st.plotly_chart(fig2,use_container_width=True)
#theme
hide_st_style="""
<style>
#MainMenu {visibility:hidden;}
footer {visibility:hidden;}
header {visibility:hidden;}
</style>
"""