Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Sales Data Analysis app to community_gallery #543

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
34 changes: 34 additions & 0 deletions community_gallery/sales_data/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 📊 Sales Data Analysis App

This is an interactive data analysis app. It allows users to explore and visualize sales data through an intuitive web-based interface.

## 🔧 Features

- **Dynamic Table Viewer**: Use a slider to control how many rows of the dataset to preview.
- **Interactive Histogram**: Select between `Quantity_Sold` or `Sales_Amount` to visualize their distributions.
- **Sales Filtering**: Toggle a checkbox to view only high-performing sales (Sales Amount > 5000).
- **Scatter Plot Visualization**: Explore the relationship between `Quantity_Sold` and `Sales_Amount`, colored by product category.
- **Error Handling**: Gracefully handles missing or misconfigured datasets with alerts.

## 🧾 Dataset Description

This app expects a dataset named `sales_data.csv` with the following columns:

| Column | Description |
|-----------------------|--------------------------------------|
| `Product_ID` | Product identifier |
| `Sale_Date` | Date of the sale |
| `Sales_Rep` | Sales representative name |
| `Region` | Sales region |
| `Sales_Amount` | Total revenue from the sale |
| `Quantity_Sold` | Units sold |
| `Product_Category` | Category (e.g., Furniture, Food) |
| `Unit_Cost` | Cost per unit |
| `Unit_Price` | Selling price per unit |
| `Customer_Type` | Customer status (New or Returning) |
| `Discount` | Discount applied (e.g., 0.08 = 8%) |
| `Payment_Method` | Cash, Credit Card, Bank Transfer... |
| `Sales_Channel` | Online or Retail |
| `Region_and_Sales_Rep`| Combined label for visualizations |


1,001 changes: 1,001 additions & 0 deletions community_gallery/sales_data/data/sales_data.csv

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions community_gallery/sales_data/data/sample.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
item,quantity,value
Item A,10,100
Item B,5,60
Item C,8,80
Item D,12,150
Item E,7,90
Item F,9,110
Item G,4,50
Item H,11,130
Item I,6,70
Item J,3,40
64 changes: 64 additions & 0 deletions community_gallery/sales_data/hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from preswald import connect, get_df, table, text, plotly, slider, selectbox, separator, alert, checkbox
import plotly.express as px

# Connect to Preswald data sources
connect()
df = get_df("sales_data") # Load dataset

# Display title
text("# Sales Data Analysis App")

# Check if df is None before proceeding
if df is None:
alert("Error: Dataset 'sales_data' not found. Ensure it is correctly configured in preswald.toml.", type="error")
else:
# Debugging: Print available columns
# table(df.head())
text(f"Columns in dataset: {', '.join(df.columns)}")

# Ensure DataFrame is loaded and contains required columns
if not df.empty and all(col in df.columns for col in ['Quantity_Sold', 'Sales_Amount', 'Product_ID', 'Product_Category']):

separator() # Add visual break

# Add slider to control number of rows displayed
num_rows = slider("Select number of rows to display", min_val=5, max_val=50, step=5, default=10)
table(df.head(num_rows))

separator() # Add another visual break

# Add selectbox for column selection
column_choice = selectbox("Choose a column to visualize", options=['Quantity_Sold', 'Sales_Amount'])

# Display histogram based on selected column
fig_hist = px.histogram(df, x=column_choice, title=f"Distribution of {column_choice}")
plotly(fig_hist)

separator() # Add another visual break

# Add checkbox for toggling between full dataset and filtered dataset
show_filtered = checkbox("Show only high sales data (Sales Amount > 5000)")

if show_filtered:
df_filtered = df[df['Sales_Amount'] > 5000]
table(df_filtered.head(num_rows))
else:
table(df.head(num_rows))

# Create scatter plot with hover labels (removing static text labels)
fig = px.scatter(df, x='Quantity_Sold', y='Sales_Amount', hover_name='Product_ID',
color='Product_Category',
title='Quantity Sold vs. Sales Amount',
labels={'Quantity_Sold': 'Quantity Sold', 'Sales_Amount': 'Sales Amount'})

# Update traces for better readability
fig.update_traces(marker=dict(size=6, opacity=0.6))

# Style the plot
fig.update_layout(template='plotly_white')

# Show the plot
plotly(fig)

else:
alert("Error: Data failed to load or missing required columns. Please check the dataset.", type="error")
Binary file added community_gallery/sales_data/images/favicon.ico
Binary file not shown.
Binary file added community_gallery/sales_data/images/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions community_gallery/sales_data/preswald.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[project]
title = "Preswald Project"
version = "0.1.0"
port = 8501
slug = "my-project-262319" # Required: Unique identifier for your project
entrypoint = "hello.py" # Required: Main script to run when executing preswald run

[branding]
name = "Preswald Project"
logo = "images/logo.png"
favicon = "images/favicon.ico"
primaryColor = "#F89613"

[data.sales_data]
type = "csv"
path = "data/sales_data.csv"

[logging]
level = "INFO" # Options: DEBUG, INFO, WARNING, ERROR, CRITICAL
format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
23 changes: 23 additions & 0 deletions community_gallery/sales_data/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "preswald-app"
version = "0.1.0"
description = "A Preswald application"
requires-python = ">=3.8"
dependencies = [
"preswald"
]

[tool.hatch.build.targets.wheel]
packages = ["."]

[tool.black]
line-length = 88
target-version = ['py38']

[tool.isort]
profile = "black"
multi_line_output = 3
33 changes: 33 additions & 0 deletions community_gallery/sales_data/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# 📊 Sales Data Analysis App

This is an interactive data analysis app. It provides visualizations and controls for exploring sales performance data, including quantities sold, sales amounts, product categories, and more.

## 🔧 Features

- **Dynamic Table Viewer**: Use a slider to control how many rows of the dataset to preview.
- **Interactive Histogram**: Select between `Quantity_Sold` or `Sales_Amount` to visualize their distributions.
- **Sales Filtering**: Toggle a checkbox to view only high-performing sales (Sales Amount > 5000).
- **Scatter Plot Visualization**: Explore the relationship between `Quantity_Sold` and `Sales_Amount`, colored by product category.
- **Error Handling**: Gracefully handles missing or misconfigured datasets with alerts.

## 📁 Dataset Requirements


| Column | Description |
|---------------------|-------------------------------------|
| `Product_ID` | Product identifier |
| `Sale_Date` | Date of the sale |
| `Sales_Rep` | Sales representative name |
| `Region` | Sales region |
| `Sales_Amount` | Total revenue from the sale |
| `Quantity_Sold` | Units sold |
| `Product_Category` | Category (e.g., Furniture, Food) |
| `Unit_Cost` | Cost per unit |
| `Unit_Price` | Selling price per unit |
| `Customer_Type` | Customer status (New or Returning) |
| `Discount` | Discount applied (e.g., 0.08 = 8%) |
| `Payment_Method` | Cash, Credit Card, Bank Transfer... |
| `Sales_Channel` | Online or Retail |
| `Region_and_Sales_Rep` | Combined label for visualizations |


3 changes: 3 additions & 0 deletions community_gallery/sales_data/secrets.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[data.postgres]
password = ""