1
+ import pickle
2
+ from pathlib import Path
3
+
4
+ import pandas as pd # pip install pandas openpyxl
5
+ import plotly .express as px # pip install plotly-express
6
+ import streamlit as st # pip install streamlit
7
+
8
+
9
+
10
+ # emojis: https://www.webfx.com/tools/emoji-cheat-sheet/
11
+ st .set_page_config (page_title = "Sales Dashboard" , page_icon = ":bar_chart:" , layout = "wide" )
12
+
13
+
14
+
15
+ def get_data_from_excel ():
16
+ df = pd .read_excel (
17
+ io = "pages/supermarkt_sales.xlsx" ,
18
+ engine = "openpyxl" ,
19
+ sheet_name = "Sales" ,
20
+ skiprows = 3 ,
21
+ usecols = "B:R" ,
22
+ nrows = 1000 ,
23
+ )
24
+ # Add 'hour' column to dataframe
25
+ df ["hour" ] = pd .to_datetime (df ["Time" ], format = "%H:%M:%S" ).dt .hour
26
+ return df
27
+
28
+ df = get_data_from_excel ()
29
+
30
+ # ---- SIDEBAR ----
31
+ # authenticator.logout("Logout", "sidebar")
32
+ name = 'bharath'
33
+ st .sidebar .title (f"Welcome { name } " )
34
+ st .sidebar .header ("Please Filter Here:" )
35
+ city = st .sidebar .multiselect (
36
+ "Select the City:" ,
37
+ options = df ["City" ].unique (),
38
+ default = df ["City" ].unique ()
39
+ )
40
+
41
+ customer_type = st .sidebar .multiselect (
42
+ "Select the Customer Type:" ,
43
+ options = df ["Customer_type" ].unique (),
44
+ default = df ["Customer_type" ].unique (),
45
+ )
46
+
47
+ gender = st .sidebar .multiselect (
48
+ "Select the Gender:" ,
49
+ options = df ["Gender" ].unique (),
50
+ default = df ["Gender" ].unique ()
51
+ )
52
+
53
+ df_selection = df .query (
54
+ "City == @city & Customer_type ==@customer_type & Gender == @gender"
55
+ )
56
+
57
+ # ---- MAINPAGE ----
58
+ st .title (":bar_chart: Sales Dashboard" )
59
+ st .markdown ("##" )
60
+
61
+ # TOP KPI's
62
+ total_sales = int (df_selection ["Total" ].sum ())
63
+ average_rating = round (df_selection ["Rating" ].mean (), 1 )
64
+ star_rating = ":star:" * int (round (average_rating , 0 ))
65
+ average_sale_by_transaction = round (df_selection ["Total" ].mean (), 2 )
66
+
67
+ left_column , middle_column , right_column = st .columns (3 )
68
+ with left_column :
69
+ st .subheader ("Total Sales:" )
70
+ st .subheader (f"US $ { total_sales :,} " )
71
+ with middle_column :
72
+ st .subheader ("Average Rating:" )
73
+ st .subheader (f"{ average_rating } { star_rating } " )
74
+ with right_column :
75
+ st .subheader ("Average Sales Per Transaction:" )
76
+ st .subheader (f"US $ { average_sale_by_transaction } " )
77
+
78
+ st .markdown ("""---""" )
79
+
80
+ # SALES BY PRODUCT LINE [BAR CHART]
81
+ sales_by_product_line = (
82
+ df_selection .groupby (by = ["Product line" ]).sum ()[["Total" ]].sort_values (by = "Total" )
83
+ )
84
+ fig_product_sales = px .bar (
85
+ sales_by_product_line ,
86
+ x = "Total" ,
87
+ y = sales_by_product_line .index ,
88
+ orientation = "h" ,
89
+ title = "<b>Sales by Product Line</b>" ,
90
+ color_discrete_sequence = ["#0083B8" ] * len (sales_by_product_line ),
91
+ template = "plotly_white" ,
92
+ )
93
+ fig_product_sales .update_layout (
94
+ plot_bgcolor = "rgba(0,0,0,0)" ,
95
+ xaxis = (dict (showgrid = False ))
96
+ )
97
+
98
+ # SALES BY HOUR [BAR CHART]
99
+ sales_by_hour = df_selection .groupby (by = ["hour" ]).sum ()[["Total" ]]
100
+ fig_hourly_sales = px .bar (
101
+ sales_by_hour ,
102
+ x = sales_by_hour .index ,
103
+ y = "Total" ,
104
+ title = "<b>Sales by hour</b>" ,
105
+ color_discrete_sequence = ["#0083B8" ] * len (sales_by_hour ),
106
+ template = "plotly_white" ,
107
+ )
108
+ fig_hourly_sales .update_layout (
109
+ xaxis = dict (tickmode = "linear" ),
110
+ plot_bgcolor = "rgba(0,0,0,0)" ,
111
+ yaxis = (dict (showgrid = False )),
112
+ )
113
+
114
+
115
+ left_column , right_column = st .columns (2 )
116
+ left_column .plotly_chart (fig_hourly_sales , use_container_width = True )
117
+ right_column .plotly_chart (fig_product_sales , use_container_width = True )
118
+
119
+
120
+ # ---- HIDE STREAMLIT STYLE ----
121
+ hide_st_style = """
122
+ <style>
123
+ #MainMenu {visibility: hidden;}
124
+ footer {visibility: hidden;}
125
+ header {visibility: hidden;}
126
+ </style>
127
+ """
128
+ st .markdown (hide_st_style , unsafe_allow_html = True )
0 commit comments