Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,9 @@
|
|
| 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']
|
|
@@ -35,10 +31,6 @@ def generate_sample_data():
|
|
| 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
|
|
@@ -84,6 +76,9 @@ if state_query:
|
|
| 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)
|
|
@@ -115,38 +110,11 @@ if not filtered_df.empty:
|
|
| 115 |
)
|
| 116 |
st.dataframe(matrix_data)
|
| 117 |
|
| 118 |
-
# Visualization
|
| 119 |
-
st.subheader("
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
st.
|
| 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 |
-
Add 'Net Sales Value - Cost' as a new column
|
| 137 |
-
filtered_df['Net Sales Value - Cost'] = filtered_df['Net Sales Value'] - filtered_df['Cost']
|
| 138 |
-
|
| 139 |
-
# Visualization 4: Profit and Loss Overview (Simple Table)
|
| 140 |
-
st.subheader("Profit and Loss Overview")
|
| 141 |
-
pnl_data = filtered_df[['Customer Name', 'Product Name', 'Net Sales Value', 'Cost', 'Net Sales Value - Cost']]
|
| 142 |
-
pnl_data.columns = ['Customer', 'Product', 'Sales', 'Cost', 'Profit']
|
| 143 |
-
st.dataframe(pnl_data)
|
| 144 |
-
|
| 145 |
-
# Visualization 5: Gross Margin Distribution (Pie chart)
|
| 146 |
-
st.subheader("Gross Margin Distribution")
|
| 147 |
-
margin_data = filtered_df.groupby('Product Name')['Net Sales Value'].sum().reset_index()
|
| 148 |
-
fig_margin = px.pie(margin_data, names='Product Name', values='Net Sales Value', title='Gross Margin by Product')
|
| 149 |
-
st.plotly_chart(fig_margin)
|
| 150 |
|
| 151 |
else:
|
| 152 |
st.write("No data available for the selected filters.")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import plotly.express as px
|
|
|
|
| 4 |
import random
|
| 5 |
from datetime import datetime, timedelta
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
# Sample Data Generation (You can remove this part when uploading your own file)
|
| 8 |
def generate_sample_data():
|
| 9 |
customers = ['Customer A', 'Customer B', 'Customer C', 'Customer D']
|
|
|
|
| 31 |
# Load sample data
|
| 32 |
df = generate_sample_data()
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
# Data Preprocessing
|
| 35 |
df['Date'] = pd.to_datetime(df['Date'])
|
| 36 |
df['Year'] = df['Date'].dt.year
|
|
|
|
| 76 |
if country_query:
|
| 77 |
filtered_df = filtered_df[filtered_df['Country'].str.contains(country_query, case=False, na=False)]
|
| 78 |
|
| 79 |
+
# Add 'Net Sales Value - Cost' as a new column
|
| 80 |
+
filtered_df['Net Sales Value - Cost'] = filtered_df['Net Sales Value'] - filtered_df['Cost']
|
| 81 |
+
|
| 82 |
# Display Filtered Data
|
| 83 |
st.write(f"Filtered Data: {len(filtered_df)} records found.")
|
| 84 |
st.dataframe(filtered_df)
|
|
|
|
| 110 |
)
|
| 111 |
st.dataframe(matrix_data)
|
| 112 |
|
| 113 |
+
# Visualization 4: Profit and Loss Overview (Simple Table)
|
| 114 |
+
st.subheader("Profit and Loss Overview")
|
| 115 |
+
pnl_data = filtered_df[['Customer Name', 'Product Name', 'Net Sales Value', 'Cost', 'Net Sales Value - Cost']]
|
| 116 |
+
pnl_data.columns = ['Customer', 'Product', 'Sales', 'Cost', 'Profit']
|
| 117 |
+
st.dataframe(pnl_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
else:
|
| 120 |
st.write("No data available for the selected filters.")
|