Your First Health App

With your development environment set up, it's time to create your first healthcare app using Health Universe and Streamlit. In this tutorial, we will guide you through building a simple app that visualizes healthcare data using interactive charts.

Step 1: Prepare Your Data

For this tutorial, we'll use a sample dataset containing information about patients, such as age, gender, and health metrics. You can find a suitable dataset online or create a CSV file with the following columns: Patient_ID, Age, Gender, Height, Weight, Blood_Pressure, and Heart_Rate.

Save the CSV file as patient_data.csv in the data directory of your project.

Step 2: Load the Data in Your App

To load the data in your app, we'll use the Pandas library. If you haven't installed Pandas, run the following command in your terminal:

pip install pandas

Next, open app.py in your text editor or IDE and import the Pandas library by adding the following line at the beginning of the file:

import pandas as pd

Now, modify the main() function to load the patient data and display it as a table using Streamlit:

def main():
    st.title("Healthcare Data Visualization App")
    
    # Load patient data
    patient_data = pd.read_csv("data/patient_data.csv")

    # Display data table
    st.write("Patient Data:")
    st.write(patient_data)

Save the app.py file.

Step 3: Add Interactive Data Filtering

To allow users to filter the data based on age and gender, add the following code to the main() function, right after displaying the data table:

# Filter data by age
min_age = st.sidebar.slider("Minimum Age", int(patient_data["Age"].min()), int(patient_data["Age"].max()), int(patient_data["Age"].min()))
max_age = st.sidebar.slider("Maximum Age", int(patient_data["Age"].min()), int(patient_data["Age"].max()), int(patient_data["Age"].max()))

# Filter data by gender
gender = st.sidebar.selectbox("Gender", ["All", "Male", "Female"])

# Apply filters
filtered_data = patient_data[(patient_data["Age"] >= min_age) & (patient_data["Age"] <= max_age)]
if gender != "All":
    filtered_data = filtered_data[filtered_data["Gender"] == gender]

# Display filtered data
st.write("Filtered Patient Data:")
st.write(filtered_data)

This code adds interactive sliders and a dropdown menu to the sidebar, allowing users to select minimum and maximum ages and a gender. The filtered data is then displayed as a table in the main content area.

Step 4: Visualize the Data

To visualize the filtered data, we'll create a bar chart showing the average blood pressure for each age group. First, install the Plotly library by running the following command in your terminal:

pip install plotly

Next, import the Plotly library in app.py by adding the following line at the beginning of the file:

import plotly.express as px

Now, add the following code to the main() function, after displaying the filtered data:

# Aggregate data by age and calculate average blood pressure
agg_data = filtered_data.groupby("Age")["Blood_Pressure"].mean().reset_index()

# Create a bar chart of average blood pressure by age
fig = px.bar(agg_data, x="Age", y="Blood_Pressure", text="Blood_Pressure", labels={"Blood_Pressure": "Average Blood Pressure"}, title="Average Blood Pressure by Age", height=400)

# Display the bar chart in the app
st.plotly_chart(fig)

This code calculates the average blood pressure for each age group, creates a bar chart using Plotly Express, and displays the chart in the main content area of the app.

Save the app.py file.

Step 5: Run Your Healthcare App

To run your healthcare data visualization app, open the terminal, navigate to your project directory, and run the following command:

streamlit run app.py

Streamlit will launch your app in your default web browser, displaying the interactive data filters and bar chart. To stop the app, press Ctrl+C in the terminal.

Congratulations! You have successfully created your first healthcare app using Streamlit that you can deploy to Health Universe and share with the world. You can now build on this foundation to create more advanced applications, incorporating machine learning models, additional visualizations, and user interactivity.

Last updated