-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdashboard.py
286 lines (250 loc) · 11.9 KB
/
dashboard.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# Importing the libraries
import streamlit as st
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import matplotlib.pyplot as plt
from PIL import Image
# Setting page configurations
st.set_page_config(page_title = 'Dashboard Book Recomendation', layout='wide', initial_sidebar_state='expanded')
# Creating initial apresentation
st.markdown("<h1 style = 'text-align: center;'> Analysing Book Rating Dataset </h1>", unsafe_allow_html = True)
col1_, col2_, col3_ = st.columns(3)
image = Image.open('book.png')
col2_.image(image, use_column_width=True)
# Reading the dataset
dataviz = pd.read_csv('dataviz.csv')
# ---------SIDEBAR--------
st.sidebar.title('Menu')
#---------MENU---------
selectPage = st.sidebar.selectbox('Select a option', ['Home Page', 'Dashboard', 'Dataframe'])
st.sidebar.markdown('''
---
Created 👩🎓 by [Diele Monteiro](https://www.linkedin.com/in/diele-monteiro/).
''')
#----------HOME PAGE--------------
if selectPage == 'Home Page':
st.header(':books: Welcome to my App')
st.markdown("<h4 style = 'text-align: left;'> A Web App by <b><a href = 'https://github.com/dielemonteiro'> Diele Monteiro </a></b></h4>", unsafe_allow_html = True)
st.write(f"""
This application's purpose is to analyze a dataset about books based on Data Visualization in order to develop a recommendation system, using Content and Collaborative filtering.
This application aims to analyze the dashboard and collect relevant insights that can contribute to building the recommendation system.
The dashboard has been modelled for a target audience of young adults (18 - 35 years old), so you'll find a modern, visually appealing design with an intuitive, easy-to-use interface.
*Please browse the Menu tab to find all features of this App* """)
elif selectPage == 'Dashboard':
st.title('Overview:')
st.markdown('##')
#TOP KPI's
total_book = int(dataviz['Book-Title'].count())
averange_rating = round(dataviz['Book-Rating'].mean(), 1)
star_rating = ':star:' * int(round(averange_rating, 0))
left_column_mainpage, right_column_mainpage = st.columns(2)
with left_column_mainpage:
st.subheader('Total of Books:')
st.subheader(f'{total_book:,}')
with right_column_mainpage:
st.subheader('Averange Rating:')
st.subheader(f'{averange_rating}{star_rating}')
st.markdown('---')
# ROW 1
# Plotting the distribution of 'Country'
st.title(':bar_chart: Dashboard')
st.markdown('## **#Country Distribuition**')
country_counts_map = dataviz['Country'].value_counts()
country_data = pd.DataFrame({'Country': country_counts_map.index, 'Count': country_counts_map.values})
fig1 = px.choropleth(country_data, locations = 'Country',
locationmode='country names',
color = 'Count',
hover_name = 'Country',
scope="north america",
color_continuous_scale = px.colors.sequential.Viridis)
fig2 = px.bar(dataviz.value_counts('Country', ascending = False),
x = dataviz.value_counts('Country', ascending = False).index,
y = dataviz.value_counts('Country', ascending = False),
color = 'count',
color_continuous_scale = px.colors.sequential.Viridis,
title = "The Country with more ratings rated",
text_auto='.2s')
fig2.update_layout(height = 600, width = 1000, xaxis_title = 'Rating Count',
yaxis_title = 'Location')
fig2.update_yaxes(automargin = True, title_standoff = 10)
# Creating the streamlit layout
col1, col2 = st.columns(2)
with col1:
st.subheader('Map Visualization')
st.plotly_chart(fig1)
with col2:
st.subheader('Total of Book per Country')
st.plotly_chart(fig2)
# ROW 2
# Plotting the distribution of 'Age'
st.markdown('## **#Age Distribuition**')
def set_age_category(v):
if (v<= 10):
return "age group 0-10"
elif (v>10)&(v<=19):
return "age group 11-19"
elif (v>19)&(v<=29):
return "age group 20-29"
elif (v>29)&(v<=39):
return "age group 30-39"
elif (v>39)&(v<=49):
return "age group 40-49"
elif (v>49)&(v<=59):
return "age group 50-59"
elif(v>60):
return "age group > 60"
temp = dataviz.copy()
temp.dropna(subset=['Age'], inplace=True)
temp['age_category'] = temp['Age'].apply(set_age_category)
age_group = temp.groupby('age_category').size().reset_index(name='count')
age_group = age_group.sort_values(by='age_category', ascending=False)
fig3 = px.bar(age_group,
x='age_category',
y='count',
color='count',
color_continuous_scale = px.colors.sequential.Viridis,
title="Distribution of Books by Age Category",
text_auto='.2s', width=1400, height=600)
fig3.update_layout(
xaxis_title='Age Category',
yaxis_title='Count',
title_font=dict(size=24),
xaxis=dict(tickfont=dict(size=18)),
yaxis=dict(tickfont=dict(size=18))
)
st.plotly_chart(fig3)
# ROW 3
# Plotting the Book Ratings
st.markdown('## **#Book Ratings**')
st.markdown('#### **Top 10 of books with more ratings**')
top_books = dataviz['Book-Title'].value_counts().nlargest(10)
top_books_df = pd.DataFrame({'title': top_books.index, 'occurances': top_books.values})
fig4 = px.bar(top_books_df,
x='occurances',
y='title',
color='occurances',
color_continuous_scale = px.colors.sequential.Viridis,
orientation='h',
labels={'occurances': 'Number of Occurrence', 'title': 'Books'})
fig4.update_layout(
height=700,
width=850,
xaxis=dict(tickfont=dict(size=18)),
yaxis=dict(tickfont=dict(size=18)),
title_font=dict(size=14)
)
# Adicionar título e subtítulo ao gráfico
fig4.update_layout(
title={
'text': 'Top 10 Books with More Occurrences',
'y': 0.95,
'x': 0.5,
'xanchor': 'center',
'yanchor': 'top'
},
)
# Plotting the top10 books per country
st.subheader('You can filter per Country:')
all_countries_book = dataviz['Country'].unique()
default_country_book = all_countries_book[0]
countries = st.multiselect(
'Select the Country:',
options=all_countries_book,
default=default_country_book,
key='countries_multiselect'
)
if not countries:
countries = all_countries_book
top_books_by_country = dataviz.groupby(['Country', 'Book-Title']).size().groupby(level=0).nlargest(10).reset_index(level=0, drop=True).reset_index(name='Occurances')
selection_country = top_books_by_country[top_books_by_country['Country'].isin(countries)]
# Creating the streamlit layout
column1, column2 = st.columns([0.6, 0.4])
with column1:
st.plotly_chart(fig4)
with column2:
st.write('Top 10 Book per Country')
st.dataframe(selection_country)
# ROW 4
# Plotting the Author Ratings
st.markdown('## **#Author Ratings**')
import plotly.graph_objects as go
most_books = dataviz.groupby('Book-Author')['Book-Title'].count().reset_index().sort_values('Book-Title', ascending=False).head(5)
fig5 = go.Figure(data=[go.Bar(
x=most_books['Book-Title'],
y=most_books['Book-Author'],
orientation='h',
marker=dict(
color=most_books['Book-Title'],
colorscale='viridis',
line=dict(color='rgba(31, 119, 180, 1.0)', width=1),
)
)])
fig5.update_layout(
title="Top 5 authors with most books",
xaxis_title="Total number of books",
yaxis_title="Authors",
height=600,
width=800,
bargap=0.2,
bargroupgap=0.1,
template='plotly_white',
xaxis=dict(tickfont=dict(size=18)),
yaxis=dict(tickfont=dict(size=18)),
title_font=dict(size=14)
)
st.plotly_chart(fig5)
# ROW 6
# Plotting the Top Author
st.markdown('### **Top 5 books by author Stephen King**')
author_1 = ['Stephen King']
search_author_1 = dataviz[dataviz['Book-Author'].str.contains('|'.join(author_1))]
author_1_top5 = search_author_1.sort_values(by='Book-Rating', ascending=False).head(5)
author_1_top5 = author_1_top5.reset_index()
columns_to_keep = ['Book-Title', 'ISBN', 'Year-Of-Publication', 'Publisher', 'Image-URL-L', 'Book-Rating']
author_1_top5 = author_1_top5.loc[:, columns_to_keep]
st.dataframe(author_1_top5)
st.markdown('### **Top 5 books by author Nora Roberts**')
author_2 = ['Nora Roberts']
search_author_2 = dataviz[dataviz['Book-Author'].str.contains('|'.join(author_2))]
author_2_top5 = search_author_2.sort_values(by='Book-Rating', ascending=False).head(5)
author_2_top5 = author_2_top5.reset_index()
columns_to_keep = ['Book-Title', 'ISBN', 'Year-Of-Publication', 'Publisher', 'Image-URL-L', 'Book-Rating']
author_2_top5 = author_2_top5.loc[:, columns_to_keep]
st.dataframe(author_2_top5)
st.markdown('### **Top 5 books by author ames Patterson**')
author_3 = ['James Patterson']
search_author_3 = dataviz[dataviz['Book-Author'].str.contains('|'.join(author_3))]
author_2_top3 = search_author_3.sort_values(by='Book-Rating', ascending=False).head(5)
author_2_top3 = author_2_top3.reset_index()
columns_to_keep = ['Book-Title', 'ISBN', 'Year-Of-Publication', 'Publisher', 'Image-URL-L', 'Book-Rating']
author_2_top3 = author_2_top3.loc[:, columns_to_keep]
st.dataframe(author_2_top3)
st.markdown('### **Top 5 books by author ohn Grisham**')
author_4 = ['John Grisham']
search_author_4 = dataviz[dataviz['Book-Author'].str.contains('|'.join(author_4))]
author_2_top4 = search_author_4.sort_values(by='Book-Rating', ascending=False).head(5)
author_2_top4 = author_2_top4.reset_index()
columns_to_keep = ['Book-Title', 'ISBN', 'Year-Of-Publication', 'Publisher', 'Image-URL-L', 'Book-Rating']
author_2_top4 = author_2_top4.loc[:, columns_to_keep]
st.dataframe(author_2_top4)
st.markdown('### **Author - Mary Higgins Clark**')
author_5 = ['Mary Higgins Clark']
search_author_5 = dataviz[dataviz['Book-Author'].str.contains('|'.join(author_5))]
author_2_top5 = search_author_5.sort_values(by='Book-Rating', ascending=False).head(5)
author_2_top5 = author_2_top5.reset_index()
columns_to_keep = ['Book-Title', 'ISBN', 'Year-Of-Publication', 'Publisher', 'Image-URL-L', 'Book-Rating']
author_2_top5 = author_2_top5.loc[:, columns_to_keep]
st.dataframe(author_2_top5)
elif selectPage == 'Dataframe':
st.title('Dataframe')
st.sidebar.header('Please filter here:')
all_countries = dataviz['Country'].unique()
default_country = all_countries[0]
countries = st.sidebar.multiselect(
'Select the Countries:',
options=all_countries,
default=default_country
)
dataviz_selection = dataviz[dataviz['Country'].isin(countries)]
st.dataframe(dataviz_selection)