Grupo 5 Building A Dashboard in Python Using Streamlit
Grupo 5 Building A Dashboard in Python Using Streamlit
Building a dashboard in
Python using Streamlit
Using pandas for data wrangling, Altair/
Plotly for data visualization, and
Streamlit as your frontend
By Chanin Nantasenamat
Posted in Tutorials, January 22 2024
Hello there �
Thanks for stopping by! We use cookies to help us understand how you interact with
our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please
see our privacy policy.
Reject all
Accept all
Cookie settings
1 de 20 6/06/24, 18:51
Building a dashboard in Python using Streamlit https://blog.streamlit.io/crafting-a-dashboard-app-in-p...
As useful as these can be, a typical chart that we may see in print or
on the web are most likely static. Imagine how much more engaging it
would be to manipulate these static variables in an interactive
dashboard?
� Ready to jump right in? Here’s the dashboard app and GitHub
repo.
In thisthere
Hello blog, �
you’ll learn how to build a Population Dashboard app that
displays data and visualizations of the US population for the years
Thanks for stopping by! We use cookies to help us understand how you interact with
2010�2019 as obtained from the US Census Bureau.
our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please
I'll guide you through the process of building this interactive
see our privacy policy.
dashboard app from scratch using Streamlit for the frontend. Our
backend muscle comes from PyData
Reject heavyweights
all like NumPy,
Pandas, Scikit-Learn, and Altair, ensuring robust data processing and
analytics. Accept all
Cookie settings
You’ll learn how to:
2 de 20 6/06/24, 18:51
Building a dashboard in Python using Streamlit https://blog.streamlit.io/crafting-a-dashboard-app-in-p...
our website.
1.1 Overview
By clicking of consent
“Accept all”, you key metrics
to our use of cookies. For more information, please
see our privacy policy.
The goal of any dashboard is to surface insights that provide the basis
for data-driven decision making.Reject
Whatallis the primary purpose of the
dashboard? This will guide the subsequent questions you want the
Accept all
dashboard to answer in the form of key metrics.
Cookie settings
For example:
3 de 20 6/06/24, 18:51
Building a dashboard in Python using Streamlit https://blog.streamlit.io/crafting-a-dashboard-app-in-p...
John Tukey's seminal work on EDA in 1977 meticulously sets the stage
4 de 20 6/06/24, 18:51
Building a dashboard in Python using Streamlit https://blog.streamlit.io/crafting-a-dashboard-app-in-p...
John Tukey's seminal work on EDA in 1977 meticulously sets the stage
for effective data communication. Here are some notable key
takeaways:
our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please
2.3
see ourSelect charts to best visualize our key
privacy policy.
metrics
Reject all
5 de 20 6/06/24, 18:51
Building a dashboard in Python using Streamlit https://blog.streamlit.io/crafting-a-dashboard-app-in-p...
app.
You can discover even more visualization options from the growing
collection of custom components that the community. Here’s a few
that you can try out:
By clicking “Accept all”, you consent to our use of cookies. For more information, please
Reject all
Cookie settings
6 de 20 6/06/24, 18:51
Building a dashboard in Python using Streamlit https://blog.streamlit.io/crafting-a-dashboard-app-in-p...
import streamlit as st
import pandas as pd
import altair as alt
import plotly.express as px
Here, we also set the color theme for the Altair plot to be dark in order
to accompany dark color theme of the app.
st.set_page_config(
page_title="US Population Dashboard",
�",
page_icon="�
layout="wide",
initial_sidebar_state="expanded")
Hello there �
alt.themes.enable("dark")
Thanks for stopping by! We use cookies to help us understand how you interact with
our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please
3.3 Load data
see our privacy policy.
Cookie settings
df_reshaped = pd.read_csv('data/us-population-2010-2019-reshaped.csv'
7 de 20 6/06/24, 18:51
Building a dashboard in Python using Streamlit https://blog.streamlit.io/crafting-a-dashboard-app-in-p...
with st.sidebar:
� US Population Dashboard')
st.title('�
year_list = list(df_reshaped.year.unique())[::-1]
our website.
Heatmap
By clicking “Accept all”, you consent to our use of cookies. For more information, please
see our privacy policy.
A heatmap will allow us to see the population growth over the years
from 2010�2019 for the 52 states.
Reject all
Accept all
Cookie settings
8 de 20 6/06/24, 18:51
Building a dashboard in Python using Streamlit https://blog.streamlit.io/crafting-a-dashboard-app-in-p...
Choropleth map
Hello there �
Thanks for stopping by! We use cookies to help us understand how you interact with
our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please
see our privacy policy.
Reject all
Choropleth map of the US population for a selected year of interest �2019 in
Accept all
this case).
Cookie settings
9 de 20 6/06/24, 18:51
Building a dashboard in Python using Streamlit https://blog.streamlit.io/crafting-a-dashboard-app-in-p...
Donut chart
Next, we’re going to create a donut chart for the states migration in
percentage.
Hello there �
Donut chart of the percentage of states with annual inbound and outbound
Thanks for stopping by! We use cookies to help us understand how you interact with
migration greater than 50,000 in 2019.
our website.
Particularly, thisall”,
By clicking “Accept represents
you consentthe percentage
to our of states
use of cookies. with
For more annualplease
information,
inbound or outbound
see our privacy policy. migration � 50,000 people. For instance, in 2019,
there were 12 out of 52 states and this corresponds to 23%.
Reject all
Before creating the donut chart, we’ll need to calculate the year-over-
Accept all
year population migrations.
Cookie settings
10 de 20 6/06/24, 18:51
Building a dashboard in Python using Streamlit https://blog.streamlit.io/crafting-a-dashboard-app-in-p...
source = pd.DataFrame({
"Topic": ['', input_text],
"% value": [100-input_response, input_response]
})
source_bg = pd.DataFrame({
"Topic": ['', input_text],
"% value": [100, 0]
})
11 de 20 6/06/24, 18:51
Building a dashboard in Python using Streamlit https://blog.streamlit.io/crafting-a-dashboard-app-in-p...
# domain=['A', 'B'],
domain=[input_text, ''],
range=chart_color), # 31333F
legend=None),
).properties(width=130, height=130)
return plot_bg + plot + text
Hello there �
def format_number(num):
Thanks for stopping by! We use cookies to help us understand how you interact with
if num > 1000000:
our website.
if not num % 1000000:
By clicking “Accept all”, you consent to our use of cookies. For more information, please
return f'{num // 1000000} M'
see our privacy policy.
return f'{round(num / 1000000, 1)} M'
return f'{num // 1000} K'
Reject all
Accept all
12 de 20 6/06/24, 18:51
Building a dashboard in Python using Streamlit https://blog.streamlit.io/crafting-a-dashboard-app-in-p...
Column 1
with col[0]:
st.markdown('#### Gains/Losses')
df_population_difference_sorted = calculate_population_difference
13 de 20 6/06/24, 18:51
Building a dashboard in Python using Streamlit https://blog.streamlit.io/crafting-a-dashboard-app-in-p...
last_state_delta = format_number(df_population_difference_sorted
else:
last_state_name = '-'
last_state_population = '-'
last_state_delta = ''
st.metric(label=last_state_name, value=last_state_population
Hello there �
Thanks for stopping by! We use cookies to help us understand how you interact with
Column 2
our website.
By clicking
Next, the“Accept
second all”,column
you consent to our the
displays use of cookies. Formap
choropleth more and
information,
heatmap please
Reject all
with col[1]:
Accept all
st.markdown('#### Total Population')
Cookie settings
choropleth = make_choropleth(df_selected_year, 'states_code'
st plotly_chart(choropleth use_container_width=True)
14 de 20 6/06/24, 18:51
Building a dashboard in Python using Streamlit https://blog.streamlit.io/crafting-a-dashboard-app-in-p...
st.plotly_chart(choropleth, use_container_width=True)
Column 3
Finally, the third column shows the top states via a dataframe
whereby the population are shown as a colored progress bar via the
column_config parameter of st.dataframe .
with col[2]:
st.markdown('#### Top States')
st.dataframe(df_selected_year_sorted,
column_order=("states", "population"),
hide_index=True,
width=None,
column_config={
"states": st.column_config.TextColumn(
"States",
),
"population": st.column_config.ProgressColumn
"Population",
format="%f",
min_value=0,
max_value=max(df_selected_year_sorted
)}
Hello there �
)
Thanks for stopping by! We use cookies to help us understand how you interact with
our website.
with st.expander('About', expanded=True):
By clicking “Accept all”, you consent to our use of cookies. For more information, please
st.write('''
see our privacy policy.
- Data: [U.S. Census Bureau](<https://www.census.gov/data/datase
- :orange[**Gains/Losses**]: states with high inbound/ outbound
Reject all
- :orange[**States Migration**]: percentage of states with annua
''')
Accept all
Cookie settings
Wrapping up
In summary, Streamlit offers a quick, efficient, and code-friendly way
to build interactive dashboard apps in Python, making it a go-to tool
for data scientists and developers working with data visualization.
Hello there �
Thanks for stopping by! We use cookies to help us understand how you interact with
our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please
see our privacy policy.
Reject all
Accept all
Cookie settings
16 de 20 6/06/24, 18:51
Building a dashboard in Python using Streamlit https://blog.streamlit.io/crafting-a-dashboard-app-in-p...
Happy Streamlit-ing! �
Comments
4 replies
1 reply
Hello there �
1 reply
Thanks for stopping by! We use cookies to help us understand how you interact with
asehmi Streamlit Creator ▶ dataprofessor 29 Jan
our website.
Thanks for reminding me about @andfanilo’s cool videos on that
By clickingtopic.
“Accept all”, you consent
Dashboards needto our use of cookies.
to minimize timeFor
tomore information,
insights please
so cross-
filtering
see our privacy is critical I think.
policy.
Accept all
Continue the conversation in our forums →
Cookie settings
17 de 20 6/06/24, 18:51