akazmi commited on
Commit
3700744
·
verified ·
1 Parent(s): 1de23bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -39
app.py CHANGED
@@ -10,58 +10,56 @@ uploaded_file = st.file_uploader("Upload your Excel file", type=["xlsx"])
10
 
11
  if uploaded_file:
12
  # Load Data
13
- data = pd.ExcelFile(uploaded_file)
14
- df = pd.read_excel(data, sheet_name=0)
15
 
16
  # Data Preprocessing
17
  df['INVOICE_DATE'] = pd.to_datetime(df['INVOICE_DATE'])
18
  df['YEAR'] = df['INVOICE_DATE'].dt.year
19
  df['MONTH'] = df['INVOICE_DATE'].dt.month
20
 
21
- # Filters
22
- year = st.sidebar.selectbox('Select Year:', sorted(df['YEAR'].unique()))
23
- month = st.sidebar.multiselect('Select Month(s):', sorted(df['MONTH'].unique()))
24
- region = st.sidebar.multiselect('Select Region(s):', df['REIGON'].unique())
25
- category = st.sidebar.multiselect('Select Category:', df['PRODUCT_CATEGORY'].dropna().unique())
 
26
 
27
  # Filter Data
28
- filtered_df = df[(df['YEAR'] == year)]
29
- if month:
30
- filtered_df = filtered_df[filtered_df['MONTH'].isin(month)]
31
- if region:
32
- filtered_df = filtered_df[filtered_df['REIGON'].isin(region)]
33
- if category:
34
- filtered_df = filtered_df[filtered_df['PRODUCT_CATEGORY'].isin(category)]
 
35
 
36
- # KPIs
37
- total_sales = filtered_df['NSV'].sum()
38
- total_quantity = filtered_df['QTY'].sum()
39
- top_product = filtered_df.groupby('DESCRIPTION')['NSV'].sum().idxmax()
40
-
41
- st.metric("Total Sales (NSV)", f"${total_sales:,.2f}")
42
- st.metric("Total Quantity Sold", f"{total_quantity:,}")
43
- st.metric("Top-Selling Product", top_product)
44
 
45
  # Visualizations
46
- st.subheader("Sales Trend Over Time")
47
- trend = filtered_df.groupby(['INVOICE_DATE'])['NSV'].sum().reset_index()
48
- fig_trend = px.line(trend, x='INVOICE_DATE', y='NSV', title='Sales Over Time')
49
- st.plotly_chart(fig_trend)
50
-
51
- st.subheader("Top 10 Products by Sales")
52
- top_products = filtered_df.groupby('DESCRIPTION')['NSV'].sum().nlargest(10).reset_index()
53
- fig_top_products = px.bar(top_products, x='DESCRIPTION', y='NSV', title='Top 10 Products')
54
- st.plotly_chart(fig_top_products)
55
 
56
- st.subheader("Sales Distribution by Region")
57
- region_sales = filtered_df.groupby('REIGON')['NSV'].sum().reset_index()
58
- fig_region = px.pie(region_sales, names='REIGON', values='NSV', title='Sales by Region')
59
- st.plotly_chart(fig_region)
60
 
61
- st.subheader("Monthly Sales Heatmap")
62
- heatmap_data = filtered_df.pivot_table(values='NSV', index='MONTH', columns='YEAR', aggfunc='sum')
63
- fig_heatmap = px.imshow(heatmap_data, labels=dict(x="Year", y="Month", color="Sales"))
64
- st.plotly_chart(fig_heatmap)
65
 
 
 
 
 
 
 
66
  else:
67
  st.write("Upload a file to begin.")
 
10
 
11
  if uploaded_file:
12
  # Load Data
13
+ df = pd.read_excel(uploaded_file, sheet_name=0)
 
14
 
15
  # Data Preprocessing
16
  df['INVOICE_DATE'] = pd.to_datetime(df['INVOICE_DATE'])
17
  df['YEAR'] = df['INVOICE_DATE'].dt.year
18
  df['MONTH'] = df['INVOICE_DATE'].dt.month
19
 
20
+ # User Inputs
21
+ st.sidebar.header("Filter Options")
22
+ product_query = st.sidebar.text_input('Product Name:')
23
+ customer_query = st.sidebar.text_input('Customer Name:')
24
+ start_date = st.sidebar.date_input('Start Date:', df['INVOICE_DATE'].min())
25
+ end_date = st.sidebar.date_input('End Date:', df['INVOICE_DATE'].max())
26
 
27
  # Filter Data
28
+ filtered_df = df[
29
+ (df['INVOICE_DATE'] >= pd.to_datetime(start_date)) &
30
+ (df['INVOICE_DATE'] <= pd.to_datetime(end_date))
31
+ ]
32
+ if product_query:
33
+ filtered_df = filtered_df[filtered_df['DESCRIPTION'].str.contains(product_query, case=False, na=False)]
34
+ if customer_query:
35
+ filtered_df = filtered_df[filtered_df['CUSTOMER_NAME'].str.contains(customer_query, case=False, na=False)]
36
 
37
+ # Display Filtered Data
38
+ st.write(f"Filtered Data: {len(filtered_df)} records found.")
39
+ st.dataframe(filtered_df)
 
 
 
 
 
40
 
41
  # Visualizations
42
+ if not filtered_df.empty:
43
+ st.subheader("Sales Trend Over Time")
44
+ trend = filtered_df.groupby('INVOICE_DATE')['NSV'].sum().reset_index()
45
+ fig_trend = px.line(trend, x='INVOICE_DATE', y='NSV', title='Sales Over Time')
46
+ st.plotly_chart(fig_trend)
 
 
 
 
47
 
48
+ st.subheader("Top 10 Products by Sales")
49
+ top_products = filtered_df.groupby('DESCRIPTION')['NSV'].sum().nlargest(10).reset_index()
50
+ fig_top_products = px.bar(top_products, x='DESCRIPTION', y='NSV', title='Top 10 Products')
51
+ st.plotly_chart(fig_top_products)
52
 
53
+ st.subheader("Sales Distribution by Region")
54
+ region_sales = filtered_df.groupby('REIGON')['NSV'].sum().reset_index()
55
+ fig_region = px.pie(region_sales, names='REIGON', values='NSV', title='Sales by Region')
56
+ st.plotly_chart(fig_region)
57
 
58
+ st.subheader("Monthly Sales Heatmap")
59
+ heatmap_data = filtered_df.pivot_table(values='NSV', index='MONTH', columns='YEAR', aggfunc='sum')
60
+ fig_heatmap = px.imshow(heatmap_data, labels=dict(x="Year", y="Month", color="Sales"))
61
+ st.plotly_chart(fig_heatmap)
62
+ else:
63
+ st.write("No data available for the selected filters.")
64
  else:
65
  st.write("Upload a file to begin.")