如何刷新烧瓶网页? [英] How to refresh the flask web page?

查看:62
本文介绍了如何刷新烧瓶网页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我有一个项目是在网页上实现动态绘图.

Recently, I have a project is to implement the dynamic plotting on a web page.

这是我的python代码

Here is my python code

from flask import Flask
#from flask import render_template
import matplotlib.pyplot as plt
import io
import base64

app = Flask(__name__)

@app.route('/plot')
def build_plot():

    img = io.BytesIO()

    y = [1,2,3,4,5]
    x = [0,2,1,3,4]
    plt.plot(x,y)
    plt.savefig(img, format='png')
    img.seek(0)

    plot_url = base64.b64encode(img.getvalue()).decode()

    return '<img src="data:image/png;base64,{}">'.format(plot_url)
if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')

这是我的HTML代码

<!DOCTYPE html>
<html>
    <head>
        <title> Plot</title>
        <meta content='5; url=http://127.0.0.1:5000/plot' http-equiv='refresh'>
    </head>
    <body>
        <img src="data:image/png;base64, {{ plot_url }}">
    </body>
</html>

尽管我在HTML代码中添加了<meta content='5; url=http://127.0.0.1:5000/plot' http-equiv='refresh'>,但仍无法自动刷新.

Although I add <meta content='5; url=http://127.0.0.1:5000/plot' http-equiv='refresh'> into my HTML code, it still can not refresh automatically.

如果我想观察绘图的变化,我仍然需要手动刷新它.

If I want to observe the change of the plotting, I still need to refresh it manually.

有人可以帮我解决这个问题吗?非常感谢

Can anyone help me to solve this problem? Thanks a lot

推荐答案

您不需要包含URL.只需使用meta标记的 content 属性即可.

You do not need to include the URL. Just use only the content attribute of meta tag.

<meta http-equiv="refresh" content="5" >

但是要查看图表中的实时变化,您可能需要查看 sockets 在烧瓶中.这将帮助服务器使用

But to see the real time changes in your graph, you might want to look at sockets in flask. This will help server push changes to all clients using broadcasting. Using sockets is way better approach than to refresh your page at regular interval.

这篇关于如何刷新烧瓶网页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆