jisaacso219 commited on
Commit
03438b7
Β·
verified Β·
1 Parent(s): 5afe83b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -10
app.py CHANGED
@@ -31,7 +31,7 @@ def complete_login(full_url):
31
  code = full_url.split("?code=")[1].split("&")[0]
32
  except IndexError:
33
  return (
34
- gr.update(value="❌ No code found in URL. Please login again."),
35
  gr.update(visible=False),
36
  gr.update(visible=False),
37
  )
@@ -48,7 +48,7 @@ def complete_login(full_url):
48
 
49
  if not devices_dict:
50
  return (
51
- gr.update(value="⚠️ Logged in, but no active Spotify devices found. Open Spotify app."),
52
  gr.update(visible=False),
53
  gr.update(visible=False),
54
  )
@@ -80,33 +80,50 @@ with gr.Blocks() as demo:
80
  gr.Markdown("## 🎡 RNG Spotify Playlist Shuffler")
81
 
82
  login_url = get_auth_url()
83
- gr.HTML(f'<a href="{login_url}" target="_blank"><button style="width:100%; height:40px; font-size:16px;">πŸ” Login to Spotify</button></a>')
84
-
85
- gr.Markdown("After logging in, copy the full redirected URL and paste below.")
86
- redirect_url_box = gr.Textbox(label="Paste redirected URL here")
87
- complete_login_btn = gr.Button("βœ… Complete Login")
88
 
89
  status = gr.Markdown()
90
  playlist_dd = gr.Dropdown(label="Select a Playlist", visible=False)
91
  device_dd = gr.Dropdown(label="Select Playback Device", visible=False)
92
 
93
- shuffle_btn = gr.Button("πŸ”€ Shuffle & Play", visible=True)
94
  result = gr.Markdown()
95
 
 
 
 
 
 
96
  complete_login_btn.click(
97
  complete_login,
98
  inputs=[redirect_url_box],
99
  outputs=[status, playlist_dd, device_dd]
100
  )
101
 
 
 
102
  shuffle_btn.click(
103
  shuffle_and_play,
104
  inputs=[playlist_dd, device_dd],
105
  outputs=[result]
106
  )
107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  if __name__ == "__main__":
109
  demo.launch(server_name="0.0.0.0", ssr_mode=False)
110
 
111
-
112
-
 
31
  code = full_url.split("?code=")[1].split("&")[0]
32
  except IndexError:
33
  return (
34
+ gr.update(value="❌ OAuth code not found. Try logging in again."),
35
  gr.update(visible=False),
36
  gr.update(visible=False),
37
  )
 
48
 
49
  if not devices_dict:
50
  return (
51
+ gr.update(value="⚠️ No active Spotify devices found. Open Spotify app."),
52
  gr.update(visible=False),
53
  gr.update(visible=False),
54
  )
 
80
  gr.Markdown("## 🎡 RNG Spotify Playlist Shuffler")
81
 
82
  login_url = get_auth_url()
83
+ login_btn = gr.HTML(
84
+ f'<a href="{login_url}"><button style="width:100%; height:40px; font-size:16px;">πŸ” Login to Spotify</button></a>'
85
+ )
 
 
86
 
87
  status = gr.Markdown()
88
  playlist_dd = gr.Dropdown(label="Select a Playlist", visible=False)
89
  device_dd = gr.Dropdown(label="Select Playback Device", visible=False)
90
 
91
+ shuffle_btn = gr.Button("πŸ”€ Shuffle & Play", visible=False)
92
  result = gr.Markdown()
93
 
94
+ # Hidden textbox to automatically capture redirected URL
95
+ redirect_url_box = gr.Textbox(visible=False)
96
+
97
+ complete_login_btn = gr.Button(visible=False)
98
+
99
  complete_login_btn.click(
100
  complete_login,
101
  inputs=[redirect_url_box],
102
  outputs=[status, playlist_dd, device_dd]
103
  )
104
 
105
+ playlist_dd.change(lambda: gr.update(visible=True), None, shuffle_btn)
106
+
107
  shuffle_btn.click(
108
  shuffle_and_play,
109
  inputs=[playlist_dd, device_dd],
110
  outputs=[result]
111
  )
112
 
113
+ # Automatic URL handling (no manual paste needed!)
114
+ demo.load(None, None, None, js="""
115
+ () => {
116
+ const params = new URLSearchParams(window.location.search);
117
+ const code = params.get('code');
118
+ if(code) {
119
+ const box = document.querySelector('#component-redirect_url_box textarea');
120
+ if(box){ box.value = window.location.href; }
121
+ const button = document.querySelector('#component-complete_login_btn');
122
+ if(button){ button.click(); }
123
+ }
124
+ }
125
+ """)
126
+
127
  if __name__ == "__main__":
128
  demo.launch(server_name="0.0.0.0", ssr_mode=False)
129