【发布时间】:2023-01-01 01:06:26
【问题描述】:
我正在尝试创建一个网页来显示用户当前播放的曲目详细信息和自动更新,但是当曲目发生变化时 html 页面不会刷新变量,即使 python 程序完美运行而没有抛出错误。
蟒蛇代码:
@app.route('/vintage')
def vintage():
cache_handler = spotipy.cache_handler.FlaskSessionCacheHandler(session)
auth_manager = spotipy.oauth2.SpotifyOAuth(cache_handler=cache_handler, show_dialog=False, scope='user-read-currently-playing')
if not auth_manager.validate_token(cache_handler.get_cached_token()):
return redirect('/')
spotify = spotipy.Spotify(auth_manager=auth_manager)
try:
track = spotify.current_user_playing_track()["item"]["name"]
global trackg
trackg = track
artist = spotify.current_user_playing_track()["item"]["artists"][0]["name"]
image = spotify.current_user_playing_track()["item"]["album"]["images"][0]["url"]
except TypeError:
track = "Your Player Is Idle"
artist = "Visuafy Will Refresh Automatically"
image = url_for('static', filename='idle.jpeg')
@copy_current_request_context
def scan():
while True:
new_track = spotify.current_user_playing_track()["item"]["name"]
if (new_track != trackg):
return render_template('vintage.html', track=new_track)
v = threading.Thread(target=scan)
v.start()
return render_template('vintage.html', track = track, artist = artist, image=image, reload=False)
HTML 代码:
<html>
<body>
<div class="d-flex flex-column justify-content-center w-100 h-100">
<div class="d-flex flex-column justify-content-center align-items-center">
<div class="center">
<center>
<img src="{{image}}" height="300" width="300" style="border-radius: 5%;">
<h1>{{track}}</h1>
<h3>{{artist}}</h3>
</center>
</body>
</html>
我希望 HTML 页面刷新并显示新变量。我什至尝试过重定向,但没有用。
【问题讨论】:
标签: python flask spotify spotipy