我有一个flask脚本,当前可以从用户那里获取Spotify播放列表信息。我想向用户显示播放列表的名称,并让他们选择一个按钮,将播放列表id返回到我的脚本中。到目前为止,我已经:
playlist_api_endpoint = "{}/playlists".format(profile_data["href"])
playlists_response = requests.get(playlist_api_endpoint,
headers=authorization_header)
playlist_data = json.loads(playlists_response.text)
它将返回一个json,类似于:
{
"href": "https://api.spotify.com/v1/users/wizzler/playlists",
"items": [ {
"collaborative": false,
"external_urls": {
"spotify": "http://open.spotify.com/user/wizzler/playlists/53Y8wT46QIMz5H4WQ8O22c"
},
"href": "https://api.spotify.com/v1/users/wizzler/playlists/53Y8wT46QIMz5H4WQ8O22c",
"id": "53Y8wT46QIMz5H4WQ8O22c",
"images" : [ ],
"name": "Wizzlers Big Playlist",
"owner": {
"external_urls": {
"spotify": "http://open.spotify.com/user/wizzler"
},
"href": "https://api.spotify.com/v1/users/wizzler",
"id": "wizzler",
"type": "user",
"uri": "spotify:user:wizzler"
},
"public": true,
"snapshot_id" : "bNLWdmhh+HDsbHzhckXeDC0uyKyg4FjPI/KEsKjAE526usnz2LxwgyBoMShVL+z+",
"tracks": {
"href": "https://api.spotify.com/v1/users/wizzler/playlists/53Y8wT46QIMz5H4WQ8O22c/tracks",
"total": 30
}
我有以下代码来获取所有播放列表的名称和ID:
href_playlist_names = []
href_playlist_id = []
for items in playlist_data:
href_playlist_names.append(items['items']['name'])
href_playlist_id.append(items['items']['id'])
我想知道如何构建一个渲染模板,将每个名称作为函数呈现,并将相应的id作为python变量返回给我的脚本。