Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,131 +1,149 @@
|
|
| 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 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
df['
|
| 61 |
-
df['
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
filtered_df =
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
st.
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
else:
|
| 131 |
-
st.write("
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import plotly.express as px
|
| 4 |
+
import plotly.graph_objects as go
|
| 5 |
+
import random
|
| 6 |
+
from datetime import datetime, timedelta
|
| 7 |
|
| 8 |
# Title of the App
|
| 9 |
st.title('Sales Data Visualization App')
|
| 10 |
|
| 11 |
+
# Sample Data Generation (You can remove this part when uploading your own file)
|
| 12 |
+
def generate_sample_data():
|
| 13 |
+
customers = ['Customer A', 'Customer B', 'Customer C', 'Customer D']
|
| 14 |
+
products = ['Product 1', 'Product 2', 'Product 3', 'Product 4']
|
| 15 |
+
cities = ['City 1', 'City 2', 'City 3', 'City 4']
|
| 16 |
+
states = ['State 1', 'State 2', 'State 3', 'State 4']
|
| 17 |
+
countries = ['Country 1', 'Country 2', 'Country 3', 'Country 4']
|
| 18 |
+
|
| 19 |
+
# Generate sample sales data for 100 records
|
| 20 |
+
data = []
|
| 21 |
+
for i in range(100):
|
| 22 |
+
customer = random.choice(customers)
|
| 23 |
+
product = random.choice(products)
|
| 24 |
+
city = random.choice(cities)
|
| 25 |
+
state = random.choice(states)
|
| 26 |
+
country = random.choice(countries)
|
| 27 |
+
date = datetime.today() - timedelta(days=random.randint(1, 365))
|
| 28 |
+
nsv = random.randint(1000, 10000) # Net Sales Value
|
| 29 |
+
cost = random.randint(500, 7000) # Cost
|
| 30 |
+
data.append([customer, product, city, state, country, date, nsv, cost])
|
| 31 |
+
|
| 32 |
+
columns = ['Customer Name', 'Product Name', 'City', 'State', 'Country', 'Date', 'Net Sales Value', 'Cost']
|
| 33 |
+
return pd.DataFrame(data, columns=columns)
|
| 34 |
+
|
| 35 |
+
# Load sample data
|
| 36 |
+
df = generate_sample_data()
|
| 37 |
+
|
| 38 |
+
# Display the first few rows of the dataframe to understand its structure
|
| 39 |
+
st.write("Preview of the sample data:")
|
| 40 |
+
st.dataframe(df.head())
|
| 41 |
+
|
| 42 |
+
# Data Preprocessing
|
| 43 |
+
df['Date'] = pd.to_datetime(df['Date'])
|
| 44 |
+
df['Year'] = df['Date'].dt.year
|
| 45 |
+
df['Month'] = df['Date'].dt.month
|
| 46 |
+
|
| 47 |
+
# Sidebar Filters
|
| 48 |
+
st.sidebar.header("Filter Options")
|
| 49 |
+
|
| 50 |
+
# Text Input for Customer and Product
|
| 51 |
+
customer_query = st.sidebar.text_input('Enter Customer Name (partial or full):').strip().lower()
|
| 52 |
+
product_query = st.sidebar.text_input('Enter Product Name (partial or full):').strip().lower()
|
| 53 |
+
city_query = st.sidebar.text_input('Enter City (partial or full):').strip().lower()
|
| 54 |
+
state_query = st.sidebar.text_input('Enter State (partial or full):').strip().lower()
|
| 55 |
+
country_query = st.sidebar.text_input('Enter Country (partial or full):').strip().lower()
|
| 56 |
+
|
| 57 |
+
# Date Range Selection
|
| 58 |
+
start_date = st.sidebar.date_input('Start Date:', df['Date'].min())
|
| 59 |
+
end_date = st.sidebar.date_input('End Date:', df['Date'].max())
|
| 60 |
+
|
| 61 |
+
# Filter Data by Date Range
|
| 62 |
+
filtered_df = df[
|
| 63 |
+
(df['Date'] >= pd.to_datetime(start_date)) &
|
| 64 |
+
(df['Date'] <= pd.to_datetime(end_date))
|
| 65 |
+
]
|
| 66 |
+
|
| 67 |
+
# Filter Data by Customer Name
|
| 68 |
+
if customer_query:
|
| 69 |
+
filtered_df = filtered_df[filtered_df['Customer Name'].str.contains(customer_query, case=False, na=False)]
|
| 70 |
+
|
| 71 |
+
# Filter Data by Product Name
|
| 72 |
+
if product_query:
|
| 73 |
+
filtered_df = filtered_df[filtered_df['Product Name'].str.contains(product_query, case=False, na=False)]
|
| 74 |
+
|
| 75 |
+
# Filter Data by City
|
| 76 |
+
if city_query:
|
| 77 |
+
filtered_df = filtered_df[filtered_df['City'].str.contains(city_query, case=False, na=False)]
|
| 78 |
+
|
| 79 |
+
# Filter Data by State
|
| 80 |
+
if state_query:
|
| 81 |
+
filtered_df = filtered_df[filtered_df['State'].str.contains(state_query, case=False, na=False)]
|
| 82 |
+
|
| 83 |
+
# Filter Data by Country
|
| 84 |
+
if country_query:
|
| 85 |
+
filtered_df = filtered_df[filtered_df['Country'].str.contains(country_query, case=False, na=False)]
|
| 86 |
+
|
| 87 |
+
# Display Filtered Data
|
| 88 |
+
st.write(f"Filtered Data: {len(filtered_df)} records found.")
|
| 89 |
+
st.dataframe(filtered_df)
|
| 90 |
+
|
| 91 |
+
if not filtered_df.empty:
|
| 92 |
+
# KPI Metrics
|
| 93 |
+
st.subheader("Key Financial Metrics")
|
| 94 |
+
|
| 95 |
+
# Profit for the Year (Calculated as Net Sales Value - Cost)
|
| 96 |
+
profit_for_the_year = filtered_df['Net Sales Value'] - filtered_df['Cost']
|
| 97 |
+
st.metric("Profit for the Year", f"${profit_for_the_year.sum():,.2f}")
|
| 98 |
+
|
| 99 |
+
# Gross Margin (Net Sales Value - Cost)
|
| 100 |
+
gross_margin = filtered_df['Net Sales Value'] - filtered_df['Cost']
|
| 101 |
+
st.metric("Gross Margin", f"${gross_margin.sum():,.2f}")
|
| 102 |
+
|
| 103 |
+
# Total Sales
|
| 104 |
+
total_sales = filtered_df['Net Sales Value'].sum()
|
| 105 |
+
st.metric("Total Sales", f"${total_sales:,.2f}")
|
| 106 |
+
|
| 107 |
+
# Matrix View (like Power BI Matrix)
|
| 108 |
+
st.subheader("Matrix View of Financial Data")
|
| 109 |
+
matrix_data = filtered_df.pivot_table(
|
| 110 |
+
values='Net Sales Value',
|
| 111 |
+
index=['Year', 'Customer Name'],
|
| 112 |
+
columns=['Product Name'],
|
| 113 |
+
aggfunc='sum',
|
| 114 |
+
fill_value=0
|
| 115 |
+
)
|
| 116 |
+
st.dataframe(matrix_data)
|
| 117 |
+
|
| 118 |
+
# Visualization 1: Sales by Customer (Bar chart)
|
| 119 |
+
st.subheader("Sales by Customer")
|
| 120 |
+
sales_by_customer = filtered_df.groupby('Customer Name')['Net Sales Value'].sum().reset_index()
|
| 121 |
+
fig_customer = px.bar(sales_by_customer, x='Customer Name', y='Net Sales Value', title='Sales by Customer')
|
| 122 |
+
st.plotly_chart(fig_customer)
|
| 123 |
+
|
| 124 |
+
# Visualization 2: Sales by Product (Bar chart)
|
| 125 |
+
st.subheader("Sales by Product")
|
| 126 |
+
sales_by_product = filtered_df.groupby('Product Name')['Net Sales Value'].sum().reset_index()
|
| 127 |
+
fig_product = px.bar(sales_by_product, x='Product Name', y='Net Sales Value', title='Sales by Product')
|
| 128 |
+
st.plotly_chart(fig_product)
|
| 129 |
+
|
| 130 |
+
# Visualization 3: Year-over-Year Sales Trend (Line chart)
|
| 131 |
+
st.subheader("Year-over-Year Sales Trend")
|
| 132 |
+
year_trend = filtered_df.groupby(['Year'])['Net Sales Value'].sum().reset_index()
|
| 133 |
+
fig_year_trend = px.line(year_trend, x='Year', y='Net Sales Value', title='Year-over-Year Sales Trend')
|
| 134 |
+
st.plotly_chart(fig_year_trend)
|
| 135 |
+
|
| 136 |
+
# Visualization 4: Profit and Loss Overview (Simple Table)
|
| 137 |
+
st.subheader("Profit and Loss Overview")
|
| 138 |
+
pnl_data = filtered_df[['Customer Name', 'Product Name', 'Net Sales Value', 'Cost', 'Net Sales Value - Cost']]
|
| 139 |
+
pnl_data.columns = ['Customer', 'Product', 'Sales', 'Cost', 'Profit']
|
| 140 |
+
st.dataframe(pnl_data)
|
| 141 |
+
|
| 142 |
+
# Visualization 5: Gross Margin Distribution (Pie chart)
|
| 143 |
+
st.subheader("Gross Margin Distribution")
|
| 144 |
+
margin_data = filtered_df.groupby('Product Name')['Net Sales Value'].sum().reset_index()
|
| 145 |
+
fig_margin = px.pie(margin_data, names='Product Name', values='Net Sales Value', title='Gross Margin by Product')
|
| 146 |
+
st.plotly_chart(fig_margin)
|
| 147 |
+
|
| 148 |
else:
|
| 149 |
+
st.write("No data available for the selected filters.")
|