-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
294 lines (257 loc) · 12.9 KB
/
main.py
File metadata and controls
294 lines (257 loc) · 12.9 KB
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
287
288
289
290
291
292
293
294
"""CSC111 Project Phase 2: Interactive Music Genre and Album Recommendation Tree (Main)
Description
===============================
This Python module is the main file that runs our app.
Note: even though some function parameters seem unused, they are actually required for the app to run properly.
This file is Copyright (c) 2023 David Wu and Kevin Hu.
"""
import os
from dotenv import load_dotenv
from dash import Dash, html, dcc, Output, Input, ctx
import plotly.graph_objects as go
import spotipy
from spotipy import SpotifyOAuth
from plot_genre_tree import plot_genre_tree, plot_default_genre_tree
from plot_recommendation_tree import plot_album_recommendation_tree, plot_genre_recommendation_tree, \
get_albums_by_genre_and_popularity
from albums_data import Album, create_albums
from genres_data import Genre, create_genres
def main() -> None:
"""
This function is the main block of code that runs our app
"""
app = Dash(__name__, suppress_callback_exceptions=True)
root_genre_stack, visited = [], set()
albums, genres = create_data()
sp = spotify_auth()
app.layout = html.Div([
html.H1(children='Music Recommendation System',
style={'textAlign': 'center', 'backgroundColor': '#383838', 'color': 'hotpink'}),
html.Div(children='''This is a web application that can recommend music based on a user's input.''',
style={'textAlign': 'center', 'backgroundColor': '#383838', 'color': 'hotpink'}),
html.Button('Get Recommendation', id='rec_button', style={'textAlign': 'center', 'height': '38px'}),
html.Button('Explore Genre Tree', id='genre_tree_button', style={'textAlign': 'center', 'height': '38px'}),
html.Div(id='rec_output'),
dcc.Graph(id='tree_plot', figure=blank_fig()),
dcc.Graph(id='placeholder', figure=blank_fig())
], style={'backgroundColor': '#383838'})
@app.callback(
Output('rec_output', 'children'),
Output('tree_plot', 'figure', allow_duplicate=True),
Input('rec_button', 'n_clicks'),
Input('genre_tree_button', 'n_clicks'),
prevent_initial_call='initial_duplicate',
suppress_callback_exceptions=True
)
def update_page(rec_button: html.Button, genre_tree_button: html.Button) -> tuple[html.Div, go.Figure]:
"""
This function updates the page based on the button pressed.
If the recommendation button is pressed, the page will be changed to display a combobox with every album.
If the genre tree button is pressed, the page will be changed to display the genre tree from plot_genre_tree.
"""
if "rec_button" == ctx.triggered_id:
main.visited = set()
return (html.Div([
html.H3('Choose either an album you like or a genre you like from one of the dropdowns below and press'
' the respective submit button:',
style={'textAlign': 'center', 'backgroundColor': '#383838', 'color': 'hotpink'}),
html.H4('Clicking on an album node will give you a new set of recommendations based on the new album'
' you clicked!',
style={'textAlign': 'center', 'backgroundColor': '#383838', 'color': 'hotpink'}),
dcc.Dropdown(
id='album_dropdown',
options=[album.name + ' - ' + album.artist for album in albums],
style={'backgroundColor': 'white', 'color': 'black'},
placeholder='Select an album...'
),
dcc.Dropdown(
id='genre_dropdown',
options=[genre.name for genre in genres],
style={'backgroundColor': 'white', 'color': 'black'},
placeholder='Select a genre...'
),
html.Div(id='album_output'),
html.Button('Submit Album', id='album_submit', style={'textAlign': 'center', 'height': '38px'}),
html.Button('Submit Genre', id='genre_submit', style={'textAlign': 'center', 'height': '38px'}),
html.Div(id='spotify_output', style={'textAlign': 'center'}),
dcc.Graph(id='rec_tree_plot', figure=blank_fig()),
]), blank_fig())
elif "genre_tree_button" == ctx.triggered_id:
main.root_genre_stack = []
return (html.Div([
html.H2('Genre Tree Free Exploration',
style={'textAlign': 'center', 'backgroundColor': '#383838', 'color': 'hotpink'}),
html.H3(
'This is an interactive visualization of music genres displayed in a hierachical tree structure.',
style={'textAlign': 'center', 'backgroundColor': '#383838', 'color': 'hotpink'}),
html.H3("You can freely explore the tree and view a genre's subgenres by clicking on it's node",
style={'textAlign': 'center', 'backgroundColor': '#383838', 'color': 'hotpink'}),
html.Button('Go Back', id='back_button', style={'textAlign': 'center', 'height': '38px'}),
]), plot_default_genre_tree())
else:
return (html.Div([
html.H3('Please press a button to get started',
style={'textAlign': 'center', 'backgroundColor': '#383838', 'color': 'hotpink'}),
]), blank_fig())
@app.callback(
Output('tree_plot', 'figure', allow_duplicate=True),
Input('tree_plot', 'clickData'),
prevent_initial_call=True
)
def plot_new_tree(clickData: dict) -> go.Figure or None:
"""
This function plots a new genre tree based on the node clicked on the old genre tree.
"""
if clickData is not None:
if clickData['points'][0]['text'] == 'Genres':
return plot_default_genre_tree()
else:
genre_name = clickData['points'][0]['text']
new_root = Genre(genre_name, None)
root_genre_stack.append(new_root)
new_fig = plot_genre_tree(new_root)
return new_fig
else:
return None
@app.callback(
Output('tree_plot', 'figure', allow_duplicate=True),
Input('back_button', 'n_clicks'),
prevent_initial_call=True,
)
def plot_previous_tree(back_button: html.Button) -> go.Figure:
"""
This function plots the previous genre tree, does nothing if it is currently the default tree.
"""
if "back_button" == ctx.triggered_id and len(root_genre_stack) > 0:
return plot_genre_tree(root_genre_stack.pop())
else:
return plot_default_genre_tree()
@app.callback(
Output('rec_tree_plot', 'figure', allow_duplicate=True),
Output('spotify_output', 'children', allow_duplicate=True),
Input('album_dropdown', 'value'),
Input('album_submit', 'n_clicks'),
prevent_initial_call=True,
)
def get_album_dropdown_value(value: str, album_submit: html.Button) -> tuple[go.Figure, html.Iframe]:
"""
This function returns the value of the album dropdown when the recommend button is pressed and plots the
recommendation tree.
Preconditions:
- value != ''
"""
main.visited = set()
if "album_submit" == ctx.triggered_id:
album_name = value.split(' - ')[0]
album_artist = value.split(' - ')[1]
album = [alb for alb in albums if alb.name == album_name and alb.artist == album_artist][0]
return (plot_album_recommendation_tree(album, visited), html.Iframe(src='', id='spotify_embed',
style={'display': 'none'}))
else:
return (blank_fig(), html.Iframe(src='', id='spotify_embed', style={'display': 'none'}))
@app.callback(
Output('rec_tree_plot', 'figure', allow_duplicate=True),
Output('spotify_output', 'children', allow_duplicate=True),
Input('genre_dropdown', 'value'),
Input('genre_submit', 'n_clicks'),
prevent_initial_call=True,
)
def get_genre_dropdown_value(value: str, genre_submit: html.Button) -> tuple[go.Figure, html.Iframe]:
"""
This function returns the value of the genre dropdown when the recommend button is pressed and plots the
recommendation tree.
Preconditions:
- value != ''
"""
if "genre_submit" == ctx.triggered_id:
genre = [gen for gen in genres if gen.name == value][0]
return (plot_genre_recommendation_tree(genre), html.Iframe(src='', id='spotify_embed',
style={'display': 'none'}))
else:
return (blank_fig(), html.Iframe(src='', id='spotify_embed', style={'display': 'none'}))
@app.callback(
Output('spotify_output', 'children', allow_duplicate=True),
Input('rec_tree_plot', 'clickData'),
prevent_initial_call=True
)
def SpotifyEmbed(clickData: dict) -> html.Div:
"""
Embeds a spotify player of the album clicked on the recommendation tree.
"""
album_name = clickData['points'][0]['text'].split(' - ')[0]
album_artist = clickData['points'][0]['text'].split(' - ')[1]
results = sp.search(q='album:' + album_name + ' artist:' + album_artist, type='album')
if not results['albums']['items']:
results = sp.search(q='album:' + album_name, type='album')
if not results['albums']['items']:
results = sp.search(q='album:' + album_name.split(' (')[0] + ' artist:' + album_artist, type='album')
if not results['albums']['items']:
results = sp.search(q='album:' + album_name.split('(')[1].split(')')[0] + ' artist:' + album_artist,
type='album')
if not results['albums']['items']:
return html.Div('No Spotify results found', style={'textAlign': 'center', 'color': 'hotpink'})
album_id = results['albums']['items'][0]['id']
return html.Div([
html.Iframe(src='https://open.spotify.com/embed/album/' + album_id, width='700', height='380'),
])
@app.callback(
Output('rec_tree_plot', 'figure', allow_duplicate=True),
Input('rec_tree_plot', 'clickData'),
prevent_initial_call=True
)
def plot_new_recommendation_tree(clickData: dict) -> go.Figure or None:
"""
This function plots a new recommendation tree based on the node clicked on the old recommendation tree.
When a leaf node is clicked on the genre recommendation tree, it will plot the album recommendation tree.
"""
if clickData is not None:
album_name = clickData['points'][0]['text'].split(' - ')[0]
album_artist = clickData['points'][0]['text'].split(' - ')[1]
album = [alb for alb in albums if alb.name == album_name and alb.artist == album_artist][0]
visited.add(album_name)
return plot_album_recommendation_tree(album, visited)
else:
return None
app.run_server(debug=False)
def create_data() -> tuple[list[Album], list[Genre]]:
"""
This function creates the data needed for our app.
"""
albums = create_albums()
genres = create_genres()
genres = [genre for genre in genres if get_albums_by_genre_and_popularity(genre.name, albums) != []]
return albums, genres
def spotify_auth() -> spotipy.Spotify:
"""
This function authenticates for the spotify API.
"""
load_dotenv()
SPOTIPY_CLIENT_ID = load_dotenv('SPOTIPY_CLIENT_ID')
SPOTIPY_CLIENT_SECRET = os.getenv('SPOTIPY_CLIENT_SECRET')
SPOTIPY_REDIRECT_URI = os.getenv('SPOTIPY_REDIRECT_URI')
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=SPOTIPY_CLIENT_ID,
client_secret=SPOTIPY_CLIENT_SECRET,
redirect_uri=SPOTIPY_REDIRECT_URI,
))
return sp
def blank_fig() -> go.Figure:
"""
This function creates a blank figure.
"""
fig = go.Figure(go.Scatter(x=[], y=[]))
fig.update_layout(paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)')
fig.update_layout(template=None)
fig.update_xaxes(showgrid=False, showticklabels=False, zeroline=False)
fig.update_yaxes(showgrid=False, showticklabels=False, zeroline=False)
return fig
if __name__ == '__main__':
import python_ta
python_ta.check_all(config={
'max-line-length': 120,
'extra-imports': ['os', 'spotipy', ' plotly.graph_objects', 'dotenv', 'plot_genre_tree',
'plot_recommendation_tree',
'dash', 'albums_data', 'genres_data'],
'allowed-io': ['main'],
'disable': ['unused-argument', 'invalid-name']
})
main()