我有一个python flask服务器,正在将数据发送回HTML页面中的javascript。我正在MatPlotLib从发送到服务器的数据生成一些图形。然后,服务器还将对数据数组进行一些统计。
我的问题是,如何将其中一些图形发送回JavaScript引用程序,以显示在HTML页面上?
这是我想发送回页面的内容:
我已经尝试将这些图的路径插入到数组中,但是出于安全原因,我想避免这种情况。
我现有的代码:
import os
import pandas as pd
from flask import Flask, request, render_template, jsonify
from werkzeug.utils import secure_filename
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = '.'
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload', methods=['POST'])
def upload():
temp_file = request.files.get('my_file') # `<input ... name="my_file">`
print(temp_file.filename)
save_name = secure_filename(temp_file.filename)
full_path = os.path.join(app.config['UPLOAD_FOLDER'], save_name)
temp_file.save(full_path)
data = pd.read_csv(full_path)
headingvalue = request.form.get('headings')
graphvalue = request.form.get('graphs')
plotvalue = request.form.get('plots')
if headingvalue == "on":
headerval = "True"
else:
headerval = False
if graphvalue == "on":
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.savefig('line-graph.png')
table = data.to_html(classes='my_table', header=headerval)
##!! This is where I want to send more data and files, like the line-graph.png and some numbers that can be used by the webpage.
return table
if __name__ == '__main__':
app.run(debug=True)
您能更具体吗?也许给我们看一些代码?