Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -52,7 +52,7 @@ def load_data(active_card):
|
|
52 |
card_specific_cols = {
|
53 |
'card1': ['FyWeek', 'State', 'Itemtype', 'Chaincode', 'SalesVolume'],
|
54 |
'card2': ['FyWeek', 'Fy', 'State','Store','Address','Zipcode','City','Itemtype', 'Chaincode', 'Containercode', 'SalesVolume', 'UnitPrice', 'Sales'],
|
55 |
-
'card3': ['FyWeek', 'Fy', 'State',
|
56 |
}
|
57 |
|
58 |
# Choose columns based on the active card
|
@@ -80,8 +80,9 @@ def load_data(active_card):
|
|
80 |
ddf = dd.read_csv("fy21-24.csv", usecols=required_columns, dtype=dtype_spec)
|
81 |
df = ddf.compute()
|
82 |
|
|
|
83 |
|
84 |
-
if active_card in ['card2','card3']:
|
85 |
df = df.groupby(['FyWeek', 'Fy', 'Chaincode', 'Store', 'Address', 'Zipcode', 'City', 'State', 'Containercode', 'Itemtype'], observed=True).agg({
|
86 |
'SalesVolume': 'sum',
|
87 |
'UnitPrice': 'mean',
|
@@ -91,20 +92,23 @@ def load_data(active_card):
|
|
91 |
df['Week'] = df['Week'].astype(int) # Convert 'Week' to int
|
92 |
df['Year'] = df['FY'].str[2:].astype(int) # Extract year part and convert to int
|
93 |
df['Dt'] = date_from_week(df['Year'], df['Week'])
|
94 |
-
|
95 |
# Add the region column based on state
|
96 |
df['Region'] = df['State'].map(state_to_region)
|
97 |
|
98 |
-
# st.write(df.columns)
|
99 |
return df
|
100 |
|
|
|
|
|
|
|
|
|
101 |
# Display logo
|
102 |
st.image("bonnie.png", width=150) # Adjust width as needed
|
103 |
|
104 |
# Display title
|
105 |
-
|
106 |
-
st.title("Price vs. Sales Volume Tracker Dashboard")
|
107 |
|
|
|
|
|
108 |
|
109 |
# Initialize session state for storing which card was clicked and item type
|
110 |
if 'active_card' not in st.session_state:
|
@@ -116,7 +120,7 @@ if 'selected_feature' not in st.session_state:
|
|
116 |
st.session_state['selected_feature'] = 'Chaincode' # Default to 'Chain Code'
|
117 |
|
118 |
# Card selection buttons
|
119 |
-
col1, col2
|
120 |
# Define buttons for plot categories, update session state when clicked
|
121 |
with col1:
|
122 |
if st.button("Sales Volume Trend for Item Category"):
|
@@ -128,7 +132,8 @@ with col2:
|
|
128 |
with col3:
|
129 |
if st.button("PE Coefficient Calculation for Regions & Item Categories"):
|
130 |
st.session_state['active_card'] = 'card3'
|
131 |
-
|
|
|
132 |
start_time=time.time()
|
133 |
# st.write(st.session_state['active_card'])
|
134 |
df = load_data(st.session_state['active_card'])
|
@@ -155,6 +160,7 @@ if st.session_state['active_card'] == 'card1':
|
|
155 |
start_time = time.time()
|
156 |
group_data = filtered_df.groupby(['FyWeek', selected_feature],observed=True)['SalesVolume'].sum().reset_index()
|
157 |
time_taken = time.time() - start_time
|
|
|
158 |
|
159 |
# Plotting
|
160 |
fig = px.bar(group_data, x='FyWeek', y='SalesVolume', color=selected_feature,
|
@@ -164,12 +170,7 @@ if st.session_state['active_card'] == 'card1':
|
|
164 |
|
165 |
##########################################################################################################
|
166 |
|
167 |
-
|
168 |
-
########################################### CARD #2 ####################################################
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
|
174 |
if st.session_state['active_card'] == 'card2':
|
175 |
# Dropdown to select item type (using session_state)
|
@@ -178,7 +179,7 @@ if st.session_state['active_card'] == 'card2':
|
|
178 |
index=list(df['Itemtype'].unique()).index(st.session_state['selected_item_type']))
|
179 |
|
180 |
# Dropdown to select the grouping category (container code, chain code, or state)
|
181 |
-
group_by_option = st.selectbox('Group by', ['Containercode', 'Chaincode', 'State'])
|
182 |
|
183 |
# Multi-select checkbox to select multiple years
|
184 |
selected_years = st.multiselect('Select Year(s)', [2021, 2022, 2023, 2024], default=[2021])
|
@@ -198,7 +199,7 @@ if st.session_state['active_card'] == 'card2':
|
|
198 |
|
199 |
# Filter the data for only the top 3 values
|
200 |
top_group_data = filtered_df[filtered_df[group_by_option].isin(top_3_values)]
|
201 |
-
|
202 |
# Aggregate data
|
203 |
agg_df = top_group_data.groupby([group_by_option, 'Year', 'Week', 'Dt'], observed=True).agg({
|
204 |
'SalesVolume': 'sum',
|
@@ -261,21 +262,17 @@ if st.session_state['active_card'] == 'card2':
|
|
261 |
yaxis2=dict(title='UnitPrice', overlaying='y', side='right'),
|
262 |
legend=dict(x=0.9, y=1.15),
|
263 |
hovermode="x unified", # Show both values in a tooltip
|
264 |
-
height=
|
265 |
-
|
266 |
-
margin=dict(l=1, r=1, t=1, b=1)
|
267 |
)
|
268 |
|
269 |
# Rotate X-axis labels
|
270 |
fig.update_xaxes(tickangle=90)
|
271 |
|
272 |
# Display the Plotly figure in Streamlit
|
273 |
-
st.plotly_chart(fig, use_container_width=
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
##########################################################################################################
|
278 |
|
|
|
279 |
if st.session_state['active_card'] == 'card3':
|
280 |
# Dropdown for selecting the item type
|
281 |
item_type_options = df['Itemtype'].unique()
|
@@ -329,4 +326,3 @@ if st.session_state['active_card'] == 'card3':
|
|
329 |
)
|
330 |
|
331 |
st.plotly_chart(fig, use_container_width=True)
|
332 |
-
###################################################################################################################
|
|
|
52 |
card_specific_cols = {
|
53 |
'card1': ['FyWeek', 'State', 'Itemtype', 'Chaincode', 'SalesVolume'],
|
54 |
'card2': ['FyWeek', 'Fy', 'State','Store','Address','Zipcode','City','Itemtype', 'Chaincode', 'Containercode', 'SalesVolume', 'UnitPrice', 'Sales'],
|
55 |
+
'card3': ['FyWeek', 'Fy', 'State','Store','Address','Zipcode','City','Itemtype', 'Chaincode', 'Containercode', 'SalesVolume', 'UnitPrice', 'Sales'] # Added for PE calculation card
|
56 |
}
|
57 |
|
58 |
# Choose columns based on the active card
|
|
|
80 |
ddf = dd.read_csv("fy21-24.csv", usecols=required_columns, dtype=dtype_spec)
|
81 |
df = ddf.compute()
|
82 |
|
83 |
+
# st.write("+++++++++++++++++++++++")
|
84 |
|
85 |
+
if active_card in ['card2', 'card3', 'card4']:
|
86 |
df = df.groupby(['FyWeek', 'Fy', 'Chaincode', 'Store', 'Address', 'Zipcode', 'City', 'State', 'Containercode', 'Itemtype'], observed=True).agg({
|
87 |
'SalesVolume': 'sum',
|
88 |
'UnitPrice': 'mean',
|
|
|
92 |
df['Week'] = df['Week'].astype(int) # Convert 'Week' to int
|
93 |
df['Year'] = df['FY'].str[2:].astype(int) # Extract year part and convert to int
|
94 |
df['Dt'] = date_from_week(df['Year'], df['Week'])
|
|
|
95 |
# Add the region column based on state
|
96 |
df['Region'] = df['State'].map(state_to_region)
|
97 |
|
|
|
98 |
return df
|
99 |
|
100 |
+
# st.image("bonnie.png", width=200)
|
101 |
+
# # Main interactive section
|
102 |
+
# st.title('Bonnnie Plants Price vs Sales Volume Trcaker')
|
103 |
+
|
104 |
# Display logo
|
105 |
st.image("bonnie.png", width=150) # Adjust width as needed
|
106 |
|
107 |
# Display title
|
108 |
+
st.title("Bonnie Plants Pricing & Sales Analytics Dashboard")
|
|
|
109 |
|
110 |
+
# Close the div for logo and title
|
111 |
+
# st.markdown('</div>', unsafe_allow_html=True)
|
112 |
|
113 |
# Initialize session state for storing which card was clicked and item type
|
114 |
if 'active_card' not in st.session_state:
|
|
|
120 |
st.session_state['selected_feature'] = 'Chaincode' # Default to 'Chain Code'
|
121 |
|
122 |
# Card selection buttons
|
123 |
+
col1, col2, col3= st.columns(3)
|
124 |
# Define buttons for plot categories, update session state when clicked
|
125 |
with col1:
|
126 |
if st.button("Sales Volume Trend for Item Category"):
|
|
|
132 |
with col3:
|
133 |
if st.button("PE Coefficient Calculation for Regions & Item Categories"):
|
134 |
st.session_state['active_card'] = 'card3'
|
135 |
+
|
136 |
+
|
137 |
start_time=time.time()
|
138 |
# st.write(st.session_state['active_card'])
|
139 |
df = load_data(st.session_state['active_card'])
|
|
|
160 |
start_time = time.time()
|
161 |
group_data = filtered_df.groupby(['FyWeek', selected_feature],observed=True)['SalesVolume'].sum().reset_index()
|
162 |
time_taken = time.time() - start_time
|
163 |
+
# st.write(f"Data grouped in {time_taken:.2f} seconds")
|
164 |
|
165 |
# Plotting
|
166 |
fig = px.bar(group_data, x='FyWeek', y='SalesVolume', color=selected_feature,
|
|
|
170 |
|
171 |
##########################################################################################################
|
172 |
|
173 |
+
##########################################################################################################
|
|
|
|
|
|
|
|
|
|
|
174 |
|
175 |
if st.session_state['active_card'] == 'card2':
|
176 |
# Dropdown to select item type (using session_state)
|
|
|
179 |
index=list(df['Itemtype'].unique()).index(st.session_state['selected_item_type']))
|
180 |
|
181 |
# Dropdown to select the grouping category (container code, chain code, or state)
|
182 |
+
group_by_option = st.selectbox('Group by', ['Containercode', 'Chaincode', 'State','Region'])
|
183 |
|
184 |
# Multi-select checkbox to select multiple years
|
185 |
selected_years = st.multiselect('Select Year(s)', [2021, 2022, 2023, 2024], default=[2021])
|
|
|
199 |
|
200 |
# Filter the data for only the top 3 values
|
201 |
top_group_data = filtered_df[filtered_df[group_by_option].isin(top_3_values)]
|
202 |
+
|
203 |
# Aggregate data
|
204 |
agg_df = top_group_data.groupby([group_by_option, 'Year', 'Week', 'Dt'], observed=True).agg({
|
205 |
'SalesVolume': 'sum',
|
|
|
262 |
yaxis2=dict(title='UnitPrice', overlaying='y', side='right'),
|
263 |
legend=dict(x=0.9, y=1.15),
|
264 |
hovermode="x unified", # Show both values in a tooltip
|
265 |
+
height=600,
|
266 |
+
margin=dict(l=50, r=50, t=50, b=50)
|
|
|
267 |
)
|
268 |
|
269 |
# Rotate X-axis labels
|
270 |
fig.update_xaxes(tickangle=90)
|
271 |
|
272 |
# Display the Plotly figure in Streamlit
|
273 |
+
st.plotly_chart(fig, use_container_width=True)
|
|
|
|
|
|
|
|
|
274 |
|
275 |
+
################################
|
276 |
if st.session_state['active_card'] == 'card3':
|
277 |
# Dropdown for selecting the item type
|
278 |
item_type_options = df['Itemtype'].unique()
|
|
|
326 |
)
|
327 |
|
328 |
st.plotly_chart(fig, use_container_width=True)
|
|