/
app.py
121 lines (94 loc) · 3.12 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""
A simple streamlit app demo.
"""
import streamlit as st # Import the streamlit package to the app.
import pandas as pd # import the pandas package to handle data.
# Page setting - Set's your application's layout, title and icon.
# "KE" is the shortcode for an emoji of the Kenyan flag
# Check all supported emojis here - https://raw.githubusercontent.com/omnidan/node-emoji/master/lib/emoji.json
st.set_page_config(
layout="wide", page_title="Visualising City Population, Kenya", page_icon="🇰🇪"
)
# The code below use the inbuilt streamlit "write" functionality and
# Markdown Language to write formated texts on your app.
# You can learn more about Markdown by Googling it.
st.write(
"""
### My First Streamlit Application
#### This app will display graphs of the populations of several Kenya towns.
#### Data used for Demo purposes only. Not accurate!
"""
)
# In the following code section, we shall create a new pandas data frame, read data to it and
# do some simple analysis.
df = pd.read_csv(
"data/kenyan_town_populations.csv"
) # Creates a dataframe df and loads data to it from the csv file.
# The code below creates and dsiplays a table with an heading.
st.write(
"""
##### Table 1: Population of Kenya Towns.
"""
)
st.table(df) # Using streamlit's table functionality to draw a table of our data
st.markdown("""---""") # Horizontal seperator
# Calculating the average population in all towns.
average_population = df["Population"].mean()
# Add two new columns to the data frame showing the percentage of population that is male or female
df["Male_Percent"] = [
55,
45,
53,
52,
47,
54,
] # Say I know Male %. Just attach a new column to df
df["Female_Percent"] = (
100 - df["Male_Percent"]
) # Calculate Female % by subtracting Male % from 100
# Add a row of means for all numerical columns
df.loc["Mean_Value"] = df.mean(numeric_only=True)
st.write(
"""
##### Table 2: Population of Kenya Towns Male / Female.
"""
)
st.table(df)
# Better textual Visualisation
# Create three Columns in the app and do some visualisation
st.write(
"""
##### Key Metrics:
"""
)
a1, a2, a3, a4 = st.columns(4) # Creat for columns
a1.image("data/kenya.png", width=200) # Add an image to col 1
a2.metric(
"Largest City", "Nairobi", +2_000_000
) # Display highlighted data with stylised text
a3.metric(
"Smallest City", "Nyeri", -1_000_000
) # Display another highleted data with stylised text
a4.metric(
"Average Population", f"{average_population}", "+15.37%"
) # Note the f-string? Nice solution when using variables.
st.markdown("""---""") # Horizontal seperator
# Graphical Visualisation of the data
# Create 2 columns in the app
st.write(
"""
##### Visualisation: Bar / Line Charts showing Population Per City
"""
)
b1, b2 = st.columns(2)
with b1:
st.bar_chart(
df,
x="City",
y="Population",
) # Barchart visualising the populations of the cities
with b2:
st.line_chart(
df, x="City", y="Population"
) # Line chart visualising the population of the cities.
st.markdown("""---""") # Horizontal seperator