路由之间传递数据 [英] Passing data from route to route

查看:58
本文介绍了路由之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建可以上传CSV文件的东西,然后可以通过浏览器在/transform 上查看数据表,并可以从中检索静态.png文件.使用matplotlib /plot 创建图.

I am attempting to create something where a CSV file can be uploaded, a table of the data can then be viewed through the browser on /transform, and a static .png file can be retrieved from /plot using matplotlib to create the plot.

我不知道JavaScript或如何在浏览器中呈现数据图,所以我作弊并使用matplotlib,可以在其中将图保存到静态目录(/transform ),然后将其投放到/plot .

I don't know JavaScript or how to render a graph of the data in a browser, so I'm cheating and using matplotlib where I can save a plot to a static directory (/transform) and then serve it on /plot.

我遇到的问题是图片没有更新.第一次尝试与上述过程一起使用,然后当我想重复该过程时,我一次又一次得到相同的图片.我以为在每次重复过程中,这些图都可以节省下来,但我可能是错的.这是浏览器缓存问题吗?

The problem I am running into is the pictures aren't updating. The first attempt works with the process described above, and then when I want to repeat the process I get the same picture graph served again and again. I thought the plots would just save over themselves on each repeat of the process but I may be wrong. Is this a browser cache issue?

from flask import Flask, make_response, request, render_template
from werkzeug.utils import secure_filename
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import time



app = Flask(__name__)



@app.route('/')
def form():
    return render_template('form.html')

@app.route('/transform', methods=["POST"])
def transform_view():
    f = request.files['data_file']
    filename = secure_filename(f.filename)
    f.save(filename)

    df = pd.read_csv(filename, index_col='Date', parse_dates=True)

    OAT = pd.Series(df['OAT'])
    RAT = pd.Series(df['RAT'])
    MAT = pd.Series(df['MAT'])

    df_OATrat = (OAT - RAT)
    df_MATrat = (MAT - RAT)

    plt.scatter(df_OATrat,df_MATrat, color='grey', marker='+')
    plt.xlabel('OAT-RAT')
    plt.ylabel('MAT-RAT')
    plt.title('Economizer Diagnostics')
    plt.plot([0,-18],[0,-18], color='green', label='100% OSA during ideal conditions')
    plt.plot([0,20],[0,5], color='red', label='Minimum OSA in cooling mode')
    plt.plot([0,-38],[0,-9.5], color='blue', label='Minimum OSA in heating mode')
    plt.plot([0,0],[-20,10], color='black')
    plt.plot([-30,20],[0,0], color='black')
    plt.legend()
    plt.text(-3, -28, time.ctime(), fontsize=9)
    plt.savefig('static/plot.png')

    return render_template('table.html',  tables=[df.to_html(classes='data')], titles=df.columns.values)


@app.route('/plot', methods=['GET'])
def plot_view():   
    return render_template('serve.html')


if __name__ == '__main__':
    app.run(debug=True)

更新脚本<​​/em> 相对于静态文件将图保存到内存中

UPDATED SCRIPT to save plot into memory Vs static file

from flask import Flask, make_response, request, render_template, send_file
from io import BytesIO
from werkzeug.utils import secure_filename
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import time



app = Flask(__name__)


@app.route('/')
def form():
    return render_template('form.html')

@app.route('/transform', methods=["POST"])
def transform_view():
    f = request.files['data_file']
    filename = secure_filename(f.filename)
    f.save(filename)

    df = pd.read_csv(filename, index_col='Date', parse_dates=True)

    OAT = pd.Series(df['OAT'])
    RAT = pd.Series(df['RAT'])
    MAT = pd.Series(df['MAT'])

    df_OATrat = (OAT - RAT)
    df_MATrat = (MAT - RAT)

    plt.scatter(df_OATrat,df_MATrat, color='grey', marker='+')
    plt.xlabel('OAT-RAT')
    plt.ylabel('MAT-RAT')
    plt.title('Economizer Diagnostics')
    plt.plot([0,-18],[0,-18], color='green', label='100% OSA during ideal conditions')
    plt.plot([0,20],[0,5], color='red', label='Minimum OSA in cooling mode')
    plt.plot([0,-38],[0,-9.5], color='blue', label='Minimum OSA in heating mode')
    plt.plot([0,0],[-20,10], color='black')
    plt.plot([-30,20],[0,0], color='black')
    #plt.legend()
    plt.text(-3, -28, time.ctime(), fontsize=9)
    img = BytesIO()
    plt.savefig(img)
    img.seek(0)
    resp = make_response(send_file(img, mimetype='image/png'))
    resp.cache_control.no_cache = True
    return resp


if __name__ == '__main__':
    app.run(debug=True)

推荐答案

如果是缓存问题,则可以实施缓存清除器,也可以彻底禁用缓存.

If it's a caching issue, you can either implement a cache buster or disable cache once and for all.

要实现缓存清除器,您可以添加

To implement a cache buster, you can add automatic versioning to your static files.

要禁用缓存,请使用 make_response()在响应对象上设置标头,然后在响应中添加 no-cache .

To disable cache, set your headers on the response objects using make_response() and add no-cache to the response.

from flask import make_response

@app.route('/nocache')
def something_not_cached():
    resp = make_response(render_template(...))
    resp.cache_control.no_cache = True
    return resp

这篇关于路由之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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