【问题标题】:Flask Return Render Template Not Working (Spotify API)Flask 返回渲染模板不工作(Spotify API)
【发布时间】: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


    【解决方案1】:

    服务器不能告诉客户端刷新它的页面。 必须刷新自身的是客户端。

    您可以使用一个在客户端运行的小 JavaScript 脚本,在您定义的关键时刻刷新整个页面,这里是音乐的结尾。

    我建议您通过 JavaScript 和 API 仅刷新页面的某些元素。这是较少的资源密集型。

    当客户端播放完音乐时,它会通过 API 警告服务器。服务器将新歌发送给他。

    from flask import request
    
    @app.route('/vintage', methods=("GET", "POST"))
    def vintage():
        if request.method == "POST":
            """next song call with API"""
            return """new data"""
        else:
            """load page"""
    

    然后,将 JavaScript 脚本放在一个文件夹中,这里是“static”。 请记住在 Flask init 中指定它。

    app = Flask(__name__, template_folder="templates", static_folder="static")
    

    并在您的 HTML 页面中调用 JavaScript

    <script src="{{ url_for('static', filename='myscript.js') }}"></script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-30
      • 1970-01-01
      • 2017-06-25
      • 2018-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多