Skip to content

Commit e7e83dd

Browse files
cgraham-rsdotNomad
andauthored
Add top-5-income-share-streamlit (#107)
* Add top-5-income-share-streamlit * Use example category --------- Co-authored-by: Jordan Jensen <[email protected]>
1 parent 777b333 commit e7e83dd

File tree

6 files changed

+933
-0
lines changed

6 files changed

+933
-0
lines changed

.github/workflows/extensions.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ jobs:
4545
fastapitableau-example: extensions/fastapitableau-example/**
4646
portfolio-report: extensions/portfolio-report/**
4747
quarto-script-r: extensions/quarto-script-r/**
48+
top-5-income-share-streamlit: extensions/top-5-income-share-streamlit/**
4849
usage-metrics-dashboard: extensions/usage-metrics-dashboard/**
4950
5051
# Runs for each extension that has changed from `simple-extension-changes`
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Top 5% income share
2+
3+
## About this example
4+
5+
A Streamlit application makes it easy to transform your analysis into an interactive dashboard using Python so users can ask and answer questions in real-time, without having to touch any code.
6+
7+
8+
## Learn more
9+
10+
* [Streamlit Getting Started Guide](https://docs.streamlit.io/en/latest/getting_started.html)
11+
* [User Guide: Streamlit](https://docs.posit.co/connect/user/streamlit/)
12+
13+
## Requirements
14+
15+
* Python version 3.9 or higher
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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

Comments
 (0)