Dashboard Python
Dashboard Python
1 de 18 13/09/2022, 5:33
Create a Live Dashboard with Python and Streamlit | by Gonçalo Ch... https://medium.com/@goncalorrc/create-a-live-dashboard-with-pytho...
data = pd.read_csv('data.csv')
2 de 18 13/09/2022, 5:33
Create a Live Dashboard with Python and Streamlit | by Gonçalo Ch... https://medium.com/@goncalorrc/create-a-live-dashboard-with-pytho...
values = data[column_name].tolist()
distribution = {}
total = 0
for value in values:
total = total + 1
if value not in distribution:
distribution[value] = 1
else:
distribution[value] = distribution[value] + 1
3 de 18 13/09/2022, 5:33
Create a Live Dashboard with Python and Streamlit | by Gonçalo Ch... https://medium.com/@goncalorrc/create-a-live-dashboard-with-pytho...
df = pd.DataFrame.from_dict(distribution,orient='index')
df = df.rename(columns = {0:'Percentage'})
return df
get_distribution(data, ‘Gender')
+========+============+
| - | Percentage |
+========+============+
| Female | 0.501 |
+--------+------------+
| Male | 0.499 |
+--------+------------+
dates = []
4 de 18 13/09/2022, 5:33
Create a Live Dashboard with Python and Streamlit | by Gonçalo Ch... https://medium.com/@goncalorrc/create-a-live-dashboard-with-pytho...
count = np.zeros_like(dates,dtype=int)
for date_str in data['Sign Up Date'].tolist():
date = dt.datetime.strptime(date_str, '%Y-%m-%d')
for i in range(len(dates)):
if date.month == dates[i].month and date.year ==
dates[i].year:
count[i] = count[i] + 1
df = pd.DataFrame({'Dates':dates, 'Count':count})
df = df.set_index('Dates')
return df
def accumulated_signups(df):
new_df = df.copy()
total = 0
for index, row in df.iterrows():
total = total + row['Count']
new_df.at[index, 'Count'] = total
keys = data[key_variable]
values = data[value_variable]
5 de 18 13/09/2022, 5:33
Create a Live Dashboard with Python and Streamlit | by Gonçalo Ch... https://medium.com/@goncalorrc/create-a-live-dashboard-with-pytho...
info = {}
aux = {} # to hold all the values to compute the mean
for key, value in zip(keys, values):
if key in info:
aux[key].append(value)
info[key][0] = np.around(np.mean(aux[key]),2)
else:
info[key] = [[]]
aux[key] = []
df = pd.DataFrame.from_dict(info,orient='index')
df = df.rename(columns = {0:value_variable})
return df
+============+=========+
| Profession | Salary |
+============+=========+
| Accountant | 2098.76 |
+------------+---------+
| Secretary | 2151.88 |
+------------+---------+
| Dentist | 2166.76 |
+------------+---------+
| ... | ... |
+------------+---------+
def generate_age_groups():
groups = {}
for i in range(0, 100):
groups[i] = math.floor(i/10)
return groups
6 de 18 13/09/2022, 5:33
Create a Live Dashboard with Python and Streamlit | by Gonçalo Ch... https://medium.com/@goncalorrc/create-a-live-dashboard-with-pytho...
if column == 'Age':
search_term = int(search_term)
indexes = data.loc[data[column].isin([search_term])].index
if indexes.size > 0:
return data.iloc[indexes]
else:
return []
import streamlit as st
7 de 18 13/09/2022, 5:33
Create a Live Dashboard with Python and Streamlit | by Gonçalo Ch... https://medium.com/@goncalorrc/create-a-live-dashboard-with-pytho...
st.write("Hello")
streamlit
8 de 18 13/09/2022, 5:33
Create a Live Dashboard with Python and Streamlit | by Gonçalo Ch... https://medium.com/@goncalorrc/create-a-live-dashboard-with-pytho...
st.columns()
with col2:
st.write("This is in column 2")
with col3:
st.write("And column 3")
with col4:
st.write("This column is half the size of 2 and 3")
9 de 18 13/09/2022, 5:33
Create a Live Dashboard with Python and Streamlit | by Gonçalo Ch... https://medium.com/@goncalorrc/create-a-live-dashboard-with-pytho...
import pandas as pd
data = pd.read_csv(Path(__file__).parents[0]/'data.csv')
df = pd.DataFrame()
with col2:
key = st.selectbox("Key",
['Name','Email','Age','Gender','Country','Sign Up Date',
'Profession','Salary'])
with col3:
search_term = st.text_input("Search")
if key != '' and search_term != '':
df = search(data, key, search_term)
10 de 18 13/09/2022, 5:33
Create a Live Dashboard with Python and Streamlit | by Gonçalo Ch... https://medium.com/@goncalorrc/create-a-live-dashboard-with-pytho...
with col2:
if not df.empty:
st.dataframe(df)
else:
st.write('Did not find any person matching the criteria')
st.dataframe()
st.bar_chart()
11 de 18 13/09/2022, 5:33
Create a Live Dashboard with Python and Streamlit | by Gonçalo Ch... https://medium.com/@goncalorrc/create-a-live-dashboard-with-pytho...
st.markdown('***') #separator
with col2:
st.markdown("<h5 style='text-align: center; color: black;'>Gender
Distribution</h1>", unsafe_allow_html=True)
with col3:
st.markdown("<h5 style='text-align: center; color: black;'>Age
Distribution</h1>", unsafe_allow_html=True)
st.bar_chart(get_distribution(data, 'Age'))
with col4:
st.markdown("<h5 style='text-align: center; color:
black;'>Country Distribution</h1>", unsafe_allow_html=True)
12 de 18 13/09/2022, 5:33
Create a Live Dashboard with Python and Streamlit | by Gonçalo Ch... https://medium.com/@goncalorrc/create-a-live-dashboard-with-pytho...
def pie_chart(df):
labels = list(df.index)
values = list(df['Percentage'])
fig, ax = plt.subplots()
ax.pie(values,labels=labels, autopct='%1.1f%%')
ax.axis('equal') # Equal aspect ratio ensures that pie is drawn
as a circle.
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
return fig
st.markdown('***') #separator
with col2:
st.markdown("<h5 style='text-align: center; color: black;'>Gender
Distribution</h1>", unsafe_allow_html=True)
st.pyplot(pie_chart(get_distribution(data, 'Gender')))
with col3:
st.markdown("<h5 style='text-align: center; color: black;'>Age
Distribution</h1>", unsafe_allow_html=True)
st.bar_chart(get_distribution(data, 'Age'))
with col4:
st.markdown("<h5 style='text-align: center; color:
black;'>Country Distribution</h1>", unsafe_allow_html=True)
st.pyplot(pie_chart(get_distribution(data, 'Country')))
13 de 18 13/09/2022, 5:33
Create a Live Dashboard with Python and Streamlit | by Gonçalo Ch... https://medium.com/@goncalorrc/create-a-live-dashboard-with-pytho...
st.bar_chart()
st.markdown('***') #separator
with col2:
st.markdown("<h5 style='text-align: center; color:
black;'>Average salary per profession</h1>", unsafe_allow_html=True)
st.bar_chart(relate_data(data, 'Profession', 'Salary'))
with col3:
st.markdown("<h5 style='text-align: center; color:
black;'>Average salary per age group</h1>", unsafe_allow_html=True)
st.bar_chart(relate_data(data, 'Age', 'Salary'))
14 de 18 13/09/2022, 5:33
Create a Live Dashboard with Python and Streamlit | by Gonçalo Ch... https://medium.com/@goncalorrc/create-a-live-dashboard-with-pytho...
st.line_chart()
st.markdown('***') #separator
with col2:
15 de 18 13/09/2022, 5:33
Create a Live Dashboard with Python and Streamlit | by Gonçalo Ch... https://medium.com/@goncalorrc/create-a-live-dashboard-with-pytho...
pipreqs /path/to
/project
pipreqs
16 de 18 13/09/2022, 5:33
Create a Live Dashboard with Python and Streamlit | by Gonçalo Ch... https://medium.com/@goncalorrc/create-a-live-dashboard-with-pytho...
17 de 18 13/09/2022, 5:33
Create a Live Dashboard with Python and Streamlit | by Gonçalo Ch... https://medium.com/@goncalorrc/create-a-live-dashboard-with-pytho...
18 de 18 13/09/2022, 5:33