From b4f82a3bfde404e584cd276898f3a5c6726c01cc Mon Sep 17 00:00:00 2001 From: Vijaya Sri Ram Date: Wed, 19 Mar 2025 17:58:28 -0700 Subject: [PATCH 1/2] Add Sleep Health and Lifestyle Analysis example --- .../sleep-health-analysis/README.md | 28 ++++++++ .../sleep-health-analysis/hello.py | 70 +++++++++++++++++++ .../sleep-health-analysis/preswald.toml | 24 +++++++ .../sleep-health-analysis/pyproject.toml | 23 ++++++ 4 files changed, 145 insertions(+) create mode 100644 community_gallery/sleep-health-analysis/README.md create mode 100644 community_gallery/sleep-health-analysis/hello.py create mode 100644 community_gallery/sleep-health-analysis/preswald.toml create mode 100644 community_gallery/sleep-health-analysis/pyproject.toml diff --git a/community_gallery/sleep-health-analysis/README.md b/community_gallery/sleep-health-analysis/README.md new file mode 100644 index 00000000..a85343ee --- /dev/null +++ b/community_gallery/sleep-health-analysis/README.md @@ -0,0 +1,28 @@ +# 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 + +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. \ No newline at end of file diff --git a/community_gallery/sleep-health-analysis/hello.py b/community_gallery/sleep-health-analysis/hello.py new file mode 100644 index 00000000..76edf14f --- /dev/null +++ b/community_gallery/sleep-health-analysis/hello.py @@ -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") diff --git a/community_gallery/sleep-health-analysis/preswald.toml b/community_gallery/sleep-health-analysis/preswald.toml new file mode 100644 index 00000000..c05e463d --- /dev/null +++ b/community_gallery/sleep-health-analysis/preswald.toml @@ -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" \ No newline at end of file diff --git a/community_gallery/sleep-health-analysis/pyproject.toml b/community_gallery/sleep-health-analysis/pyproject.toml new file mode 100644 index 00000000..60878dfb --- /dev/null +++ b/community_gallery/sleep-health-analysis/pyproject.toml @@ -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 \ No newline at end of file From 4cf34b1fc8450349e1baf7212fce2e1da65cc5de Mon Sep 17 00:00:00 2001 From: Vijaya Sri Ram Date: Wed, 19 Mar 2025 18:01:44 -0700 Subject: [PATCH 2/2] Modify Readme.md file --- .../sleep-health-analysis/README.md | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/community_gallery/sleep-health-analysis/README.md b/community_gallery/sleep-health-analysis/README.md index a85343ee..7e3b63a4 100644 --- a/community_gallery/sleep-health-analysis/README.md +++ b/community_gallery/sleep-health-analysis/README.md @@ -13,15 +13,23 @@ This application provides: ## 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 +## 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)