Final_Part2_Personal / pages /Dashboard.py
Shah-Miloni's picture
Update pages/Dashboard.py
4847308 verified
import os
import sys
import subprocess
required_libraries = ["streamlit", "pandas", "plotly", "altair"]
for lib in required_libraries:
try:
__import__(lib)
except ImportError:
subprocess.check_call([sys.executable, "-m", "pip", "install", lib])
import streamlit as st
import pandas as pd
import plotly.express as px
import altair as alt
child_mortality_url = "https://huggingface.co/spaces/jiyachachan/fp2/resolve/main/child_mortality_0_5_year_olds_dying_per_1000_born.csv"
life_expectancy_url = "https://huggingface.co/spaces/jiyachachan/fp2/resolve/main/life_expectancy.csv"
population_url = "https://huggingface.co/spaces/jiyachachan/fp2/resolve/main/pop.csv"
income_url = "https://huggingface.co/spaces/jiyachachan/fp2/resolve/main/mincpcap_cppp.csv"
child_mortality = pd.read_csv(child_mortality_url)
life_expectancy = pd.read_csv(life_expectancy_url)
population = pd.read_csv(population_url)
income = pd.read_csv(income_url)
def convert_population(value):
if isinstance(value, str):
if 'B' in value:
return float(value.replace('B', '')) * 1_000_000_000
elif 'M' in value:
return float(value.replace('M', '')) * 1_000_000
elif 'k' in value:
return float(value.replace('k', '')) * 1_000
else:
return float(value)
return value
population.iloc[:, 1:] = population.iloc[:, 1:].applymap(convert_population)
# 1. Choropleth Map
st.title("Analyzing Global Child Mortality")
st.write("By Jiya Chachan, Smeet Patel, Ji Eun Kim, Miloni Shah, Chenzhao Wang")
data_melted = child_mortality.melt(id_vars=["country"], var_name="year", value_name="mortality_rate")
data_melted["year"] = pd.to_numeric(data_melted["year"])
min_year = int(data_melted["year"].min())
max_year = int(data_melted["year"].max())
selected_year = st.slider("Select Year", min_value=min_year, max_value=max_year, value=2024, step=5)
filtered_data = data_melted[data_melted["year"] == selected_year]
st.write("""
### Child Mortality Map - per 1000 children born
This chart reveals an important trend of how the child mortality rate has been changing across the years.
It provides insights into how developed countries have successfully reduced the rate, while underdeveloped countries still face challenges in curbing child mortality.
We can utilize the trends in the graph to understand the factors responsible for high or low mortality.
""")
fig1 = px.choropleth(
filtered_data,
locations="country",
locationmode="country names",
color="mortality_rate",
title=f"Child Mortality Rate in {selected_year}",
color_continuous_scale=px.colors.sequential.OrRd,
)
st.plotly_chart(fig1)
# 2. Visual 2: Child Mortality Trends (Altair Line Chart)
countries = list(child_mortality['country'].unique())
selected_countries = st.sidebar.multiselect("Select Countries", countries, default=["Argentina", "Australia", "India", "USA"])
years = [str(year) for year in range(1900, 2025)]
filtered_mortality = child_mortality[child_mortality['country'].isin(selected_countries)][['country'] + years]
mortality_melted = filtered_mortality.melt(id_vars="country", var_name="year", value_name="mortality_rate")
st.write("""
### Child Mortality Trends
This line chart shows the trends in child mortality rates over the years for selected countries.
""")
mortality_chart = alt.Chart(mortality_melted).mark_line().encode(
x="year:O",
y="mortality_rate:Q",
color="country:N",
tooltip=["country", "year", "mortality_rate"]
).properties(width=700, height=400)
st.altair_chart(mortality_chart)
# 3. Visual 3: Child Mortality vs. Population
selected_country = st.selectbox("Country for Population vs. Mortality", countries, index=0)
mortality_country = child_mortality[child_mortality['country'] == selected_country].melt(
id_vars='country', var_name='year', value_name='child_mortality'
)
population_country = population[population['country'] == selected_country].melt(
id_vars='country', var_name='year', value_name='population'
)
merged_country_data = pd.merge(mortality_country, population_country, on=['country', 'year'], how='inner')
merged_country_data['year'] = merged_country_data['year'].astype(int)
st.write("""
### Child Mortality vs Population
This visualization shows the relationship between child mortality rates (per 1,000 live births) and population size over time for a selected country.
Displayed on a dual-y-axis chart, it allows users to explore how both metrics have evolved across decades.
The interactive feature lets users choose a country, and hovering over the chart reveals detailed tooltips with year, population size, and child mortality rate information.
""")
dual_axis_chart = alt.layer(
alt.Chart(merged_country_data).mark_line().encode(
x="year:O",
y="child_mortality:Q",
tooltip=["year", "child_mortality"]
),
alt.Chart(merged_country_data).mark_line(color='orange').encode(
x="year:O",
y="population:Q",
tooltip=["year", "population"]
)
).resolve_scale(y='independent').properties(width=700, height=400)
st.altair_chart(dual_axis_chart)
# 4. Visual 4: Child Mortality vs. Income
income_long = pd.melt(income, id_vars=['country'], var_name='year', value_name='income')
mortality_long = pd.melt(child_mortality, id_vars=['country'], var_name='year', value_name='mortality')
income_long['year'] = income_long['year'].astype(int)
mortality_long['year'] = mortality_long['year'].astype(int)
yay = pd.merge(income_long, mortality_long, on=['country', 'year'])
yeyear = st.slider("Year for Income vs. Mortality", min_value=1800, max_value=2024, value=2024)
filtered_yay = yay[yay["year"] == yeyear]
st.write("""
### Child Mortality vs Daily Income
The interactive scatterplot compares child mortality to daily income for 24 countries, with a slider to select a year between 1800 and 2024.
The x-axis represents daily income (USD) on a logarithmic scale, and the y-axis shows child mortality (per 1,000).
Each country is color-coded, and hover tooltips display details for each data point.
Our analysis reveals that in earlier years (e.g., 1800), countries have tightly clustered mortality and income.
By 2024, a negative correlation emerges, showing that as child mortality decreases, daily income increases.
""")
scatter_plot = alt.Chart(filtered_yay).mark_circle(size=60).encode(
x=alt.X('income', title='Daily Income (USD)', scale=alt.Scale(type='log')),
y=alt.Y('mortality', title='Child Mortality (per 1,000)'),
color='country',
tooltip=['country', 'year', 'income', 'mortality']
).properties(width=700, height=400)
st.altair_chart(scatter_plot)
# 5. Visual 5: Life Expectancy Trends
filtered_expectancy = life_expectancy[life_expectancy['country'].isin(selected_countries)]
expectancy_melted = filtered_expectancy.melt(id_vars="country", var_name="year", value_name="life_expectancy")
st.write("""
### Life Expectancy Trends
The scatter plot reveals a strong inverse relationship between child mortality and life expectancy, emphasizing how improvements in healthcare, nutrition, and living conditions enhance both indicators.
Notably, countries like India and South Africa show steep curves, reflecting rapid progress in life expectancy as child mortality declines.
Developed nations such as Australia and the USA exhibit a plateau, with incremental life expectancy gains as child mortality remains low.
""")
expectancy_chart = alt.Chart(expectancy_melted).mark_line().encode(
x="year:O",
y="life_expectancy:Q",
color="country:N",
tooltip=["country", "year", "life_expectancy"]
).properties(width=700, height=400)
st.altair_chart(expectancy_chart)
st.subheader("Child Mortality vs. Life Expectancy")
st.write("""
This scatter plot illustrates the strong inverse relationship between child mortality
rates and life expectancy at birth, underscoring how advancements in healthcare,
nutrition, and living conditions improve both indicators simultaneously. A particularly
striking observation is how the data for countries like India and South Africa forms a
steep curve, signifying rapid improvements in life expectancy as child mortality declines.
Developed nations such as Australia and the USA show a plateau in the later stages, where
child mortality rates are already low, and life expectancy improvements are incremental.
The trajectory of China’s data during the mid-20th century highlights a rapid transition,
likely due to systemic public health efforts.
""")
mortality_long = pd.melt(child_mortality, id_vars=["country"], var_name="year", value_name="child_mortality")
expectancy_long = pd.melt(life_expectancy, id_vars=["country"], var_name="year", value_name="life_expectancy")
mortality_long["year"] = mortality_long["year"].astype(int)
expectancy_long["year"] = expectancy_long["year"].astype(int)
merged_data = pd.merge(mortality_long, expectancy_long, on=["country", "year"])
merged_data = merged_data.dropna(subset=["child_mortality", "life_expectancy"])
default_countries = ["Argentina", "Australia", "China", "India", "South Africa", "UK", "USA"]
countries = merged_data["country"].unique()
selected_countries = st.multiselect("Select Countries", countries, default=default_countries)
filtered_data = merged_data[merged_data["country"].isin(selected_countries)]
scatter_chart = alt.Chart(filtered_data).mark_circle(size=60).encode(
x=alt.X("life_expectancy:Q", title="Life Expectancy at Birth"),
y=alt.Y("child_mortality:Q", title="Child Mortality (0–5 years per 1000 births)"),
color="country:N",
tooltip=["country", "year", "child_mortality", "life_expectancy"]
).properties(width=700, height=400)
st.altair_chart(scatter_chart)