Spaces:
Sleeping
Sleeping
| 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 * | |
| #Creating session variables | |
| st.set_page_config(page_title="Dashboard",page_icon="π",layout="wide") | |
| st.header("Data Analytics, KPI, Trends & Predictions") | |
| df = pd.DataFrame() | |
| uploaded_file = st.file_uploader( | |
| "Choose a CSV file. This should be in long format (one datapoint per row).", | |
| type="csv", | |
| ) | |
| if uploaded_file is not None: | |
| df = pd.read_csv(uploaded_file) | |
| else: | |
| #load excel file | comment this line when you fetch data from mysql | |
| df=pd.read_excel('data.xlsx', sheet_name='Sheet1')# load Style css | |
| with open('style.css')as f: | |
| st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html = True) | |
| if "openai_key" not in st.session_state: | |
| with st.form("API key"): | |
| key = st.text_input("OpenAI Key", value="", type="password") | |
| if st.form_submit_button("Submit"): | |
| st.session_state.openai_key = key | |
| st.success('Saved API key for this session.') | |
| #all graphs we use custom css not streamlit | |
| theme_plotly = None | |
| #uncomment these two lines if you fetch data from mysql | |
| #result = view_all_data() | |
| #df=pd.DataFrame(result,columns=["StaffNum","Engage","Location","State","Region","Staff","Job","BusinessType","Absentee","Training","Rating","id"]) | |
| #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 BUSINESS TYPE", | |
| options=df["BusinessType"].unique(), | |
| default=df["BusinessType"].unique(), | |
| ) | |
| df_selection=df.query( | |
| "Region==@region & Location==@location" | |
| ) | |
| #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=["StaffNum","Engage","Location","State","Region","Salary","Job","BusinessType","Absentee","Training","Rating"]) | |
| st.dataframe(df_selection[showData],use_container_width=True) | |
| #compute top analytics | |
| total_salary = float(pd.Series(df_selection['Salary']).sum()) | |
| salary_mode = float(pd.Series(df_selection['Salary']).mode()) | |
| salary_mean = float(pd.Series(df_selection['Salary']).mean()) | |
| salary_median= float(pd.Series(df_selection['Salary']).median()) | |
| rating = float(pd.Series(df_selection['Rating']).sum()) | |
| total1,total2,total3,total4,total5=st.columns(5,gap='small') | |
| with total1: | |
| st.info('Sum Salary',icon="π°") | |
| st.metric(label="Sum TZS",value=f"{total_salary:,.0f}") | |
| with total2: | |
| st.info('Most Salary',icon="π°") | |
| st.metric(label="Mode TZS",value=f"{salary_mode:,.0f}") | |
| with total3: | |
| st.info('Average Salary',icon="π°") | |
| st.metric(label="Average TZS",value=f"{salary_mean:,.0f}") | |
| with total4: | |
| st.info('Central Earnings',icon="π°") | |
| st.metric(label="Median TZS",value=f"{salary_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("DISTRIBUTIONS BY FREQUENCY"): | |
| df.hist(figsize=(16,8),color='#898784', zorder=2, rwidth=0.9,legend = ['Salary']); | |
| st.pyplot() | |
| #graphs | |
| def graphs(): | |
| #total_salary=int(df_selection["Salary"]).sum() | |
| #averageRating=int(round(df_selection["Rating"]).mean(),2) | |
| #simple bar graph salary by business type | |
| salary_by_business_type=( | |
| df_selection.groupby(by=["BusinessType"]).count()[["Salary"]].sort_values(by="Salary") | |
| ) | |
| fig_investment=px.bar( | |
| salary_by_business_type, | |
| x="Salary", | |
| y=salary_by_business_type.index, | |
| orientation="h", | |
| title="<b> Total Salary BY BUSINESS TYPE </b>", | |
| color_discrete_sequence=["#0083B8"]*len(salary_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()[["Salary"]] | |
| fig_state=px.line( | |
| investment_state, | |
| x=investment_state.index, | |
| y="Salary", | |
| orientation="v", | |
| title="<b> Total Salary 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["Salary"].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 Distribution Tredns 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 Salary"), | |
| 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> | |
| """ |