Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions community_gallery/sleep-health-analysis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Sleep Health and Lifestyle Analysis

## Dataset Source
This project uses the Sleep Health and Lifestyle Dataset from Kaggle, which contains information about sleep patterns, BMI categories, and sleep disorders across different categories.
Reference Link: https://www.kaggle.com/datasets/uom190346a/sleep-health-and-lifestyle-dataset/data

## What This App Does
This application provides:
- An overview of sleep health metrics across the dataset
- Interactive filtering by sleep duration using a slider
- Visualizations showing the relationship between gender, BMI category, and sleep disorders
- A data table displaying the filtered dataset records

## How to Run and Deploy

## How to Run and Deploy

1. Install preswald. You can create a new virtual environment or install other requirements as needed.
- pip install preswald

2. Download the dataset from the provided source.
(https://www.kaggle.com/datasets/uom190346a/sleep-health-and-lifestyle-dataset/data)

3. You can execute the hello.py file using: preswald run to initialize and run the application locally.

4. To deploy the application, execute the following code in your terminal:
preswald deploy --target structured --github YOUR_GITHUB_USERNAME --api-key YOUR_API_KEY

Note: You need to replace "YOUR_GITHUB_USERNAME" with your Github Profile username (all lowercase) and "YOUR_API_KEY" with the API key generated from "app.preswald.com"



## Live Demo
[Sleep Health Dashboard](https://coding-assessment-466795-3ewu6miz-ndjz2ws6la-ue.a.run.app)

Note: This app is developed taking the 30-minute time limitation into account. The visual dashboard and the storyline can further be improved further with more time.
70 changes: 70 additions & 0 deletions community_gallery/sleep-health-analysis/hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from preswald import text, plotly, connect, get_df, table, slider
import pandas as pd
import plotly.express as px

text("# Sleep Health and Lifestyle Dashboard")
text("This dashboard will help you understand the relationship between sleep and lifestyle factors based on gender, BMI category and sleep disorders.")

# Load the CSV
connect()
df = get_df('sleep_health')

#Overview of the data
total_participants = len(df)
male_participants = len(df[df['Gender'] == 'Male'])
female_participants = len(df[df['Gender'] == 'Female'])
disorder_count = len(df[df['Sleep Disorder'] != 'None'])

#Display of summary statistics
text("## Sleep Health at a Glance")
text(f"**Total Participants:** {total_participants} | **Males:** {male_participants} | **Females:** {female_participants} | **Suffering from Sleep Disorders:** {disorder_count}")

#Sleep Duration Breakdown
min_sleep = float(df['Sleep Duration'].min())
avg_sleep = float(df['Sleep Duration'].mean())
max_sleep = float(df['Sleep Duration'].max())

#Filtering data based on sleep duration
text("## Filter by Sleep Duration")
sleep_threshold = slider("Minimum Sleep Duration (hours)",
min_val=min_sleep,
max_val=max_sleep,
default=min_sleep)

# Filter data based on slider value
filtered_df = df[df['Sleep Duration'] >= sleep_threshold]
text(f"Showing {len(filtered_df)} records with Sleep Duration ≥ {sleep_threshold:.2f} hours")

#Gender vs BMI Category
bmi_by_gender = filtered_df.groupby(['Gender', 'BMI Category']).size().reset_index(name='Count')
fig1 = px.bar(
bmi_by_gender,
x = 'Gender',
y = 'Count',
color = 'BMI Category',
title = 'BMI Category by Gender',
labels = {'Count': 'Number of People', 'BMI Category': 'BMI Category Type'},
barmode = 'group'
)

#Display the graph
plotly(fig1)

#Gender vs Sleep Disorders
disorder_by_gender = filtered_df.groupby(['Gender', 'Sleep Disorder']).size().reset_index(name='Count')
fig2 = px.bar(
disorder_by_gender,
x='Gender',
y='Count',
color='Sleep Disorder',
title='Sleep Disorders by Gender',
labels={'Count': 'Number of People', 'Sleep Disorder': 'Sleep Disorder Type'},
barmode='group'
)

#Display the graph
plotly(fig2)

#Dataset as table
text("## Dataset overview")
table(filtered_df.head(100), title="Sleep Health and Lifestyle Dataset")
24 changes: 24 additions & 0 deletions community_gallery/sleep-health-analysis/preswald.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[project]
title = "Preswald Project"
version = "0.1.0"
port = 8501
slug = "coding-assessment-466795" # 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.sample_csv]
type = "csv"
path = "data/sample.csv"

[logging]
level = "INFO" # Options: DEBUG, INFO, WARNING, ERROR, CRITICAL
format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"

[data.sleep_health]
path = "data/Sleep_Health.csv"
type = "csv"
23 changes: 23 additions & 0 deletions community_gallery/sleep-health-analysis/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