|
| 1 | +import os.path |
| 2 | + |
| 3 | +import altair as alt |
| 4 | +import pandas as pd |
| 5 | +import streamlit as st |
| 6 | + |
| 7 | +HERE = os.path.dirname(os.path.abspath(__file__)) |
| 8 | + |
| 9 | +st.title("Top 5%" " income share") |
| 10 | +st.markdown("Share of income received by the richest 5%" " of the population.") |
| 11 | +DATA = os.path.join(HERE, "data.csv") |
| 12 | + |
| 13 | + |
| 14 | +@st.cache_data |
| 15 | +def load_data(nrows): |
| 16 | + return pd.read_csv("./data.csv", nrows=nrows) |
| 17 | + |
| 18 | + |
| 19 | +data_load_state = st.text("Loading data...") |
| 20 | +data = load_data(10000) |
| 21 | +data_load_state.text("") |
| 22 | + |
| 23 | +countries = st.multiselect( |
| 24 | + "Countries", |
| 25 | + list(sorted({d for d in data["Entity"]})), |
| 26 | + default=["Australia", "China", "Germany", "Japan", "United States"], |
| 27 | +) |
| 28 | +earliest_year = data["Year"].min() |
| 29 | +latest_year = data["Year"].max() |
| 30 | +min_year, max_year = st.slider( |
| 31 | + "Year Range", |
| 32 | + min_value=int(earliest_year), |
| 33 | + max_value=int(latest_year), |
| 34 | + value=[int(earliest_year), int(latest_year)], |
| 35 | +) |
| 36 | +filtered_data = data[data["Entity"].isin(countries)] |
| 37 | +filtered_data = filtered_data[filtered_data["Year"] >= min_year] |
| 38 | +filtered_data = filtered_data[filtered_data["Year"] <= max_year] |
| 39 | + |
| 40 | +chart = ( |
| 41 | + alt.Chart(filtered_data) |
| 42 | + .mark_line() |
| 43 | + .encode( |
| 44 | + x=alt.X("Year", axis=alt.Axis(format="d")), |
| 45 | + y=alt.Y("Percent", axis=alt.Axis(format="~s")), |
| 46 | + color="Entity", |
| 47 | + strokeDash="Entity", |
| 48 | + ) |
| 49 | +) |
| 50 | +st.altair_chart(chart, use_container_width=True) |
| 51 | + |
| 52 | +if st.checkbox("Show raw data"): |
| 53 | + st.subheader("Raw data") |
| 54 | + st.write(filtered_data) |
| 55 | + |
| 56 | +st.markdown("Source: <https://ourworldindata.org/grapher/top-5-income-share>") |
0 commit comments