Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 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 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
# Filter Data
|
| 28 |
-
filtered_df = df[
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 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 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 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 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 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.")
|