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.
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)
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)