Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import plotly.express as px
|
| 4 |
+
|
| 5 |
+
# Title of the App
|
| 6 |
+
st.title('Sales Data Visualization App')
|
| 7 |
+
|
| 8 |
+
# Upload File
|
| 9 |
+
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.")
|