-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgui.py
57 lines (48 loc) · 2.19 KB
/
gui.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
"""Open visualization file as an interactive web app using Eel.
open_visualization(): opens the visualization in browser and exposes functions to eel
"""
from playlist import Playlist
import eel
def open_visualization(spotify, data, time_range):
"""
Opens index.html file in Chrome (or default browser as fallback) and
sets up connection between front-end and Python using Eel in order to
handle user interaction within Python code.
Args
----
spotify: Spotify object
data: Pandas dataframe
Top tracks data, needed for playlist creation on button press
time_range: str
Time range of top tracks. Possible values are:
short_term - Approx last 4 weeks
medium_term - Approx last 6 months
long_term - All time
"""
eel.init(".")
@eel.expose
def create_playlist(): # pylint: disable=unused-variable
"""
Handles button press from top tracks visualization and
creates Spotify playlist with top tracks
"""
# Create playlist from top tracks data using Playlist class
spotitude_playlist = Playlist(spotify)
spotitude_playlist.create_spotitude_playlist(time_range, data["id"].tolist())
print(
f"Playlist '{spotitude_playlist.name}' created.\n{spotitude_playlist.url}"
)
# Change 'Create Playlist' to 'Open Playlist' and write hidden URL to html file
with open("index.html", "r") as spot_html:
filedata = spot_html.read()
filedata = filedata.replace("Create Playlist", "Open Playlist")
# Change onclick action to open playlist url, also possible independent from python script
filedata = filedata.replace(
"onclick='create_playlist()'",
f"onclick=\"window.open('{spotitude_playlist.url}','_blank','resizable=yes')",
)
with open("index.html", "w") as spot_html:
spot_html.write(filedata)
# pylint: disable=no-member
eel.open_url(spotitude_playlist.url) # open playlist in new window
eel.start("index.html", mode="chrome") # default browser as fallback