如何将字典键显示为按钮并在烧瓶中获取用户输入?
How to present dictionary keys as buttons and get user input in flask?
我有一个 flask 脚本,目前可以从用户那里获取 Spotify 播放列表信息。我想向用户显示播放列表的名称,并让他们选择一个按钮,该按钮将 return 播放列表 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)
return 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 变量返回给我的脚本。
这就是我要做的:
- 将 href_playlist_names 传递到渲染模板并创建按钮。
像这样:
app.py
render_template("your_template_name.html", names = href_playlist_names)
your_template_name.html
{% for name in names %}
<button onclick = "give_id_to_server({{name}})" >{{name}}</button>
{% endfor %}
<script>
function give_id_to_server(name) {
$.ajax({
type: 'POST',
url: 'http://localhost:your_port/receive_id',
data: name,
});
}
</script>
然后在你的app.py中做一个flask路由点来接收这样的数据:
app.py
@app.route('/receive_id')
def do_something_with_id():
*do whatever you want to with the id
希望对您有所帮助!
我有一个 flask 脚本,目前可以从用户那里获取 Spotify 播放列表信息。我想向用户显示播放列表的名称,并让他们选择一个按钮,该按钮将 return 播放列表 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)
return 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 变量返回给我的脚本。
这就是我要做的:
- 将 href_playlist_names 传递到渲染模板并创建按钮。
像这样:
app.py
render_template("your_template_name.html", names = href_playlist_names)
your_template_name.html
{% for name in names %}
<button onclick = "give_id_to_server({{name}})" >{{name}}</button>
{% endfor %}
<script>
function give_id_to_server(name) {
$.ajax({
type: 'POST',
url: 'http://localhost:your_port/receive_id',
data: name,
});
}
</script>
然后在你的app.py中做一个flask路由点来接收这样的数据:
app.py
@app.route('/receive_id')
def do_something_with_id():
*do whatever you want to with the id
希望对您有所帮助!