-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_panel_bokeh_dashboard.py
221 lines (191 loc) · 6.26 KB
/
1_panel_bokeh_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
# ---
# jupyter:
# jupytext:
# text_representation:
# extension: .py
# format_name: light
# format_version: '1.4'
# jupytext_version: 1.2.1
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---
# # Dashboard with Panel, Holoviews and Bokeh for Spotify datasets
#
# Some documentation pages that helped me:
# - http://holoviews.org/user_guide/Plotting_with_Bokeh.html
# - https://bokeh.pydata.org/en/latest/docs/user_guide/tools.html#custom-tooltip
# - https://github.com/pyviz-demos/glaciers
# +
from pathlib import Path
import holoviews as hv
import pandas as pd
import panel as pn
from bokeh.models import HoverTool
hv.extension("bokeh")
# -
# ## Get and preprocess datasets
#
# - Converting some column types.
# - Clipping some columns to avoid irrelevant outliers.
# - Creating columns for direct plotting.
DATA_DIR = Path("data/")
albums = pd.read_json(DATA_DIR / "albums_features.json")
artists = pd.read_json(DATA_DIR / "artists_features.json")
albums["name"] = albums["name"].astype(str)
albums["release_date"] = pd.to_datetime(albums.release_date)
albums["year"] = albums.release_date.dt.year
albums["decade"] = albums.year.astype(str).str[:3] + "0"
albums["loudness"] = albums.loudness.clip(
albums.loudness.quantile(0.05), albums.loudness.quantile(0.95)
)
# ## Build the main visualizations
def create_artists_points(data):
artists_tooltips = """
<div>
<div>
<img
src="@image" height="70" alt="@image" width="70"
style="float: left; margin: 0px 15px 15px 0px;"
border="1"
></img>
</div>
<div>
<span style="font-size: 15px;"><b>@name</b></span>
</div>
<div>
<span style="font-weight: bold;">Main genre:</span>
<span>@genre_cluster</span>
</div>
<div>
<span style="font-weight: bold;">Subgenre:</span>
<span>@genre_specific</span>
</div>
</div>
"""
artists_hover = HoverTool(tooltips=artists_tooltips)
artists_points = hv.Points(
data=data, kdims=["genre_x", "genre_y"],
vdims=["genre_cluster", "genre_specific", "popularity", "image", "name"]
)
artists_points.opts(
tools=["box_select", "lasso_select", artists_hover, "tap"],
color="genre_cluster", cmap="dark2",
line_color="black", size=hv.dim("popularity")/5,
padding=0.1, width=800, height=600, show_grid=False, show_frame=False,
xaxis="bare", yaxis="bare", title="Artists"
)
return artists_points
def create_albums_points(data=None, x=None, y=None, color=None):
if data is None:
data = albums
x = x or x_select.value
y = y or y_select.value
color = color or color_select.value
albums_tooltips = """
<div>
<div>
<img
src="@image" height="70" alt="@image" width="70"
style="float: left; margin: 0px 15px 15px 0px;"
border="2"
></img>
</div>
<div>
<span>@artist_name</span>
</div>
<div>
<span>@name</span>
</div>
<div>
<span>@year</span>
</div>
</div>
"""
albums_hover = HoverTool(tooltips=albums_tooltips)
albums_points = hv.Points(
data, [x, y],
["name", "artist_name", "image", "year", color]
)
albums_points.opts(
tools=[albums_hover], color=color, cmap="viridis",
line_color="black", size=10, colorbar=True,
padding=0.1, width=800, height=600, title="Albums"
)
return albums_points
# ### Widgets
# +
genres = list(artists.genre_cluster.value_counts().index)
decades = sorted(albums.decade.unique(), reverse=True)
columns = sorted([
"popularity", "release_date", "total_tracks", "duration_ms", "danceability", "energy",
"key", "loudness", "mode", "speechiness", "acousticness",
"instrumentalness", "liveness", "valence", "tempo", "time_signature", "year"
])
decade_select = pn.widgets.CheckBoxGroup(
name="decades", value=decades, options=decades,
inline=False
)
x_select = pn.widgets.Select(value="valence", options=columns, name="x")
y_select = pn.widgets.Select(value="popularity", options=columns, name="y")
color_select = pn.widgets.Select(value="loudness", options=columns, name="color")
decade_box = pn.WidgetBox("# Decade", decade_select)
axes_box = pn.WidgetBox("# Axes", x_select, y_select, color_select)
# -
# ### HTML
title = "<div style='font-size:35px'>Spotify user library explorer</div>"
instruction = ("<div style='font-size:15px'>Select artists on the map and album release decade to filter albums.<br>"
"Albums view is zoomable, and axes are customizable.<br></div>")
# ### Base layout
# +
artists_points = create_artists_points(artists)
albums_points = create_albums_points(
x=x_select.value,
y=y_select.value,
color=color_select.value,
data=albums
)
layout = pn.Column(
pn.Row(
pn.Column(
pn.Pane(title, width=400),
pn.Pane(instruction, width=400),
),
pn.Pane(artists_points),
),
pn.Row(
pn.Column(
decade_box,
axes_box,
width=400,
),
pn.Pane(albums_points),
),
width_policy="max", height_policy="max"
)
# -
# ### Interactivity and selections
# +
def update(event):
if artists_select.index:
artist_names = artists_points.columns()["name"][artists_select.index]
data_albums = albums[albums.artist_name.isin(artist_names)]
else:
data_albums = albums
data_albums = data_albums[data_albums.decade.isin(decade_select.value)]
layout[-1][-1] = create_albums_points(
x=x_select.value,
y=y_select.value,
color=color_select.value,
data=data_albums
)
artists_select = hv.streams.Selection1D(source=artists_points)
x_select.param.watch(update, "value");
y_select.param.watch(update, "value");
color_select.param.watch(update, "value");
artists_select.param.watch(update, "index");
decade_select.param.watch(update, "value");
# -
# ### Result
layout.servable()