jisaacso219 commited on
Commit
b414958
Β·
verified Β·
1 Parent(s): 1ff8081

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -14
app.py CHANGED
@@ -17,39 +17,44 @@ sp_oauth = SpotifyOAuth(
17
  scope=SCOPE
18
  )
19
 
20
- auth_url = sp_oauth.get_authorize_url()
21
-
22
- sp = None
23
  user_playlists = {}
24
  selected_playlist_id = None
25
 
 
 
 
26
  with gr.Blocks(title="RNG Spotify Playlist Shuffler") as demo:
27
- gr.Markdown("## 🎡 RNG Spotify Playlist Shuffler")
28
  gr.Markdown("A clean and simple way to shuffle your Spotify playlists with one click.")
29
 
30
  with gr.Column():
31
- login_link = gr.Markdown(f"[πŸ” Step 1: Login to Spotify]({auth_url})")
32
- code_box = gr.Textbox(label="Step 2: Paste Authorization Code", visible=True)
33
- authorize_button = gr.Button("βœ… Step 3: Authorize", visible=True)
34
  login_status = gr.Markdown(visible=False)
35
- playlist_dropdown = gr.Dropdown(choices=[], label="Step 4: Select Playlist", visible=False)
36
  shuffle_button = gr.Button("πŸ”€ Step 5: Shuffle and Play", visible=False)
37
  result_box = gr.Markdown(visible=False)
38
 
39
- # Autofill JavaScript
40
  gr.HTML("""
41
  <script>
42
  window.onload = () => {
43
  const params = new URLSearchParams(window.location.search);
44
  const code = params.get("code");
45
  if (code) {
46
- document.querySelector("textarea").value = code;
47
- document.querySelector("button").click();
 
 
48
  }
49
  }
50
  </script>
51
  """)
52
 
 
 
 
53
  def check_login(code):
54
  global sp, user_playlists
55
  if not code:
@@ -67,7 +72,7 @@ with gr.Blocks(title="RNG Spotify Playlist Shuffler") as demo:
67
  gr.update(visible=True, value="βœ… Logged in! Select your playlist below."),
68
  gr.update(visible=True, choices=list(user_playlists.keys())),
69
  gr.update(visible=True),
70
- gr.update(visible=False)
71
  )
72
 
73
  def shuffle_and_play(playlist_name):
@@ -94,12 +99,12 @@ with gr.Blocks(title="RNG Spotify Playlist Shuffler") as demo:
94
  sp.start_playback(device_id=device_id, uris=uris)
95
  return gr.update(value=f"▢️ Now playing: **{playlist_name}** in shuffled order!", visible=True)
96
 
 
97
  authorize_button.click(
98
  check_login,
99
- inputs=[code_box],
100
  outputs=[login_status, playlist_dropdown, shuffle_button, result_box]
101
  )
102
-
103
  shuffle_button.click(
104
  shuffle_and_play,
105
  inputs=[playlist_dropdown],
@@ -108,3 +113,4 @@ with gr.Blocks(title="RNG Spotify Playlist Shuffler") as demo:
108
 
109
  if __name__ == "__main__":
110
  demo.launch()
 
 
17
  scope=SCOPE
18
  )
19
 
20
+ sp = None # Will be set after authorization
 
 
21
  user_playlists = {}
22
  selected_playlist_id = None
23
 
24
+ def get_auth_url():
25
+ return sp_oauth.get_authorize_url()
26
+
27
  with gr.Blocks(title="RNG Spotify Playlist Shuffler") as demo:
28
+ gr.Markdown("## 🎡 RNG Spotify Playlist Shuffler", elem_id="title")
29
  gr.Markdown("A clean and simple way to shuffle your Spotify playlists with one click.")
30
 
31
  with gr.Column():
32
+ auth_url_button = gr.Button("πŸ” Step 1: Login to Spotify")
33
+ auth_code_box = gr.Textbox(label="Step 2: Paste authorization code", placeholder="...?code=ABC123")
34
+ authorize_button = gr.Button("βœ… Step 3: Complete Authorization")
35
  login_status = gr.Markdown(visible=False)
36
+ playlist_dropdown = gr.Dropdown(choices=[], label="Step 4: 🎧 Select a Playlist", visible=False)
37
  shuffle_button = gr.Button("πŸ”€ Step 5: Shuffle and Play", visible=False)
38
  result_box = gr.Markdown(visible=False)
39
 
 
40
  gr.HTML("""
41
  <script>
42
  window.onload = () => {
43
  const params = new URLSearchParams(window.location.search);
44
  const code = params.get("code");
45
  if (code) {
46
+ const textbox = document.querySelector("textarea");
47
+ if (textbox) {
48
+ textbox.value = code;
49
+ }
50
  }
51
  }
52
  </script>
53
  """)
54
 
55
+ def open_auth_url():
56
+ return gr.update(value=get_auth_url())
57
+
58
  def check_login(code):
59
  global sp, user_playlists
60
  if not code:
 
72
  gr.update(visible=True, value="βœ… Logged in! Select your playlist below."),
73
  gr.update(visible=True, choices=list(user_playlists.keys())),
74
  gr.update(visible=True),
75
+ gr.update(visible=True)
76
  )
77
 
78
  def shuffle_and_play(playlist_name):
 
99
  sp.start_playback(device_id=device_id, uris=uris)
100
  return gr.update(value=f"▢️ Now playing: **{playlist_name}** in shuffled order!", visible=True)
101
 
102
+ auth_url_button.click(fn=lambda: None, _js=f"() => window.open('{get_auth_url()}', '_blank')")
103
  authorize_button.click(
104
  check_login,
105
+ inputs=[auth_code_box],
106
  outputs=[login_status, playlist_dropdown, shuffle_button, result_box]
107
  )
 
108
  shuffle_button.click(
109
  shuffle_and_play,
110
  inputs=[playlist_dropdown],
 
113
 
114
  if __name__ == "__main__":
115
  demo.launch()
116
+