Spaces:
Sleeping
Sleeping
Milind Kamat
commited on
Commit
·
a97cd9f
1
Parent(s):
3dba339
2024 Dec 30 New streamlit tutorial
Browse filesSigned-off-by: Milind Kamat <36366961+milindkamat0507@users.noreply.github.com>
- app.py +198 -145
- datanalysis.py +161 -0
app.py
CHANGED
|
@@ -1,161 +1,214 @@
|
|
| 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 |
|
| 7 |
-
st.set_page_config(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
dates = pd.date_range(start='2024-01-01', end='2024-12-31', freq='D')
|
| 12 |
-
np.random.seed(42)
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
return df
|
| 23 |
-
|
| 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 |
-
#
|
| 50 |
-
|
| 51 |
-
(df_sales['Date'] >= pd.Timestamp(date_range[0])) & \
|
| 52 |
-
(df_sales['Date'] <= pd.Timestamp(date_range[1]))
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
|
| 57 |
-
|
|
|
|
| 58 |
|
| 59 |
-
#
|
| 60 |
-
st.
|
| 61 |
-
kpi1, kpi2, kpi3, kpi4 = st.columns(4)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
"
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
with
|
| 82 |
-
|
| 83 |
-
"
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
|
| 88 |
-
st.
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
-
# Footer
|
| 160 |
st.markdown("---")
|
| 161 |
-
st.
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
st.set_page_config(page_title="Learn Streamlit", layout="wide")
|
| 6 |
+
|
| 7 |
+
# Main navigation sidebar
|
| 8 |
+
with st.sidebar:
|
| 9 |
+
st.title("Streamlit Tutorial")
|
| 10 |
+
selected_topic = st.radio(
|
| 11 |
+
"Choose a Topic:",
|
| 12 |
+
[
|
| 13 |
+
"1. Basic Text Elements",
|
| 14 |
+
"2. Input Widgets",
|
| 15 |
+
"3. Layouts & Containers",
|
| 16 |
+
"4. Data Display",
|
| 17 |
+
"5. Charts & Plots",
|
| 18 |
+
"6. Interactive Components",
|
| 19 |
+
"Try It Yourself"
|
| 20 |
+
]
|
| 21 |
+
)
|
| 22 |
|
| 23 |
+
if selected_topic == "1. Basic Text Elements":
|
| 24 |
+
st.title("Basic Text Elements in Streamlit")
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# Example & Output side by side
|
| 27 |
+
col1, col2 = st.columns(2)
|
| 28 |
+
|
| 29 |
+
with col1:
|
| 30 |
+
st.header("Code Examples")
|
| 31 |
+
st.code("""
|
| 32 |
+
# Title
|
| 33 |
+
st.title('Main Title')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
# Header
|
| 36 |
+
st.header('Header')
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
# Subheader
|
| 39 |
+
st.subheader('Subheader')
|
| 40 |
|
| 41 |
+
# Normal text
|
| 42 |
+
st.write('Normal text')
|
| 43 |
|
| 44 |
+
# Markdown text
|
| 45 |
+
st.markdown('**Bold** and *italic*')
|
|
|
|
| 46 |
|
| 47 |
+
# Colored text
|
| 48 |
+
st.markdown(':blue[Blue text]')
|
| 49 |
+
""")
|
| 50 |
+
|
| 51 |
+
with col2:
|
| 52 |
+
st.header("Live Output")
|
| 53 |
+
st.title('Main Title')
|
| 54 |
+
st.header('Header')
|
| 55 |
+
st.subheader('Subheader')
|
| 56 |
+
st.write('Normal text')
|
| 57 |
+
st.markdown('**Bold** and *italic*')
|
| 58 |
+
st.markdown(':blue[Blue text]')
|
| 59 |
+
|
| 60 |
+
elif selected_topic == "2. Input Widgets":
|
| 61 |
+
st.title("Input Widgets")
|
| 62 |
+
|
| 63 |
+
col1, col2 = st.columns(2)
|
| 64 |
+
|
| 65 |
+
with col1:
|
| 66 |
+
st.header("Code")
|
| 67 |
+
st.code("""
|
| 68 |
+
# Text input
|
| 69 |
+
name = st.text_input('Enter your name')
|
| 70 |
+
|
| 71 |
+
# Number input
|
| 72 |
+
age = st.number_input('Enter age',
|
| 73 |
+
min_value=0, max_value=120)
|
| 74 |
+
|
| 75 |
+
# Slider
|
| 76 |
+
value = st.slider('Select value',
|
| 77 |
+
0, 100)
|
| 78 |
+
|
| 79 |
+
# Checkbox
|
| 80 |
+
agree = st.checkbox('I agree')
|
| 81 |
+
|
| 82 |
+
# Selectbox
|
| 83 |
+
option = st.selectbox(
|
| 84 |
+
'Choose option',
|
| 85 |
+
['A', 'B', 'C'])
|
| 86 |
+
""")
|
| 87 |
+
|
| 88 |
+
with col2:
|
| 89 |
+
st.header("Try these widgets")
|
| 90 |
+
name = st.text_input('Enter your name')
|
| 91 |
+
if name:
|
| 92 |
+
st.write(f'Hello {name}!')
|
| 93 |
+
|
| 94 |
+
age = st.number_input('Enter age', min_value=0, max_value=120)
|
| 95 |
+
value = st.slider('Select value', 0, 100)
|
| 96 |
+
agree = st.checkbox('I agree')
|
| 97 |
+
option = st.selectbox('Choose option', ['A', 'B', 'C'])
|
| 98 |
+
|
| 99 |
+
elif selected_topic == "3. Layouts & Containers":
|
| 100 |
+
st.title("Layouts & Containers")
|
| 101 |
+
|
| 102 |
+
st.header("1. Columns")
|
| 103 |
+
st.code("col1, col2 = st.columns(2)")
|
| 104 |
+
col1, col2 = st.columns(2)
|
| 105 |
+
with col1:
|
| 106 |
+
st.write("This is column 1")
|
| 107 |
+
with col2:
|
| 108 |
+
st.write("This is column 2")
|
| 109 |
+
|
| 110 |
+
st.header("2. Tabs")
|
| 111 |
+
st.code("tab1, tab2 = st.tabs(['Tab 1', 'Tab 2'])")
|
| 112 |
+
tab1, tab2 = st.tabs(['Tab 1', 'Tab 2'])
|
| 113 |
+
with tab1:
|
| 114 |
+
st.write("Content of tab 1")
|
| 115 |
+
with tab2:
|
| 116 |
+
st.write("Content of tab 2")
|
| 117 |
+
|
| 118 |
+
st.header("3. Expander")
|
| 119 |
+
with st.expander("Click to expand"):
|
| 120 |
+
st.write("Hidden content revealed!")
|
| 121 |
|
| 122 |
+
elif selected_topic == "4. Data Display":
|
| 123 |
+
st.title("Working with Data")
|
| 124 |
+
|
| 125 |
+
# Create sample dataframe
|
| 126 |
+
df = pd.DataFrame({
|
| 127 |
+
'Name': ['John', 'Anna', 'Peter'],
|
| 128 |
+
'Age': [25, 30, 35],
|
| 129 |
+
'City': ['New York', 'Paris', 'London']
|
| 130 |
+
})
|
| 131 |
+
|
| 132 |
+
st.header("Display DataFrame")
|
| 133 |
+
st.code("""
|
| 134 |
+
# Create DataFrame
|
| 135 |
+
df = pd.DataFrame({
|
| 136 |
+
'Name': ['John', 'Anna', 'Peter'],
|
| 137 |
+
'Age': [25, 30, 35],
|
| 138 |
+
'City': ['New York', 'Paris', 'London']
|
| 139 |
+
})
|
| 140 |
+
|
| 141 |
+
# Display as table
|
| 142 |
+
st.dataframe(df)
|
| 143 |
+
|
| 144 |
+
# Display as static table
|
| 145 |
+
st.table(df)
|
| 146 |
+
""")
|
| 147 |
+
|
| 148 |
+
st.dataframe(df)
|
| 149 |
+
st.table(df)
|
| 150 |
+
|
| 151 |
+
elif selected_topic == "5. Charts & Plots":
|
| 152 |
+
st.title("Creating Charts")
|
| 153 |
+
|
| 154 |
+
# Generate sample data
|
| 155 |
+
chart_data = pd.DataFrame(
|
| 156 |
+
np.random.randn(20, 3),
|
| 157 |
+
columns=['A', 'B', 'C']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
)
|
| 159 |
+
|
| 160 |
+
st.header("Line Chart")
|
| 161 |
+
st.code("st.line_chart(chart_data)")
|
| 162 |
+
st.line_chart(chart_data)
|
| 163 |
+
|
| 164 |
+
st.header("Bar Chart")
|
| 165 |
+
st.code("st.bar_chart(chart_data)")
|
| 166 |
+
st.bar_chart(chart_data)
|
| 167 |
+
|
| 168 |
+
st.header("Area Chart")
|
| 169 |
+
st.code("st.area_chart(chart_data)")
|
| 170 |
+
st.area_chart(chart_data)
|
| 171 |
+
|
| 172 |
+
elif selected_topic == "6. Interactive Components":
|
| 173 |
+
st.title("Interactive Components")
|
| 174 |
+
|
| 175 |
+
st.header("Form Example")
|
| 176 |
+
with st.form("my_form"):
|
| 177 |
+
st.write("Inside the form")
|
| 178 |
+
name = st.text_input("Name")
|
| 179 |
+
age = st.slider("Age", 0, 100, 25)
|
| 180 |
+
submitted = st.form_submit_button("Submit")
|
| 181 |
+
if submitted:
|
| 182 |
+
st.write(f"Name: {name}, Age: {age}")
|
| 183 |
+
|
| 184 |
+
st.header("File Uploader")
|
| 185 |
+
uploaded_file = st.file_uploader("Choose a file")
|
| 186 |
+
if uploaded_file:
|
| 187 |
+
st.write("File uploaded!")
|
| 188 |
|
| 189 |
+
elif selected_topic == "Try It Yourself":
|
| 190 |
+
st.title("Practice Zone")
|
| 191 |
+
|
| 192 |
+
st.write("Try writing some Streamlit code below!")
|
| 193 |
+
|
| 194 |
+
code = st.text_area("Your code:", height=200,
|
| 195 |
+
placeholder="Example:\nst.write('Hello World!')")
|
| 196 |
+
|
| 197 |
+
if st.button("Run Code"):
|
| 198 |
+
try:
|
| 199 |
+
exec(code)
|
| 200 |
+
except Exception as e:
|
| 201 |
+
st.error(f"Error: {str(e)}")
|
| 202 |
+
|
| 203 |
+
# Footer with helpful tips
|
| 204 |
+
with st.expander("💡 Tips & Tricks"):
|
| 205 |
+
st.markdown("""
|
| 206 |
+
- Use `st.write()` for quick output
|
| 207 |
+
- Columns help organize content
|
| 208 |
+
- Forms batch multiple inputs
|
| 209 |
+
- Always handle errors in user inputs
|
| 210 |
+
- Use expanders for additional info
|
| 211 |
+
""")
|
| 212 |
|
|
|
|
| 213 |
st.markdown("---")
|
| 214 |
+
st.caption("Learn more at streamlit.io/docs")
|
datanalysis.py
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 7 |
+
st.set_page_config(layout="wide", page_title="Business Analytics Dashboard Tutorial")
|
| 8 |
+
|
| 9 |
+
# Sample business data generation
|
| 10 |
+
def generate_sales_data():
|
| 11 |
+
dates = pd.date_range(start='2024-01-01', end='2024-12-31', freq='D')
|
| 12 |
+
np.random.seed(42)
|
| 13 |
+
|
| 14 |
+
df = pd.DataFrame({
|
| 15 |
+
'Date': dates,
|
| 16 |
+
'Sales': np.random.normal(1000, 200, len(dates)),
|
| 17 |
+
'Region': np.random.choice(['North', 'South', 'East', 'West'], len(dates)),
|
| 18 |
+
'Product': np.random.choice(['Electronics', 'Clothing', 'Food', 'Books'], len(dates)),
|
| 19 |
+
'Customer_Type': np.random.choice(['Retail', 'Wholesale'], len(dates)),
|
| 20 |
+
})
|
| 21 |
+
df['Profit'] = df['Sales'] * np.random.uniform(0.15, 0.25, len(df))
|
| 22 |
+
return df
|
| 23 |
+
|
| 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')}")
|