Spaces:
Sleeping
Sleeping
Milind Kamat
commited on
Commit
·
3dba339
1
Parent(s):
5eb869a
2024 Dec 30 : version app updated
Browse filesSigned-off-by: Milind Kamat <36366961+milindkamat0507@users.noreply.github.com>
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
-
import plotly.express as px
|
| 5 |
import plotly.graph_objects as go
|
| 6 |
from datetime import datetime, timedelta
|
| 7 |
|
|
@@ -25,243 +24,138 @@ def generate_sales_data():
|
|
| 25 |
# Main Navigation
|
| 26 |
st.title("📊 Business Analytics & Data Analysis Tutorial")
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
"Data Analysis",
|
| 31 |
-
"Financial Metrics",
|
| 32 |
-
"Practice Zone"
|
| 33 |
-
])
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
st.
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
"Select Date Range",
|
| 54 |
-
value=(df_sales['Date'].min(), df_sales['Date'].max())
|
| 55 |
-
)
|
| 56 |
-
with col3:
|
| 57 |
-
product_type = st.selectbox(
|
| 58 |
-
"Select Product",
|
| 59 |
-
['All'] + list(df_sales['Product'].unique())
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
# Filter data based on selections
|
| 63 |
-
mask = (df_sales['Region'].isin(selected_region)) & \
|
| 64 |
-
(df_sales['Date'] >= pd.Timestamp(date_range[0])) & \
|
| 65 |
-
(df_sales['Date'] <= pd.Timestamp(date_range[1]))
|
| 66 |
-
|
| 67 |
-
if product_type != 'All':
|
| 68 |
-
mask &= (df_sales['Product'] == product_type)
|
| 69 |
-
|
| 70 |
-
filtered_df = df_sales[mask]
|
| 71 |
-
|
| 72 |
-
# KPI Metrics
|
| 73 |
-
st.subheader("Key Performance Indicators")
|
| 74 |
-
kpi1, kpi2, kpi3, kpi4 = st.columns(4)
|
| 75 |
-
|
| 76 |
-
with kpi1:
|
| 77 |
-
st.metric(
|
| 78 |
-
"Total Sales",
|
| 79 |
-
f"${filtered_df['Sales'].sum():,.0f}",
|
| 80 |
-
f"{((filtered_df['Sales'].sum() / df_sales['Sales'].sum()) - 1) * 100:.1f}%"
|
| 81 |
-
)
|
| 82 |
-
with kpi2:
|
| 83 |
-
st.metric(
|
| 84 |
-
"Average Daily Sales",
|
| 85 |
-
f"${filtered_df['Sales'].mean():,.0f}",
|
| 86 |
-
f"{((filtered_df['Sales'].mean() / df_sales['Sales'].mean()) - 1) * 100:.1f}%"
|
| 87 |
-
)
|
| 88 |
-
with kpi3:
|
| 89 |
-
st.metric(
|
| 90 |
-
"Total Profit",
|
| 91 |
-
f"${filtered_df['Profit'].sum():,.0f}",
|
| 92 |
-
f"{((filtered_df['Profit'].sum() / df_sales['Profit'].sum()) - 1) * 100:.1f}%"
|
| 93 |
-
)
|
| 94 |
-
with kpi4:
|
| 95 |
-
st.metric(
|
| 96 |
-
"Profit Margin",
|
| 97 |
-
f"{(filtered_df['Profit'].sum() / filtered_df['Sales'].sum() * 100):.1f}%"
|
| 98 |
-
)
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
fig = go.Figure()
|
| 105 |
-
fig.add_trace(go.Scatter(
|
| 106 |
-
x=daily_sales['Date'],
|
| 107 |
-
y=daily_sales['Sales'],
|
| 108 |
-
name='Sales',
|
| 109 |
-
line=dict(color='blue')
|
| 110 |
-
))
|
| 111 |
-
fig.add_trace(go.Scatter(
|
| 112 |
-
x=daily_sales['Date'],
|
| 113 |
-
y=daily_sales['Profit'],
|
| 114 |
-
name='Profit',
|
| 115 |
-
line=dict(color='green')
|
| 116 |
-
))
|
| 117 |
-
fig.update_layout(title='Daily Sales and Profit Trends')
|
| 118 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
)
|
| 128 |
-
|
| 129 |
-
if analysis_type == "Time Series Analysis":
|
| 130 |
-
st.subheader("Time Series Decomposition")
|
| 131 |
-
# Monthly aggregation
|
| 132 |
-
monthly_sales = df_sales.groupby(df_sales['Date'].dt.to_period('M')).agg({
|
| 133 |
-
'Sales': 'sum',
|
| 134 |
-
'Profit': 'sum'
|
| 135 |
-
}).reset_index()
|
| 136 |
-
|
| 137 |
-
# Rolling averages
|
| 138 |
-
window_size = st.slider("Moving Average Window (months)", 1, 12, 3)
|
| 139 |
-
monthly_sales['Sales_MA'] = monthly_sales['Sales'].rolling(window=window_size).mean()
|
| 140 |
-
|
| 141 |
-
fig = px.line(monthly_sales,
|
| 142 |
-
x='Date',
|
| 143 |
-
y=['Sales', 'Sales_MA'],
|
| 144 |
-
title=f'Monthly Sales with {window_size}-Month Moving Average')
|
| 145 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
regional_sales = df_sales.groupby('Region').agg({
|
| 151 |
-
'Sales': 'sum',
|
| 152 |
-
'Profit': 'sum'
|
| 153 |
-
}).reset_index()
|
| 154 |
-
|
| 155 |
-
fig = px.scatter(regional_sales,
|
| 156 |
-
x='Sales',
|
| 157 |
-
y='Profit',
|
| 158 |
-
size='Sales',
|
| 159 |
-
color='Region',
|
| 160 |
-
title='Regional Sales vs Profit')
|
| 161 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 162 |
|
| 163 |
-
#
|
| 164 |
-
|
| 165 |
-
st.header("Financial Analysis Tools")
|
| 166 |
-
|
| 167 |
-
# ROI Calculator
|
| 168 |
-
st.subheader("ROI Calculator")
|
| 169 |
-
col1, col2 = st.columns(2)
|
| 170 |
-
with col1:
|
| 171 |
-
investment = st.number_input("Initial Investment ($)", min_value=0, value=10000)
|
| 172 |
-
revenue = st.number_input("Expected Revenue ($)", min_value=0, value=15000)
|
| 173 |
-
with col2:
|
| 174 |
-
costs = st.number_input("Operating Costs ($)", min_value=0, value=5000)
|
| 175 |
-
time_period = st.number_input("Time Period (years)", min_value=1, value=1)
|
| 176 |
-
|
| 177 |
-
roi = ((revenue - costs - investment) / investment) * 100
|
| 178 |
-
st.metric("ROI (%)", f"{roi:.1f}%")
|
| 179 |
-
|
| 180 |
-
# Break-even Analysis
|
| 181 |
-
st.subheader("Break-even Analysis")
|
| 182 |
-
fixed_costs = st.number_input("Fixed Costs ($)", min_value=0, value=50000)
|
| 183 |
-
unit_price = st.number_input("Unit Price ($)", min_value=0, value=100)
|
| 184 |
-
unit_cost = st.number_input("Unit Variable Cost ($)", min_value=0, value=60)
|
| 185 |
-
|
| 186 |
-
break_even_units = fixed_costs / (unit_price - unit_cost)
|
| 187 |
-
st.metric("Break-even Point (units)", f"{break_even_units:,.0f}")
|
| 188 |
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
)
|
| 197 |
-
|
| 198 |
-
if exercise == "Sales Dashboard":
|
| 199 |
-
st.markdown("""
|
| 200 |
-
### Exercise: Create a Sales Dashboard
|
| 201 |
-
|
| 202 |
-
Create a dashboard that shows:
|
| 203 |
-
1. Total sales by region
|
| 204 |
-
2. Monthly sales trend
|
| 205 |
-
3. Top-selling products
|
| 206 |
-
|
| 207 |
-
```python
|
| 208 |
-
# Sample code to get started
|
| 209 |
-
import streamlit as st
|
| 210 |
-
import pandas as pd
|
| 211 |
-
|
| 212 |
-
# Load your data
|
| 213 |
-
df = pd.DataFrame({
|
| 214 |
-
'Date': dates,
|
| 215 |
-
'Sales': values,
|
| 216 |
-
'Region': regions
|
| 217 |
-
})
|
| 218 |
-
|
| 219 |
-
# Create visualizations
|
| 220 |
-
st.line_chart(df.set_index('Date')['Sales'])
|
| 221 |
-
```
|
| 222 |
-
""")
|
| 223 |
-
|
| 224 |
-
# Code input and execution area
|
| 225 |
-
st.subheader("Your Code")
|
| 226 |
-
user_code = st.text_area("Write your code here:", height=200)
|
| 227 |
-
if st.button("Run Analysis"):
|
| 228 |
-
try:
|
| 229 |
-
with st.container(border=True):
|
| 230 |
-
exec(user_code)
|
| 231 |
-
except Exception as e:
|
| 232 |
-
st.error(f"Error: {str(e)}")
|
| 233 |
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
|
| 260 |
-
# Footer
|
| 261 |
st.markdown("---")
|
| 262 |
-
st.markdown(""
|
| 263 |
-
<div style='text-align: center'>
|
| 264 |
-
<p>📈 For more business analytics resources, visit our documentation</p>
|
| 265 |
-
<p>Last updated: {}</p>
|
| 266 |
-
</div>
|
| 267 |
-
""".format(datetime.now().strftime("%Y-%m-%d")), unsafe_allow_html=True)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
import plotly.graph_objects as go
|
| 5 |
from datetime import datetime, timedelta
|
| 6 |
|
|
|
|
| 24 |
# Main Navigation
|
| 25 |
st.title("📊 Business Analytics & Data Analysis Tutorial")
|
| 26 |
|
| 27 |
+
# Generate sample data
|
| 28 |
+
df_sales = generate_sales_data()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# Dashboard filters
|
| 31 |
+
col1, col2, col3 = st.columns(3)
|
| 32 |
+
with col1:
|
| 33 |
+
selected_region = st.multiselect(
|
| 34 |
+
"Select Region",
|
| 35 |
+
df_sales['Region'].unique(),
|
| 36 |
+
default=df_sales['Region'].unique()[0]
|
| 37 |
+
)
|
| 38 |
+
with col2:
|
| 39 |
+
date_range = st.date_input(
|
| 40 |
+
"Select Date Range",
|
| 41 |
+
value=(df_sales['Date'].min(), df_sales['Date'].max())
|
| 42 |
+
)
|
| 43 |
+
with col3:
|
| 44 |
+
product_type = st.selectbox(
|
| 45 |
+
"Select Product",
|
| 46 |
+
['All'] + list(df_sales['Product'].unique())
|
| 47 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
# Filter data based on selections
|
| 50 |
+
mask = (df_sales['Region'].isin(selected_region)) & \
|
| 51 |
+
(df_sales['Date'] >= pd.Timestamp(date_range[0])) & \
|
| 52 |
+
(df_sales['Date'] <= pd.Timestamp(date_range[1]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
if product_type != 'All':
|
| 55 |
+
mask &= (df_sales['Product'] == product_type)
|
| 56 |
+
|
| 57 |
+
filtered_df = df_sales[mask]
|
| 58 |
+
|
| 59 |
+
# KPI Metrics
|
| 60 |
+
st.subheader("Key Performance Indicators")
|
| 61 |
+
kpi1, kpi2, kpi3, kpi4 = st.columns(4)
|
| 62 |
+
|
| 63 |
+
with kpi1:
|
| 64 |
+
st.metric(
|
| 65 |
+
"Total Sales",
|
| 66 |
+
f"${filtered_df['Sales'].sum():,.0f}",
|
| 67 |
+
f"{((filtered_df['Sales'].sum() / df_sales['Sales'].sum()) - 1) * 100:.1f}%"
|
| 68 |
+
)
|
| 69 |
+
with kpi2:
|
| 70 |
+
st.metric(
|
| 71 |
+
"Average Daily Sales",
|
| 72 |
+
f"${filtered_df['Sales'].mean():,.0f}",
|
| 73 |
+
f"{((filtered_df['Sales'].mean() / df_sales['Sales'].mean()) - 1) * 100:.1f}%"
|
| 74 |
+
)
|
| 75 |
+
with kpi3:
|
| 76 |
+
st.metric(
|
| 77 |
+
"Total Profit",
|
| 78 |
+
f"${filtered_df['Profit'].sum():,.0f}",
|
| 79 |
+
f"{((filtered_df['Profit'].sum() / df_sales['Profit'].sum()) - 1) * 100:.1f}%"
|
| 80 |
+
)
|
| 81 |
+
with kpi4:
|
| 82 |
+
st.metric(
|
| 83 |
+
"Profit Margin",
|
| 84 |
+
f"{(filtered_df['Profit'].sum() / filtered_df['Sales'].sum() * 100):.1f}%"
|
| 85 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
+
# Sales Trends
|
| 88 |
+
st.subheader("Sales Trends Analysis")
|
| 89 |
+
daily_sales = filtered_df.groupby('Date')[['Sales', 'Profit']].sum().reset_index()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
# Create the figure ensuring dates are in datetime format
|
| 92 |
+
fig = go.Figure()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
fig.add_trace(go.Scatter(
|
| 95 |
+
x=daily_sales['Date'].dt.strftime('%Y-%m-%d'), # Convert to string format
|
| 96 |
+
y=daily_sales['Sales'],
|
| 97 |
+
name='Sales',
|
| 98 |
+
line=dict(color='blue')
|
| 99 |
+
))
|
| 100 |
+
|
| 101 |
+
fig.add_trace(go.Scatter(
|
| 102 |
+
x=daily_sales['Date'].dt.strftime('%Y-%m-%d'), # Convert to string format
|
| 103 |
+
y=daily_sales['Profit'],
|
| 104 |
+
name='Profit',
|
| 105 |
+
line=dict(color='green')
|
| 106 |
+
))
|
| 107 |
+
|
| 108 |
+
fig.update_layout(
|
| 109 |
+
title='Daily Sales and Profit Trends',
|
| 110 |
+
xaxis_title='Date',
|
| 111 |
+
yaxis_title='Amount ($)',
|
| 112 |
+
xaxis=dict(
|
| 113 |
+
type='category', # Use category type for x-axis
|
| 114 |
+
tickangle=45
|
| 115 |
)
|
| 116 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 119 |
+
|
| 120 |
+
# Regional Analysis
|
| 121 |
+
st.subheader("Regional Performance")
|
| 122 |
+
regional_data = filtered_df.groupby('Region').agg({
|
| 123 |
+
'Sales': 'sum',
|
| 124 |
+
'Profit': 'sum'
|
| 125 |
+
}).reset_index()
|
| 126 |
+
|
| 127 |
+
fig_region = go.Figure(data=[
|
| 128 |
+
go.Bar(name='Sales', x=regional_data['Region'], y=regional_data['Sales']),
|
| 129 |
+
go.Bar(name='Profit', x=regional_data['Region'], y=regional_data['Profit'])
|
| 130 |
+
])
|
| 131 |
+
|
| 132 |
+
fig_region.update_layout(
|
| 133 |
+
barmode='group',
|
| 134 |
+
title='Sales and Profit by Region'
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
st.plotly_chart(fig_region, use_container_width=True)
|
| 138 |
+
|
| 139 |
+
# Product Analysis
|
| 140 |
+
if product_type == 'All':
|
| 141 |
+
st.subheader("Product Performance")
|
| 142 |
+
product_data = filtered_df.groupby('Product').agg({
|
| 143 |
+
'Sales': 'sum',
|
| 144 |
+
'Profit': 'sum'
|
| 145 |
+
}).reset_index()
|
| 146 |
+
|
| 147 |
+
fig_product = go.Figure(data=[
|
| 148 |
+
go.Bar(name='Sales', x=product_data['Product'], y=product_data['Sales']),
|
| 149 |
+
go.Bar(name='Profit', x=product_data['Product'], y=product_data['Profit'])
|
| 150 |
+
])
|
| 151 |
+
|
| 152 |
+
fig_product.update_layout(
|
| 153 |
+
barmode='group',
|
| 154 |
+
title='Sales and Profit by Product'
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
st.plotly_chart(fig_product, use_container_width=True)
|
| 158 |
|
| 159 |
+
# Footer
|
| 160 |
st.markdown("---")
|
| 161 |
+
st.markdown(f"Dashboard last updated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|